Commit 499065fa9db3c8aed5ab680a79aa21490760a132
1 parent
2887790736
Exists in
master
Beta Version
Showing 2 changed files with 71 additions and 1 deletions Side-by-side Diff
index.js
... | ... | @@ -0,0 +1,69 @@ |
1 | +var iniparser = require('iniparser'); | |
2 | +var config = iniparser.parseSync('./config.ini'); | |
3 | +var telegram = require('node-telegram-bot-api'); | |
4 | +var request = require('request'); | |
5 | +var http = require('http'); | |
6 | +var url = require('url'); | |
7 | + | |
8 | +var chat_ids = {}; | |
9 | + | |
10 | +var options = { | |
11 | + polling: true | |
12 | +}; | |
13 | + | |
14 | +var bot = new telegram(config.globals.token, options); | |
15 | + | |
16 | +function sendMessage(destination, message) { | |
17 | + //destination = destination.replace(/@TELEGRAM$/, ''); | |
18 | + console.log('Sending reply to ' + destination + ': ' + message); | |
19 | + var chat_id = chat_ids[destination]; | |
20 | + bot.sendMessage(chat_id, message); | |
21 | +} | |
22 | + | |
23 | +function createHttpResponseServer(){ | |
24 | + var httpServer = http.createServer(function(request,response){ | |
25 | + var qs = url.parse(request.url, true).query; | |
26 | + console.log('Incoming message from SMSIN server:') | |
27 | + console.log(qs); | |
28 | + response.end('OK'); | |
29 | + | |
30 | + sendMessage(qs.PhoneNumber, qs.text); | |
31 | + }); | |
32 | + httpServer.listen(config.globals.listen_port, function(){ | |
33 | + console.log("listening on " + config.globals.listen_port); | |
34 | + }) | |
35 | +} | |
36 | +createHttpResponseServer(); | |
37 | + | |
38 | +bot.getMe().then(function (me) { | |
39 | + console.log('Hi my name is %s!', me.username); | |
40 | +}); | |
41 | +bot.on('text', function (msg) { | |
42 | + console.log(msg); | |
43 | + | |
44 | + var from = msg.from.username.toUpperCase() + '@TELEGRAM'; | |
45 | + | |
46 | + chat_ids[from] = msg.chat.id; | |
47 | + | |
48 | + var request_opts = { | |
49 | + url: config.globals.aaa, | |
50 | + qs: { | |
51 | + PhoneNumber: from, | |
52 | + Text: msg.text, | |
53 | + Res_Port: config.globals.listen_port, | |
54 | + SMSCID: config.globals.smscid | |
55 | + } | |
56 | + }; | |
57 | + | |
58 | + request(request_opts, function(err, response, body) { | |
59 | + if (err) { | |
60 | + console.log('Request error: ' + err); | |
61 | + return; | |
62 | + } | |
63 | + | |
64 | + console.log('Response: ' + response); | |
65 | + console.log('Body: ' + body); | |
66 | + | |
67 | + }); | |
68 | +}); | |
69 | + |