-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegramhelper.cpp
608 lines (495 loc) · 19.2 KB
/
telegramhelper.cpp
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
#include "telegramhelper.h"
#include <QDebug>
#include <QDateTime>
#include <QDir>
#include <QEventLoop>
#include <QtNetwork/QNetworkReply>
#include <QUrlQuery>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
TelegramHelper::TelegramHelper(const QString& vToken, const QString &vStorageFolder, const RoutesHandler& vRoutesh):
token(vToken),
storageFolder(vStorageFolder),
routesh(vRoutesh),
lastUpdateId(0)
{
}
void TelegramHelper::checkUpdates()
{
qDebug() << "Called" << __FUNCTION__;
//compose url to check new messages
QUrl toCall("https://api.telegram.org");
QUrlQuery query;
toCall.setPath(QString("/bot%1/getUpdates").arg(token));
if(lastUpdateId)
query.addQueryItem("offset", QString::number(lastUpdateId + 1));
toCall.setQuery(query);
//ask the server
QByteArray recvJson = makeHttpGet(toCall);
//if there is something, parse the update
if(!recvJson.isEmpty())
parseGetUpdates(recvJson);
}
bool TelegramHelper::parseGetUpdates(const QByteArray &payload)
{
bool messageIsOk(false);
//decode json
QJsonDocument d = QJsonDocument::fromJson( payload );
if(!d.isNull())
{
QJsonObject root = d.object();
messageIsOk = (root.contains("ok") && root["ok"].toBool() == true);
if(messageIsOk && root.contains("result")) {
//get result array, that contains our messages
QJsonArray arrResult = root["result"].toArray();
for(auto aRes: arrResult){
QJsonObject updateObj = aRes.toObject();
QString httpPath; //url to call for sending reply
QJsonDocument jsonDoc; //body of the reply
//for each message get a properly reply
bool allOk = parseUpdate(updateObj, httpPath, jsonDoc);
if(allOk) {
if(!httpPath.isEmpty()) {
//if there is something, send reply to the user
if(replyOnChat(httpPath, jsonDoc))
qDebug() << "Delivered ok";
}
}
}
}
}
return messageIsOk;
}
bool TelegramHelper::parseUpdate(QJsonObject &updateObj, QString &httpPath, QJsonDocument &jsonDoc)
{
bool allOk(true);
//update update_id for next call
int update_id = updateObj["update_id"].toInt();
if(update_id > lastUpdateId) lastUpdateId = update_id;
//we can receive different type of messages
if(updateObj.contains("message"))
allOk = parseMessage( updateObj["message"].toObject(), httpPath, jsonDoc);
if(updateObj.contains("inline_query"))
allOk = parseInlineQuery( updateObj["inline_query"].toObject(), httpPath, jsonDoc );
if(updateObj.contains("callback_query"))
allOk = parseCallbackQuery( updateObj["callback_query"].toObject(), httpPath, jsonDoc );
if(updateObj.contains("poll_answer"))
allOk = parsePollAnswer(updateObj["poll_answer"].toObject(), httpPath, jsonDoc);
if(updateObj.contains("poll"))
allOk = parsePoll(updateObj["poll"].toObject(), httpPath, jsonDoc);
return allOk;
}
bool TelegramHelper::parseMessage(const QJsonObject &messageObj, QString &httpPath, QJsonDocument &jsonDoc)
{
bool allOk(true);
if(messageObj.contains("chat")) {
//get user and chat parameters
QString messageText = messageObj["text"].toString();
QJsonObject chatObj = messageObj["chat"].toObject();
int chatId = chatObj["id"].toInt();
QString first_name = chatObj["first_name"].toString();
if(first_name.isEmpty()) first_name = chatObj["title"].toString();
qDebug() << "Message from" << first_name;
bool doReplyToChat(true);
//check if it is a command (starts with slash)
if(messageObj.contains("entities")) {
QJsonArray nttsArr = messageObj["entities"].toArray();
for(auto curNtt: nttsArr){
QJsonObject currNttObj = curNtt.toObject();
if(currNttObj.contains("type") && currNttObj["type"].toString().compare("bot_command") == 0){
allOk = parseCommand(chatId, first_name, messageText, doReplyToChat, httpPath, jsonDoc);
}
}
}
//if no command handled compose a reply
if(doReplyToChat) {
QDateTime messageTime = QDateTime::fromTime_t( uint(messageObj["date"].toInt()) );
QString toSendText;
if(messageObj.contains("contact")) {
//received user contact informations
QJsonObject contactObj = messageObj["contact"].toObject();
toSendText = QString("Received contact: %1, from: %2, at %3")
.arg(contactObj["phone_number"].toString())
.arg(first_name)
.arg(messageTime.toString());
}
else if(messageObj.contains("location")) {
//received user location
QJsonObject locationObj = messageObj["location"].toObject();
toSendText = QString("Received location: %1, %2, from: %3")
.arg(locationObj["latitude"].toDouble(),6,'f')
.arg(locationObj["longitude"].toDouble(),6,'f')
.arg(first_name);
}
else if(messageObj.contains("new_chat_participant")) {
//received notification that new user is added to a group
QJsonObject new_chat_participantObj = messageObj["new_chat_participant"].toObject();
toSendText = QString("Benvenuto: %1").arg(new_chat_participantObj["first_name"].toString());
}
else {
//simple text message, reply with same message
toSendText = QString("Received: %1, from: %2, at %3")
.arg(messageText)
.arg(first_name)
.arg(messageTime.toString());
}
//if there is some text to send, compose message payload
if(!toSendText.isEmpty())
allOk = makeMessage(chatId, toSendText, QJsonObject(), httpPath, jsonDoc);
}
}
return allOk;
}
bool TelegramHelper::parseInlineQuery(const QJsonObject &messageObj, QString &httpPath, QJsonDocument &jsonDoc)
{
bool allOk(false);
if(messageObj.contains("from")) {
QString queryText = messageObj["query"].toString();
QString queryId = messageObj["id"].toString();
QJsonObject fromObj = messageObj["from"].toObject();
//int chatId = fromObj["id"].toInt();
QString first_name = fromObj["first_name"].toString();
qDebug() << "Query from" << first_name;
QVector<QString> allResults;
allResults.push_back("casa al mare");
allResults.push_back("casa in montagna");
allResults.push_back("casa in collina");
allResults.push_back("hotel al mare");
allResults.push_back("hotel in montagna");
allResults.push_back("hotel in collina");
QJsonArray articles;
for(int i=0; i<allResults.size(); i++){
if(!queryText.isEmpty()){
if(!allResults[i].contains( queryText ,Qt::CaseInsensitive))
continue;
}
QJsonObject mess;
mess["message_text"] = allResults[i];
QJsonObject aSingleResult;
aSingleResult["type"] = "article";
aSingleResult["id"] = QString::number(i);
aSingleResult["title"] = QString("Titolo: %1").arg(allResults[i]);
aSingleResult["description"] = QString("Descr: %1").arg(allResults[i]);
aSingleResult["input_message_content"] = mess;
articles.append(aSingleResult);
}
allOk = makeInlineQueryResult(queryId, articles, httpPath, jsonDoc);
}
return allOk;
}
bool TelegramHelper::parseCallbackQuery(const QJsonObject &messageObj, QString &httpPath, QJsonDocument &jsonDoc)
{
bool allOk(false);
if(messageObj.contains("from")) {
QString callbackId = messageObj["id"].toString();
QString callbackData = messageObj["data"].toString();
QJsonObject fromObj = messageObj["from"].toObject();
//int chatId = fromObj["id"].toInt();
QString first_name = fromObj["first_name"].toString();
qDebug() << "Callback" << callbackData << "from" << first_name;
QString messageText = QString("You sent: %1").arg(callbackData);
allOk = makeAnswerCallbackQuery(callbackId, messageText, httpPath, jsonDoc);
}
return allOk;
}
bool TelegramHelper::parsePollAnswer(const QJsonObject &messageObj, QString & /*httpPath*/, QJsonDocument & /*jsonDoc*/)
{
bool allOk(false);
if(messageObj.contains("user")) {
QString first_name = messageObj["user"]["first_name"].toString();
QJsonArray option_ids = messageObj["option_ids"].toArray();
qDebug() << "Poll answer from" << first_name << "options:";
for(const auto curOpt: option_ids){
qDebug() << curOpt.toInt();
}
allOk = true;
}
return allOk;
}
bool TelegramHelper::parsePoll(const QJsonObject &messageObj, QString & /*httpPath*/, QJsonDocument & /*jsonDoc*/)
{
bool allOk(false);
if(messageObj.contains("question")) {
QString question = messageObj["question"].toString();
QJsonArray options = messageObj["options"].toArray();
qDebug() << "Poll " << question << "summary:";
for(const auto curOpt: options){
QJsonObject curOptObj = curOpt.toObject();
qDebug() << curOptObj["text"].toString() << curOptObj["voter_count"].toInt() << "votes";
}
allOk = true;
}
return allOk;
}
bool TelegramHelper::parseCommand(int chatId, const QString &first_name, const QString &messageText, bool &unknownCommand, QString &httpPath, QJsonDocument &jsonDoc)
{
bool allOk(true);
unknownCommand = false;
if(messageText.startsWith("/start")) {
//first time I see this user
QString toSendText = QString("%1 welcome to BignoDevBot").arg(first_name);
if(messageText.length() > 6){
//strip text after /start, the parameter and store it
QString userParam = messageText.mid(6).trimmed();
setChatParameter(chatId, userParam);
if(!userParam.isEmpty()) {
toSendText.append("\nWith parameter: ");
toSendText.append(userParam);
}
}
allOk = makeMessage(chatId, toSendText, QJsonObject(), httpPath, jsonDoc);
}
else if(messageText.startsWith("/help")) {
//ask for help
QString toSendText = "Help message";
if(messageText.length() > 6) {
//strip text after /help
toSendText.append(" on topic: ");
toSendText.append(messageText.mid(6));
}
//if this user has a starting parameter display it
QString userParam = getChatParameter(chatId, "");
if(!userParam.isEmpty()) {
toSendText.append("\nWith parameter: ");
toSendText.append(userParam);
}
allOk = makeMessage(chatId, toSendText, QJsonObject(), httpPath, jsonDoc);
}
else if(messageText.startsWith("/gatto")) {
//send a cat to the user
QString toSendPic = "https://thiscatdoesnotexist.com/";
allOk = makePhoto(chatId, toSendPic, httpPath, jsonDoc);
}
else if(messageText.startsWith("/posizione")) {
//send a position to the user
QString userParam = getChatParameter(chatId, "");
if(!userParam.isEmpty()) {
int routeId = userParam.replace("route", "").toInt();
QPointF toSendPos(0,0);
bool toSendPosReady = routesh.getPos(routeId, toSendPos);
if(toSendPosReady) {
allOk = makePosition(chatId, toSendPos, httpPath, jsonDoc);
}
else {
QString toSendText = "Error fetching position";
allOk = makeMessage(chatId, toSendText, QJsonObject(), httpPath, jsonDoc);
}
}
else {
QString toSendText = "Questa chat non è collegata ad alcuna sessione";
allOk = makeMessage(chatId, toSendText, QJsonObject(), httpPath, jsonDoc);
}
}
else if(messageText.startsWith("/contattami")) {
//ask for contact informations
QJsonObject replyMarkup;
QString toSendText = "Chiedo informazioni di contatto";
createContactKeyboard(replyMarkup);
allOk = makeMessage(chatId, toSendText, replyMarkup, httpPath, jsonDoc);
}
else if(messageText.startsWith("/domanda")) {
//ask for a question
QJsonObject replyMarkup;
QString toSendText = "Ti faccio una domanda";
createInlineKeyboard(replyMarkup);
allOk = makeMessage(chatId, toSendText, replyMarkup, httpPath, jsonDoc);
}
else if(messageText.startsWith("/sondaggio")) {
//make a poll
allOk = makePoll(chatId, httpPath, jsonDoc);
}
else
unknownCommand = true;
return allOk;
}
bool TelegramHelper::makeMessage(int chatId, const QString &message, const QJsonObject& replyMarkup, QString &httpPath, QJsonDocument &jsonDoc)
{
httpPath = QString("/bot%1/sendMessage").arg(token);
QJsonObject root;
root["method"] = "sendMessage";
root["chat_id"] = chatId;
root["text"] = message;
root["reply_markup"] = replyMarkup;
/*
QJsonObject forceReply;
forceReply["force_reply"] = true;
root["reply_markup"] = forceReply;
*/
jsonDoc.setObject(root);
return true;
}
bool TelegramHelper::makePhoto(int chatId, const QString &photoUrl, QString &httpPath, QJsonDocument &jsonDoc)
{
httpPath = QString("/bot%1/sendPhoto").arg(token);
QJsonObject root;
root["method"] = "sendPhoto";
root["chat_id"] = chatId;
root["photo"] = photoUrl;
jsonDoc.setObject(root);
return true;
}
bool TelegramHelper::makePosition(int chatId, const QPointF &thePosition, QString &httpPath, QJsonDocument &jsonDoc)
{
httpPath = QString("/bot%1/sendLocation").arg(token);
QJsonObject root;
root["method"] = "sendLocation";
root["chat_id"] = chatId;
root["latitude"] = thePosition.x();
root["longitude"] = thePosition.y();
jsonDoc.setObject(root);
return true;
}
bool TelegramHelper::makeInlineQueryResult(const QString &inlineQueryId, const QJsonArray &articles, QString &httpPath, QJsonDocument &jsonDoc)
{
httpPath = QString("/bot%1/answerInlineQuery").arg(token);
QJsonObject root;
root["method"] = "answerInlineQuery";
root["inline_query_id"] = inlineQueryId;
root["results"] = articles;
jsonDoc.setObject(root);
return true;
}
bool TelegramHelper::makeAnswerCallbackQuery(const QString& callbackQueryId, const QString &message, QString &httpPath, QJsonDocument &jsonDoc)
{
httpPath = QString("/bot%1/answerCallbackQuery").arg(token);
QJsonObject root;
root["method"] = "answerCallbackQuery";
root["callback_query_id"] = callbackQueryId;
root["text"] = message;
root["show_alert"] = true;
jsonDoc.setObject(root);
return true;
}
bool TelegramHelper::makePoll(int chatId, QString &httpPath, QJsonDocument &jsonDoc)
{
httpPath = QString("/bot%1/sendPoll").arg(token);
QJsonObject root;
root["method"] = "sendPoll";
root["chat_id"] = chatId;
root["question"] = "Ti piace il c++?";
root["is_anonymous"] = false;
QJsonArray optArray;
optArray.append("Si");
optArray.append("No");
optArray.append("Preferisco non rispondere");
root["options"] = optArray;
jsonDoc.setObject(root);
return true;
}
void TelegramHelper::createContactKeyboard(QJsonObject &replyMarkup)
{
QJsonArray arrRows;
QJsonArray arrButtons1;
QJsonArray arrButtons2;
{
QJsonObject curButton;
curButton["text"] = "Manda la mia posizione";
curButton["request_location"] = true;
curButton["request_contact"] = false;
arrButtons1.append(curButton);
}
{
QJsonObject curButton;
curButton["text"] = "Manda il mio contatto";
curButton["request_contact"] = true;
arrButtons1.append(curButton);
}
{
QJsonObject curButton;
curButton["text"] = "Manda qualcosa";
arrButtons2.append(curButton);
}
arrRows.append( arrButtons1 );
arrRows.append( arrButtons2 );
replyMarkup["keyboard"] = arrRows;
}
void TelegramHelper::createInlineKeyboard(QJsonObject &replyMarkup)
{
QJsonArray arrRows;
QJsonArray arrButtons;
{
QJsonObject curButton;
curButton["text"] = "Bottone 1";
curButton["callback_data"] = "data for bottone 1";
arrButtons.append(curButton);
}
{
QJsonObject curButton;
curButton["text"] = "Bottone 2";
curButton["callback_data"] = "other data";
arrButtons.append(curButton);
}
arrRows.append( arrButtons );
replyMarkup["inline_keyboard"] = arrRows;
}
QByteArray TelegramHelper::makeHttpGet(const QUrl &toCall)
{
QNetworkRequest request;
QEventLoop synchronous;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &synchronous, SLOT(quit()));
request.setUrl(toCall);
QNetworkReply *reply = manager.get(request);
synchronous.exec();
if (reply->error()) {
qDebug() << reply->errorString();
return QByteArray();
}
return reply->readAll();
}
QByteArray TelegramHelper::makeHttpPost(const QUrl &toCall, const QString &contentType, const QByteArray &toSend)
{
QNetworkRequest request;
QEventLoop synchronous;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &synchronous, SLOT(quit()));
request.setUrl(toCall);
request.setHeader(QNetworkRequest::ContentTypeHeader,QVariant(contentType));
QNetworkReply *reply = manager.post(request, toSend);
synchronous.exec();
if (reply->error()) {
qDebug() << reply->errorString();
return QByteArray();
}
return reply->readAll();
}
bool TelegramHelper::replyOnChat(const QString &httpPath, const QJsonDocument &jsonDoc)
{
//compose url
QUrl toCall("https://api.telegram.org");
toCall.setPath( httpPath );
//send jsonDoc to the server
QByteArray sended = makeHttpPost(toCall, "application/json", jsonDoc.toJson(QJsonDocument::Compact));
//if not empty it's ok (not very accurate)
return !sended.isEmpty();
}
bool TelegramHelper::setChatParameter(int chatId, const QString ¶mValue)
{
QDir sessions(storageFolder);
if(!sessions.exists())
qDebug() << "Folder:" << sessions.path() << "does not exists";
bool allOk(false);
QFile file(sessions.filePath(QString("chat_%1.dat").arg(chatId)));
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream stream(&file);
stream << paramValue;
file.close();
allOk = true;
}
return allOk;
}
QString TelegramHelper::getChatParameter(int chatId, const QString &defaultValue)
{
QDir sessions(storageFolder);
QString paramValue;
QFile file(sessions.filePath(QString("chat_%1.dat").arg(chatId)));
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream stream(&file);
paramValue = stream.readLine();
file.close();
}
if(paramValue.isEmpty())
paramValue = defaultValue;
return paramValue;
}