-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtelegram.js
More file actions
48 lines (46 loc) · 1.45 KB
/
telegram.js
File metadata and controls
48 lines (46 loc) · 1.45 KB
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
function Telegram(bot_id){
this.bot_id=bot_id;
}
Telegram.prototype.setWebhook=function(url){
return this.request('setWebhook',{url:url});
}
Telegram.prototype.setMessage=function(to,text){
return this.request('sendMessage',{chat_id:to,text:text});
}
Telegram.prototype.getStatus=function(to,text){
return this.request('getwebhookInfo',{});
}
Telegram.prototype.request=function(method,content){
return new Promise((success,fail)=>{
const https = require('https');
var options = {
host: 'api.telegram.org',
port: 443,
method: 'POST',
path: '/bot' + this.bot_id + '/' + method,
headers:{'Content-Type':'application/json'}
};
var req = https.request(options, function(res) {
var bodyChunks = [];
res.on('data', function(chunk) {
bodyChunks.push(chunk);
}).on('end', function() {
var body = Buffer.concat(bodyChunks);
// console.log('telegram resp: ' + body);
try{
var json=JSON.parse(body);
success(json);
}catch(e){
fail(e);
}
})
});
req.on('error', function(e) {
console.log('ERROR: ' + e.message);
fail(e.message);
});
req.write(JSON.stringify(content));
req.end();
});
}
module.exports=Telegram;