-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.js
216 lines (180 loc) · 5.57 KB
/
bot.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/* Node.js */
var needle = require('needle');
var fs = require('fs');
var BASE_URL = "https://api.telegram.org/botTUOIDTUTTO"; // ID del BOT da sostituire
var POLLING_URL = BASE_URL + "getUpdates?offset=:offset:&timeout=60";
var SEND_MESSAGE_URL = BASE_URL + "sendMessage";
var SEND_PHOTO_URL = BASE_URL + "sendPhoto";
var CREATOR_ID = ; // ID del creatore
/*
start - Da' il benvenuto
help - Visualizza l'elenco dei comandi con relativi aiuti
esami - Visualizza la lista dei prossimi esami
citazioni - Visualizza una citazione a caso
foto - Invia una foto a caso
yesno - PROVA
dosth - PROVA
esercizi - Invia un esercizio (ancora da implementare)
*/
function sendErrorMessage (text) {
sendSimpleTextMessage(CREATOR_ID, text);
}
function sendMessage (msg) {
needle.post(SEND_MESSAGE_URL, msg, function(err, response) {
if (err) { console.log("Errore nella spedizione del messaggio\n"); console.log(response); return false; }
if (response.status == 200) console.log("Messaggio spedito con successo a " + msg.chat_id);
});
}
function sendSimpleTextMessage (chat_id, text) {
var answer = {
chat_id: chat_id,
text: text
};
sendMessage(answer);
}
var dosth = function (message) {
var caps = message.text.toUpperCase();
sendSimpleTextMessage(message.chat.id,
"You told me to do something, so I took your input and made it all caps. Look: " + caps);
}
var start = function (message) {
sendSimpleTextMessage(message.chat.id,
"Benvenuto su SNS Mate 2014");
}
var help = function (message) {
sendSimpleTextMessage(message.chat.id,
"Elenco di comandi che puoi utilizzare:\n"+
"/help\t Visualizza questa scritta\n"+
"/dosth msg\t Converte il messaggio in lettere maiuscole\n");
}
var yesno = function (message) {
var keyboard = {
keyboard: [ ["Yes", "No"] ],
one_time_keyboard: true,
resize_keyboard: true
};
var answer = {
chat_id: message.chat.id,
text: 'Yes or No?',
reply_markup: JSON.stringify(keyboard)
};
sendMessage(answer);
}
var admin = function (message) {
if (message.chat.id == CREATOR_ID) {
sendSimpleTextMessage(message.chat.id, "Si, sei negli Admin");
} else {
sendSimpleTextMessage(message.chat.id, "WTF?");
}
}
//Mostra gli esami contenuti nel file esami.txt
var esami = function (message) {
fs.readFile("esami.txt", function (err, logData) {
if (err) {
sendErrorMessage("esami.txt errore");
return false;
}
var text = logData.toString();
var lines = text.split("\n");
var totale = 0;
var messaggio = "Lista dei prossimi esami:\n";
lines.forEach(function (line) {
if (line != "") {
var parts = line.split("\t");
totale++;
messaggio += parts[0] + ", il " + parts[1] + " ore " + parts[2] + " in aula " + parts[3] + "\n";
}
});
messaggio += "\nTotale esami: " + totale + "";
sendSimpleTextMessage(message.chat.id, messaggio);
});
}
function randomInBetween (minimum, maximum) {
return Math.floor(Math.random() * (maximum - minimum + 1)) + minimum;
}
function sendRandomLine (chat_id, filename) {
fs.readFile(filename, function (err, logData) {
if (err) {
sendErrorMessage(filename + " errore");
return false;
}
var text = logData.toString();
var lines = text.split("\n");
sendSimpleTextMessage(chat_id, lines[randomInBetween(0, lines.length-1)]);
});
}
var citazioni = function (message) {
sendRandomLine(message.chat.id, "citazioni.txt");
}
//Toglie gli esami la cui data è anteriore a quella attuale
function aggiornaEsami () {
}
var esercizi = function (message) {
sendSimpleTextMessage(message.chat.id, "Funzione ancora da implementare");
}
var scusu = function (message) {
sendSimpleTextMessage(message.chat.id, "Scusu Cusu");
}
var foto = function (message) {
fs.readdir('images/', function (err, files){
if (err) {console.log("Error in reading images/ directory"); return false;}
var answer = {
chat_id: message.chat.id,
photo: { file: 'images/'+files[randomInBetween(0, files.length-1)], content_type: 'multipart/form-data'}
};
needle.post(SEND_PHOTO_URL, answer, {multipart: true}, function (err, resp, body) {
if (err) {console.log("Errore"); return false; }
});
});
}
var COMMANDS = {
"start" : start,
"help" : help,
"dosth" : dosth,
"yesno" : yesno,
"admin" : admin,
"esami" : esami,
"citazioni" : citazioni,
"esercizi" : esercizi,
"scusu" : scusu,
"foto" : foto
};
function poll(offset) {
var url = POLLING_URL.replace(":offset:", offset);
console.log("Polling for new messages...");
needle.get(url, function(err, response, body) {
if (!err && response) {
if (response.statusCode == 200) {
var result = body.result;
console.log("Got "+ result.length +" messages");
if (result.length > 0) {
for (i in result) {
if (runCommand(result[i].message)) continue;
}
max_offset = parseInt(result[result.length - 1].update_id) + 1; //update max offset
}
} else {
console.log("Bad response, status = " + response.statusCode);
}
} else {
console.log("Timed out");
}
poll(typeof max_offset === 'undefined' ? 0 : max_offset);
});
}
function runCommand(message) {
var msgtext = message.text;
if (msgtext.indexOf("/") != 0) return false; //no slash at beginning
var command = msgtext.substring(1, msgtext.indexOf(" ") == -1 ? msgtext.length : msgtext.indexOf(" "));
if (COMMANDS[command] == null) { // not a valid command
sendSimpleTextMessage(message.chat.id, "Comando non valido\nPer una lista dei comandi digita /help");
return false;
}
COMMANDS[command](message);
return true;
}
console.log("Bot started...");
needle.defaults({
open_timeout: 60000,
user_agent: 'MyBot/1.0'});
poll(0);