Commit f082ad5f11eb2ff1cd5bdc88d48dd8a1f6c5e168
1 parent
a0a9c129c4
Exists in
master
ready to run
Showing 4 changed files with 169 additions and 0 deletions Side-by-side Diff
.gitignore
config.sample.json
... | ... | @@ -0,0 +1,25 @@ |
1 | +{ | |
2 | + "ym_account": { | |
3 | + "userid": "changeme", | |
4 | + "password": "changeme" | |
5 | + }, | |
6 | + "interval_in_seconds": 180, | |
7 | + "partners": [ | |
8 | + { | |
9 | + "name": "CHANGEME", | |
10 | + "ym_ids": [ | |
11 | + "ym0", | |
12 | + "ym1", | |
13 | + "ym2" | |
14 | + ], | |
15 | + "command": "s.1234", | |
16 | + "regex_pattern": "Rp\\.(\\d+)\\.", | |
17 | + "regex_match_index": 1 | |
18 | + } | |
19 | + ], | |
20 | + "redis": { | |
21 | + "host": "127.0.0.1", | |
22 | + "port": "6379" | |
23 | + }, | |
24 | + "test": 1 | |
25 | +} |
index.js
... | ... | @@ -0,0 +1,114 @@ |
1 | +var config = require("./config.json"); | |
2 | +var ym = require("yahoomessenger"); | |
3 | +var redis = require('redis'); | |
4 | + | |
5 | +var redisClient; | |
6 | + | |
7 | +function sendPM(destination, message) { | |
8 | + console.log("SEND " + destination + ": " + message); | |
9 | + if (!config.test) { | |
10 | + ym.sendPM(destination, message); | |
11 | + } | |
12 | + | |
13 | +} | |
14 | + | |
15 | +function parseBalance(partner, message) { | |
16 | + try { | |
17 | + var re = new RegExp(partner.regex_pattern); | |
18 | + var match = message.match(re); | |
19 | + return match[partner.regex_match_index]; | |
20 | + } | |
21 | + catch(err) { | |
22 | + console.log("Exception on parseBalance: " + err); | |
23 | + return; | |
24 | + } | |
25 | +} | |
26 | + | |
27 | +function onPM(data) { | |
28 | + console.log("RECEIVE " + data.sender + ': ' + data.message); | |
29 | + | |
30 | + var partner = getPartnerFromMessage(data.sender); | |
31 | + if (!partner) { | |
32 | + console.log('Invalid sender'); | |
33 | + return; | |
34 | + } | |
35 | + | |
36 | + console.log("Message is from known sender: " + data.sender + "(" + partner.name + ")"); | |
37 | + var balance = parseBalance(partner, data.message); | |
38 | + if (balance) { | |
39 | + console.log("Balance di " + partner.name + ": " + balance); | |
40 | + saveBalanceToRedis(partner, balance); | |
41 | + } | |
42 | +} | |
43 | + | |
44 | +function saveBalanceToRedis(partner, balance) { | |
45 | + var key = 'balance.gw:' + partner.name; | |
46 | + redisClient.set(key, Number(balance)); | |
47 | + redisClient.expire(key, 3600*24*7); | |
48 | +} | |
49 | + | |
50 | +function getPartnerFromMessage(sender) { | |
51 | + var partner_count = config.partners.length; | |
52 | + | |
53 | + for (var i = 0; i < partner_count; i++) { | |
54 | + var partner = config.partners[i]; | |
55 | + var ym_ids = partner.ym_ids; | |
56 | + var ym_count = ym_ids.length; | |
57 | + | |
58 | + for (var j = 0; j < ym_count; j++) { | |
59 | + if (sender == ym_ids[j]) { | |
60 | + return partner; | |
61 | + } | |
62 | + } | |
63 | + | |
64 | + } | |
65 | + | |
66 | + return; | |
67 | + | |
68 | +} | |
69 | + | |
70 | +function checkAllBalance() { | |
71 | + var count = config.partners.length; | |
72 | + for (var i = 0; i < count; i++) { | |
73 | + var partner = config.partners[i]; | |
74 | + | |
75 | + console.log("Checking balance for " + partner.name); | |
76 | + | |
77 | + var ym_idx = Math.round(Math.random() * (partner.ym_ids.length - 1)); | |
78 | + sendPM(partner.ym_ids[ym_idx], partner.command); | |
79 | + } | |
80 | +} | |
81 | + | |
82 | + | |
83 | +try { | |
84 | + redisClient = redis.createClient(config.redis.port, config.redis.host); | |
85 | +} catch(err) { | |
86 | + logger.info("Error creating redis client"); | |
87 | +} | |
88 | + | |
89 | + | |
90 | +ym.newInstance(); | |
91 | + | |
92 | +ym.on("ready", function() { | |
93 | + ym.login(config.ym_account.userid, config.ym_account.password); | |
94 | +}); | |
95 | + | |
96 | +ym.on('loginSuccessful', function(data) { | |
97 | + console.log("Login successful as: " + data.firstname + " " + data.lastname + " (" + data.user_id + ")"); | |
98 | + | |
99 | + | |
100 | + checkAllBalance(); | |
101 | + | |
102 | + var sleepTime = config.interval_in_seconds * 1000; | |
103 | + if (config.test) { | |
104 | + sleepTime = 1000; | |
105 | + } | |
106 | + setInterval(checkAllBalance, sleepTime); | |
107 | +}); | |
108 | + | |
109 | +ym.on('buddyAddRequest', function(data) { | |
110 | + ym.acceptAddBuddy(data.username); | |
111 | + console.log("Add budy: " + data.username); | |
112 | +}); | |
113 | + | |
114 | +ym.on('pm', onPM); |
package.json
... | ... | @@ -0,0 +1,28 @@ |
1 | +{ | |
2 | + "name": "sate24-ym-balance", | |
3 | + "version": "1.0.0", | |
4 | + "description": "Tools tuk check balance via YM", | |
5 | + "main": "index.js", | |
6 | + "scripts": { | |
7 | + "test": "mocha" | |
8 | + }, | |
9 | + "repository": { | |
10 | + "type": "git", | |
11 | + "url": "git@gitlab.kodesumber.com:reload97/sate24-ym-balance.git" | |
12 | + }, | |
13 | + "keywords": [ | |
14 | + "st24", | |
15 | + "ppob", | |
16 | + "reload97", | |
17 | + "r97", | |
18 | + "ym", | |
19 | + "yahoo", | |
20 | + "yahoomessenger" | |
21 | + ], | |
22 | + "author": "Adhidarma Hadiwinoto <me@adhisimon.org>", | |
23 | + "license": "ISC", | |
24 | + "dependencies": { | |
25 | + "redis": "^2.6.0-2", | |
26 | + "yahoomessenger": "^0.1.3-Beta" | |
27 | + } | |
28 | +} |