-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (43 loc) · 1.06 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
const axios = require('axios');
const querystring = require('querystring');
class Adapter {
constructor (settings) {
if (!settings.token) throw new Error('Not specified token in settings');
this.token = settings.token;
}
set bot (bot) {
this.message = bot.message.bind(bot);
bot.on('message', (message) => {
axios.request({
url: 'https://api.telegram.org/bot' + this.token + '/sendMessage',
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
data: querystring.stringify({
chat_id: message.client.split('telegram-')[1],
text: message.text
})
});
});
}
async middleware (req, res, next) {
try {
const {
body: {
message
}
} = req;
if (message.text !== '/start') {
await this.message({
client: `telegram-${message.from.id}`,
text: message.text
});
}
res.end();
} catch (err) {
next(err);
}
}
}
module.exports = Adapter;