-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTorvyBot.js
175 lines (142 loc) · 4.34 KB
/
TorvyBot.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
var mqtt = require('mqtt');
var emoji= require('node-emoji');
var tgbot = require('node-telegram-bot-api');
//various emoji, still more to add
var rewind= emoji.get('rewind');
var sunglasses=emoji.get('sunglasses');
var sorriso= emoji.get('grin');
var batteria=emoji.get('battery'); //non vuole funzionare
var frecciaindietro=emoji.get('arrow_left');
var sunny=emoji.get('sunny');
var moon=emoji.get('new_moon');
var erba=emoji.get('leaves');
var erbasecca=emoji.get('fallen_leaf');
var authorized_users = [
111111,
000000
];
// Inizializzazione del bot con il token
var token = "YOUR TOKEN BOT";
var bot = new tgbot(token, {polling:true});
bot.onText(/\Menu/, function (msg) { //quando riceve il comando menu
if(!isAuthorized(msg.from.id)) return; //controlla che sia l'id autorizzato
var chatId = msg.chat.id;
var mopts = {
reply_markup: JSON.stringify({
keyboard: [
['Torvy'],
['Movy','Smoky'],
['Help','Settings']
],
one_time_keyboard:true, //dopo che la clicchi scompare
resize_keyboard:true, //ridimensiona i botton
force_reply: true
})
};
bot.sendMessage(chatId, 'Let me know what you want next: '+ sorriso, mopts);
});
bot.onText(/\Torvy/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
var chatId = msg.chat.id;
var topts = {
reply_markup: JSON.stringify({
keyboard: [
[sunny + '\ntorvy ON', moon + '\ntorvy OFF',erba + '\neco ON',erbasecca + '\neco OFF'],
['Temperature','Real Feel Temperature','Humidity','Real Feel Humidity'],
['Set Temperature'],
['Battery status'],
[rewind + 'Menu']
],
resize_keyboard:true
})
};
bot.sendMessage(chatId, 'Torvy commands:' + sunglasses, topts);
});
bot.onText(/torvy ON/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('7,1,1,1');
var chatId = msg.chat.id;
bot.sendMessage(chatId, 'Torvy Acceso');
});
bot.onText(/torvy OFF/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('7,1,1,1');
var chatId = msg.chat.id;
bot.sendMessage(chatId, 'Torvy Spento');
});
bot.onText(/\eco ON/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('7,1,1,5');
var chatId = msg.chat.id;
bot.sendMessage(chatId, 'Ecomode On');
});
bot.onText(/\eco OFF/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('8,1,1,1');
var chatId = msg.chat.id;
bot.sendMessage(chatId, 'Ecomode disattivato');
});
bot.onText(/\Set Temperature/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('7,1,1,1');
var chatId = msg.chat.id;
bot.sendMessage(chatId, 'Temperature: ');
});
bot.onText(/\Movy/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
var chatId = msg.chat.id;
var opts = {
reply_markup: JSON.stringify({
keyboard: [
['Battery status'],
['Menu']],
resize_keyboard:true
})
};
bot.sendMessage(chatId, 'Movy commands', opts);
});
bot.onText(/\Battery status/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('7,1,1,1');
//deve ricevere lo stato della batteria
var chatId = msg.chat.id;
bot.sendMessage(chatId,battery+ ' Battery Status: ');
});
bot.onText(/\Smoky/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
var chatId = msg.chat.id;
var topts = {
reply_markup: JSON.stringify({
keyboard: [
['Alarm OFF','Get Gas'],
['Menu']
],
resize_keyboard:true
})
};
bot.sendMessage(chatId, 'Smoky commands:', topts);
});
bot.onText(/\Alarm OFF/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('7,1,1,1');
var chatId = msg.chat.id;
bot.sendMessage(chatId, 'Gas alarm is off');
});
bot.onText(/\Get Gas/, function (msg) {
if(!isAuthorized(msg.from.id)) return;
invio('7,1,1,1');
var chatId = msg.chat.id;
bot.sendMessage(chatId, 'Gas is: ');
});
// Controlla se l'id è autorizzato
function isAuthorized(userid) {
for(i = 0; i < authorized_users.length; i++)
if(authorized_users[i ] == userid) return true;
return false;
}
function invio(mess){
var client = mqtt.connect('tcp://localhost:1883', [{host: 'localhost', port: 1883}]);
client.subscribe('RFM/101/13');
client.publish('RFM/101/13', mess);
client.end();
}