Commit 499065fa9db3c8aed5ab680a79aa21490760a132
1 parent
2887790736
Exists in
master
Beta Version
Showing 2 changed files with 71 additions and 1 deletions Inline Diff
index.js
File was created | 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 | |||
70 |
package.json
1 | { | 1 | { |
2 | "name": "node-sate24-telegram", | 2 | "name": "node-sate24-telegram", |
3 | "version": "0.0.1", | 3 | "version": "0.0.1", |
4 | "description": "ST24 Telegram Center", | 4 | "description": "ST24 Telegram Center", |
5 | "main": "index.js", | 5 | "main": "index.js", |
6 | "scripts": { | 6 | "scripts": { |
7 | "test": "echo \"Error: no test specified\" && exit 1" | 7 | "test": "echo \"Error: no test specified\" && exit 1" |
8 | }, | 8 | }, |
9 | "repository": { | 9 | "repository": { |
10 | "type": "git", | 10 | "type": "git", |
11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-center-telegram.git" | 11 | "url": "git@gitlab.kodesumber.com:reload97/sate24-center-telegram.git" |
12 | }, | 12 | }, |
13 | "keywords": [ | 13 | "keywords": [ |
14 | "st24", | 14 | "st24", |
15 | "reload97", | 15 | "reload97", |
16 | "telegram" | 16 | "telegram" |
17 | ], | 17 | ], |
18 | "author": "husni", | 18 | "author": "husni", |
19 | "license": "ISC", | 19 | "license": "ISC", |
20 | "dependencies": { | 20 | "dependencies": { |
21 | "iniparser": "^1.0.5", | 21 | "iniparser": "^1.0.5", |
22 | "telegrambot": "0.0.11" | 22 | "node-telegram-bot-api": "^0.12.1", |
23 | "request": "^2.61.0" | ||
23 | } | 24 | } |
24 | } | 25 | } |
25 | 26 |