-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (57 loc) · 1.84 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var TelegramBot = require('node-telegram-bot-api');
var fs = require("fs");
var config = JSON.parse(fs.readFileSync("config.json"));
// Setup telegram polling
var bot = new TelegramBot(config["Telegram_token"], { polling: true });
//incoming telegram /echo
bot.onText(/\/echo (.+)/, function (msg, match) {
var fromId = msg.from.id;
var resp = match[1];
bot.sendMessage(fromId, resp);
});
//incoming telegram /start
bot.onText(/\/start/i, function (msg, match) {
var fromId = msg.from.id;
var resp = match[1];
bot.sendMessage(fromId, resp);
});
//incoming telegram /addvalue
bot.onText(/^\/addvalue[\s]+(.+\s*)+/ig, function (msg, match) {
var fromId = msg.chat.id || msg.from.id;
var params = match[1].split(" ", 2);
var userid = params[0].toLowerCase();
var addvalue = parseInt(params[1]);
var msgoptions = {
disable_web_page_preview:true,
reply_to_message: msg.message_id
};
var db = JSON.parse(fs.readFileSync("accounts.json"));
if(db[userid]){
db[userid] += addvalue;
console.log("Update value for " + userid)
}
else {
db[userid] = addvalue;
console.log("Added user " + userid)
}
var newvalue = db[userid];
fs.writeFileSync("accounts.json", JSON.stringify(db));
bot.sendMessage(fromId, params[0] + " total now at " + newvalue.toString(), msgoptions);
});
//incoming telegram /addvalue
bot.onText(/^\/showvalue[\s]+(.+\s*)+/ig, function (msg, match) {
var fromId = msg.chat.id || msg.from.id;
var params = match[1].split(" ", 1);
var userid = params[0].toLowerCase();
var msgoptions = {
disable_web_page_preview:true,
reply_to_message: msg.message_id
};
var db = JSON.parse(fs.readFileSync("accounts.json"));
if(db[userid]){
bot.sendMessage(fromId, params[0] + " total is " + db[userid].toString(), msgoptions);
}
else {
bot.sendMessage(fromId, params[0] + " not present in DB", msgoptions);
}
});