80 lines
2.0 KiB
C++
80 lines
2.0 KiB
C++
#include "telegrambotbase.h"
|
|
#include <QDebug>
|
|
#include <QCoreApplication>
|
|
|
|
TelegramBotBase::TelegramBotBase(QObject *parent) : QObject(parent) {
|
|
// connect(&api, SIGNAL(newMessage(TelegramBotAPI::Message)), this, SLOT(processMessage(TelegramBotAPI::Message)), Qt::QueuedConnection);
|
|
connect(&api, SIGNAL(newMessage(TelegramBotAPI::Message)), this, SLOT(processMessage(TelegramBotAPI::Message)));
|
|
}
|
|
|
|
|
|
void TelegramBotBase::sendMessageToAll(const QString &message) {
|
|
QList<uint> keys = users.keys();
|
|
foreach (uint cid, keys) {
|
|
if (users[cid].logged_in)
|
|
api.sendMessage(cid, message);
|
|
}
|
|
}
|
|
|
|
|
|
void TelegramBotBase::processMessage(TelegramBotAPI::Message msg) {
|
|
// QApplication::processEvents();
|
|
qDebug() << "[TelegramBotBase]" << msg.text;
|
|
uint cid = msg.chat_id;
|
|
User m;
|
|
m.user_id = msg.user_id;
|
|
m.user_name = msg.user_name;
|
|
m.last_timestamp = msg.time;
|
|
if (!users.contains(cid)) users[cid] = m;
|
|
if (users[cid].logged_in) {
|
|
if (msg.text == "/help") {
|
|
api.sendMessage(cid, help());
|
|
return;
|
|
}
|
|
messageFromUser(cid, msg.text);
|
|
} else {
|
|
if (loginUser(cid, msg.text)) {
|
|
users[cid].logged_in = true;
|
|
api.sendMessage(cid, welcome(cid));
|
|
newUser(cid, users[cid]);
|
|
} else {
|
|
api.sendMessage(cid, loginMessage(cid));
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
QString TelegramBotBase::welcome(uint id) {
|
|
User user = getUser(id);
|
|
return tr("Hello %1! I am @%2.").arg(user.user_name).arg(api.botName());
|
|
}
|
|
|
|
|
|
QString TelegramBotBase::loginMessage(uint) {
|
|
return tr("Please send me /start");
|
|
}
|
|
|
|
|
|
QString TelegramBotBase::help() {
|
|
return tr("Sorry, /help not implemented in @%1.").arg(api.botName());
|
|
}
|
|
|
|
|
|
bool TelegramBotBase::loginUser(uint , const QString &msg) {
|
|
return (msg == "/start");
|
|
}
|
|
|
|
|
|
void TelegramBotBase::messageFromUser(uint id, const QString &msg) {
|
|
if (msg.toLower() == "ping") api.sendMessage(id, "pong");
|
|
}
|
|
|
|
|
|
void TelegramBotBase::newUser(uint, User) {}
|
|
|
|
void TelegramBotBase::disconnectUser(uint id) {
|
|
users.remove(id);
|
|
}
|
|
|