-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.js
38 lines (31 loc) · 1.06 KB
/
telegram.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
var request = require('request');
var telegramApiEndpoint = "https://api.telegram.org/bot"
module.exports = () => {
this.telegramApi = "";
this.sendRequest = (method, data, callback) => {
request.post({url: this.telegramApi + "/" + method, body: data, json:true}, callback);
}
var response = {};
response.createConnection = (token, urlWebhook, callback) => {
//check if token is set:
if(!token) { throw "You must provide a telegram bot token"};
if(!urlWebhook) { throw "You must provide a webhook url"};
this.telegramApi = telegramApiEndpoint + token;
var data = {url: 'https://' + urlWebhook};
this.sendRequest('setWebhook', data, (err, response, body) => {
callback(err);
console.log('telegram connection to ' + urlWebhook + ' : ', body);
});
};
response.sendMessage = (to, text, keyboard, callback) => {
var data = {
chat_id: to,
text: text,
reply_markup: keyboard
};
this.sendRequest('sendMessage', data, (err, response, body) => {
callback(err);
});
};
return response;
}