added base implementation of the telegram bots

This commit is contained in:
Andrei Yankovich 2023-09-03 20:49:42 +02:00
parent 8dafa0097c
commit c517fa3499
14 changed files with 267 additions and 53 deletions

View File

@ -4,11 +4,11 @@
"src/build/Debug/qTbotEaxample.exe" "src/build/Debug/qTbotEaxample.exe"
], ],
"clear": true, "clear": true,
"binPrefix": "/home/andrei/git/build-qTbot-Desktop_Qt_6_5_2_GCC_64bit-Debug", "binPrefix": "/home/andrei/gitHub/build-qTbot-Desktop_Qt_6_5_2_GCC_64bit-Debug",
"libDir": [ "libDir": [
"/home/andrei/git/qTbot", "/media/D/builds/qTbot",
"/home/andrei/Qt/6.5.2/gcc_64", "/home/andrei/Qt/6.5.2/gcc_64",
"/home/andrei/git/build-qTbot-Desktop_Qt_6_5_2_GCC_64bit-Debug" "/home/andrei/gitHub/build-qTbot-Desktop_Qt_6_5_2_GCC_64bit-Debug"
], ],
"recursiveDepth": "10", "recursiveDepth": "10",
"deploySystem": false, "deploySystem": false,
@ -17,11 +17,11 @@
"qif": true, "qif": true,
"zip": true, "zip": true,
"ignoreEnv": [ "ignoreEnv": [
"/home/andrei/git/qTbot/Distro" "/media/D/builds/qTbot/Distro"
], ],
"extraLib": "crypto", "extraLib": "crypto",
"targetDir": "/home/andrei/git/qTbot/Distro", "targetDir": "/media/D/builds/qTbot/Distro",
"deployVersion": "0.5.37c5d19", "deployVersion": "0.6.8dafa00",
} }

View File

@ -10,7 +10,7 @@
#include <QtCore/qglobal.h> #include <QtCore/qglobal.h>
#define QTBOT_VERSION "0.5.37c5d19" #define QTBOT_VERSION "0.6.8dafa00"
#if defined(QTBOT_LIBRARY) #if defined(QTBOT_LIBRARY)
# define QTBOT_EXPORT Q_DECL_EXPORT # define QTBOT_EXPORT Q_DECL_EXPORT

View File

@ -72,7 +72,7 @@ signals:
/** /**
* @brief receiveMessage emit when but receive any updates from users. * @brief receiveMessage emit when but receive any updates from users.
*/ */
void receiveMessage(QSharedPointer<iMessage> ); void receiveMessage(const QSharedPointer<iMessage>& );
}; };

View File

@ -6,26 +6,69 @@
//# //#
#include "itelegrambot.h" #include "itelegrambot.h"
#include <QNetworkAccessManager>
#include <qTbot/messages/telegramgetmsg.h> #include <qTbot/messages/telegramgetmsg.h>
#include <QNetworkReply>
#include <QSharedPointer>
namespace qTbot { namespace qTbot {
ITelegramBot::ITelegramBot() { ITelegramBot::ITelegramBot() {
_manager = new QNetworkAccessManager();
}
ITelegramBot::~ITelegramBot() {
delete _manager;
} }
bool ITelegramBot::login(const QByteArray &token) { bool ITelegramBot::login(const QByteArray &token) {
if (token.isEmpty()) {
return false;
}
setToken(token); setToken(token);
TelegramGetMsg msg; return sendMessage(QSharedPointer<TelegramGetMsg>::create());
auto getInfoRquest = makePrefix() + msg.makeUpload(); }
bool ITelegramBot::sendMessage(const QSharedPointer<iMessage> &message) {
if (!message)
return false;
auto getInfoRquest = makePrefix() + message->makeUpload();
QNetworkReply* networkReplay = _manager->get(QNetworkRequest(QUrl::fromEncoded(getInfoRquest)));
if (!networkReplay)
return false;
connect(networkReplay, &QNetworkReply::finished,
this, &ITelegramBot::handleReplayIsFinished,
Qt::QueuedConnection);
return true;
} }
QByteArray ITelegramBot::makePrefix() const { QByteArray ITelegramBot::makePrefix() const {
return ""; return "https://api.telegram.org/bot" + token();
}
void ITelegramBot::handleReplayIsFinished() {
if (QNetworkReply* replay = dynamic_cast<QNetworkReply*>(sender())) {
auto rawData = replay->readAll();
auto message = QSharedPointer<ITelegramMessage>::create();
message->setRawData(rawData);
replay->deleteLater();
onMessageReceived(message);
emit receiveMessage(message);
}
} }
} }

View File

@ -1,5 +1,5 @@
//# //#
//# Copyright (C) 2021-2023 QuasarApp. //# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying //# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies //# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed. //# of this license document, but changing it is not allowed.
@ -11,18 +11,27 @@
#define ITELEGRAMBOT_H #define ITELEGRAMBOT_H
#include "ibot.h" #include "ibot.h"
#include <QObject>
class QNetworkAccessManager;
class QNetworkReply;
namespace qTbot { namespace qTbot {
class ITelegramMessage;
/** /**
* @brief The ITelegramBot class This is base implementation of the all telegramm bots. * @brief The ITelegramBot class This is base implementation of the all telegramm bots.
*/ */
class QTBOT_EXPORT ITelegramBot : public IBot class QTBOT_EXPORT ITelegramBot : public QObject, public IBot
{ {
Q_OBJECT
public: public:
ITelegramBot(); ITelegramBot();
~ITelegramBot();
bool login(const QByteArray &token) override; bool login(const QByteArray &token) override;
bool sendMessage(const QSharedPointer<iMessage> &message) override;
protected: protected:
/** /**
@ -31,8 +40,20 @@ protected:
*/ */
QByteArray makePrefix() const; QByteArray makePrefix() const;
/**
* @brief onMessageReceived This method will be invoked every time when network rplays will be finished.
* @param replay This is ansver of the server.
*/
virtual void onMessageReceived(const QSharedPointer<ITelegramMessage>& replay) = 0;
private slots:
void handleReplayIsFinished();
signals:
void receiveMessage(const QSharedPointer<iMessage>& );
private: private:
QNetworkReques request; QNetworkAccessManager *_manager = nullptr;
}; };
} }

View File

@ -11,22 +11,22 @@
namespace qTbot { namespace qTbot {
ItelegramMessage::ItelegramMessage():iMessage(){} ITelegramMessage::ITelegramMessage():iMessage(){}
QByteArray ItelegramMessage::makeUpload() const {
QByteArray ITelegramMessage::makeUpload() const {
return QJsonDocument(_rawJson).toJson(QJsonDocument::Compact);
} }
const QJsonObject &ItelegramMessage::rawJson() const { const QJsonObject &ITelegramMessage::rawJson() const {
return _rawJson; return _rawJson;
} }
void ItelegramMessage::setRawJson(const QJsonObject &newRawJson) { void ITelegramMessage::setRawJson(const QJsonObject &newRawJson) {
_rawJson = newRawJson; _rawJson = newRawJson;
} }
void ItelegramMessage::setRawData(const QByteArray &newRawData) { void ITelegramMessage::setRawData(const QByteArray &newRawData) {
setRawData(newRawData); iMessage::setRawData(newRawData);
auto doc = QJsonDocument::fromJson(newRawData); auto doc = QJsonDocument::fromJson(newRawData);
if (!doc.isObject()) { if (!doc.isObject()) {
@ -35,4 +35,9 @@ void ItelegramMessage::setRawData(const QByteArray &newRawData) {
setRawJson(doc.object()); setRawJson(doc.object());
} }
bool ITelegramMessage::isValid() const {
return !_rawJson.isEmpty();
}
} }

View File

@ -9,23 +9,5 @@
namespace qTbot { namespace qTbot {
TelegramGetMsg::TelegramGetMsg() { TelegramGetMsg::TelegramGetMsg():TelegramSingleRquest("getMe") {}
}
QByteArray TelegramGetMsg::makeUpload() const {
return "/get";
}
bool TelegramGetMsg::isValid() const {
return true;
}
void TelegramGetMsg::setRawData(const QByteArray &) {
return;
}
void TelegramGetMsg::setRawJson(const QJsonObject &) {
return;
}
} }

View File

@ -8,21 +8,16 @@
#ifndef TELEGRAMGETMSG_H #ifndef TELEGRAMGETMSG_H
#define TELEGRAMGETMSG_H #define TELEGRAMGETMSG_H
#include "qTbot/itelegrammessage.h" #include "qTbot/messages/telegramsinglerquest.h"
namespace qTbot { namespace qTbot {
/** /**
* @brief The TelegramGetMsg class just prepare get request to tellegram * @brief The TelegramGetMsg class just prepare get request to tellegram
*/ */
class QTBOT_EXPORT TelegramGetMsg: public ITelegramMessage class QTBOT_EXPORT TelegramGetMsg final: public TelegramSingleRquest
{ {
public: public:
TelegramGetMsg(); TelegramGetMsg();
QByteArray makeUpload() const override;
bool isValid() const override;
void setRawData(const QByteArray &newRawData) override;
void setRawJson(const QJsonObject &newRawJson) override;
}; };
} }
#endif // TELEGRAMGETMSG_H #endif // TELEGRAMGETMSG_H

View File

@ -0,0 +1,15 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "telegramgetupdate.h"
namespace qTbot {
TelegramGetUpdate::TelegramGetUpdate(): TelegramSingleRquest("getUpdates"){}
}

View File

@ -0,0 +1,25 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef TELEGRAMGETUPDATE_H
#define TELEGRAMGETUPDATE_H
#include "qTbot/messages/telegramsinglerquest.h"
namespace qTbot {
/**
* @brief The TelegramGetUpdate class This is implementation of the get update method of the Telegram API
*/
class TelegramGetUpdate final: public TelegramSingleRquest
{
public:
TelegramGetUpdate();
};
}
#endif // TELEGRAMGETUPDATE_H

View File

@ -0,0 +1,35 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#include "telegramsinglerquest.h"
namespace qTbot {
TelegramSingleRquest::TelegramSingleRquest(const QByteArray &request) {
_request = request;
}
void TelegramSingleRquest::setRawData(const QByteArray &) {
return;
}
QByteArray TelegramSingleRquest::makeUpload() const {
return "/" + _request;
}
bool TelegramSingleRquest::isValid() const {
return _request.size();
}
void TelegramSingleRquest::setRawJson(const QJsonObject &) {
return;
}
}

View File

@ -0,0 +1,48 @@
//#
//# Copyright (C) 2023-2023 QuasarApp.
//# Distributed under the GPLv3 software license, see the accompanying
//# Everyone is permitted to copy and distribute verbatim copies
//# of this license document, but changing it is not allowed.
//#
#ifndef TELEGRAMSINGLERQUEST_H
#define TELEGRAMSINGLERQUEST_H
#include <qTbot/itelegrammessage.h>
namespace qTbot {
/**
* @brief The TelegramSingleRquest class Is base class for all single requests commands.
*
* Example:
* create a single request to telegram server.
*
* @code{cpp}
#include "qTbot/messages/telegramsinglerquest.h"
class QTBOT_EXPORT TelegramGetMsg final: public TelegramSingleRquest
{
public:
TelegramGetMsg();
};
TelegramGetMsg::TelegramGetMsg():TelegramSingleRquest("getMe") {}
* @endcode
*/
class QTBOT_EXPORT TelegramSingleRquest: public ITelegramMessage
{
public:
TelegramSingleRquest(const QByteArray& request);
void setRawData(const QByteArray &newRawData) override final;
QByteArray makeUpload() const override final;
bool isValid() const override final;
void setRawJson(const QJsonObject &newRawJson) override final;
private:
QByteArray _request;
};
}
#endif // TELEGRAMSINGLERQUEST_H

View File

@ -7,18 +7,41 @@
#include "telegramrestbot.h" #include "telegramrestbot.h"
#include <qTbot/messages/telegramgetupdate.h>
#include <QTimer>
namespace qTbot { namespace qTbot {
TelegramRestBot::TelegramRestBot() TelegramRestBot::TelegramRestBot() {
{ _timer = new QTimer();
_timer->start(1000);
connect(_timer, &QTimer::timeout, this, &TelegramRestBot::handleTimeOut,
Qt::QueuedConnection);
}
TelegramRestBot::~TelegramRestBot() {
delete _timer;
} }
bool TelegramRestBot::login(const QByteArray &token) { bool TelegramRestBot::login(const QByteArray &token) {
ITelegramBot::login(token); if (!ITelegramBot::login(token)) {
return false;
}
return true;
} }
bool TelegramRestBot::sendMessage(const QSharedPointer<iMessage> &message) { int TelegramRestBot::interval() const {
return _timer->interval();
}
void TelegramRestBot::setInterval(int newInterval) {
_timer->setInterval(newInterval);
}
void TelegramRestBot::handleTimeOut() {
sendMessage(QSharedPointer<TelegramGetUpdate>::create());
}
} }
}

View File

@ -11,6 +11,8 @@
#include "itelegrambot.h" #include "itelegrambot.h"
class QTimer;
namespace qTbot { namespace qTbot {
/** /**
@ -18,12 +20,32 @@ namespace qTbot {
*/ */
class QTBOT_EXPORT TelegramRestBot: public ITelegramBot class QTBOT_EXPORT TelegramRestBot: public ITelegramBot
{ {
Q_OBJECT
public: public:
TelegramRestBot(); TelegramRestBot();
~TelegramRestBot();
// IBot interface // IBot interface
bool login(const QByteArray &token); bool login(const QByteArray &token);
bool sendMessage(const QSharedPointer<iMessage> &message);
/**
* @brief interval This is interval "how often bot will be check updates on the telegram server" By defaul is 1 second.
* @return interval of the updates.
*/
int interval() const;
/**
* @brief setInterval This method sets new value for the TelegramRestBot::interval property.
* @param newInterval This is new value of the TelegramRestBot::interval property.
*/
void setInterval(int newInterval);
private slots:
void handleTimeOut();
private:
QTimer *_timer = nullptr;
}; };
} }
#endif // TELEGRAMRESTBOT_H #endif // TELEGRAMRESTBOT_H