-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapi.js
79 lines (51 loc) · 2.12 KB
/
api.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const Homey = require('homey');
const fetch = require('node-fetch');
module.exports =
{
async addbot({homey, body: {bot_token}}){
const result = await fetch('https://api.telegram.org/bot' + bot_token + '/setWebhook?url=https://webhooks.athom.com/webhook/' + Homey.env.CLIENT_ID);
if(!result.ok) {
console.log('Response:', await result.text());
return ('Error ' + result.response.statusCode + ': ' + result.data, false);
}
const body = await result.json();
if (result.ok && body && body.ok === true){
homey.settings.set('bot_token', bot_token);
return true;
}
},
async deletebot({homey, args}){
const result = await fetch('https://api.telegram.org/bot' + homey.settings.get('bot_token') + '/setWebhook?url=');
if(!result.ok) {
console.log('Response:', await result.text());
return false;
}
const body = await result.json();
if (result.ok && body && body.ok === true){
homey.settings.set('bot_token', '');
return true;
}
},
async renew_webhook({homey, args}){
var result = await homey.app.unregister_webhook();
var result = await homey.app.register_webhook();
return result;
},
async send_message({homey, args}){
console.log ("received: " + JSON.stringify(args));
var chat_id = args.query.to;
var message = args.query.text;
var custom_bot = homey.settings.get('bot_token');
if (custom_bot) {
const result = await fetch('https://api.telegram.org/bot' + custom_bot + '/sendMessage?chat_id=' + chat_id + '&parse_mode=Markdown&text=' + encodeURIComponent(message));
} else {
const result = await fetch('https://telegram.corbata.nl/?action=sendMessage&chat_id=' + chat_id + '&text=' + encodeURIComponent(message));
}
if(!result.ok) {
console.log('Response:', await result.text());
return false;
}
const body = await result.json();
return (result.ok && body && body.ok === true);
}
}