mirror of
https://github.com/QuasarApp/qTbot.git
synced 2025-05-06 18:29:37 +00:00
added base implementation of the telegram bots
This commit is contained in:
parent
8dafa0097c
commit
c517fa3499
@ -4,11 +4,11 @@
|
||||
"src/build/Debug/qTbotEaxample.exe"
|
||||
],
|
||||
"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": [
|
||||
"/home/andrei/git/qTbot",
|
||||
"/media/D/builds/qTbot",
|
||||
"/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",
|
||||
"deploySystem": false,
|
||||
@ -17,11 +17,11 @@
|
||||
"qif": true,
|
||||
"zip": true,
|
||||
"ignoreEnv": [
|
||||
"/home/andrei/git/qTbot/Distro"
|
||||
"/media/D/builds/qTbot/Distro"
|
||||
],
|
||||
"extraLib": "crypto",
|
||||
"targetDir": "/home/andrei/git/qTbot/Distro",
|
||||
"deployVersion": "0.5.37c5d19",
|
||||
"targetDir": "/media/D/builds/qTbot/Distro",
|
||||
"deployVersion": "0.6.8dafa00",
|
||||
|
||||
}
|
||||
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#define QTBOT_VERSION "0.5.37c5d19"
|
||||
#define QTBOT_VERSION "0.6.8dafa00"
|
||||
|
||||
#if defined(QTBOT_LIBRARY)
|
||||
# define QTBOT_EXPORT Q_DECL_EXPORT
|
||||
|
@ -72,7 +72,7 @@ signals:
|
||||
/**
|
||||
* @brief receiveMessage emit when but receive any updates from users.
|
||||
*/
|
||||
void receiveMessage(QSharedPointer<iMessage> );
|
||||
void receiveMessage(const QSharedPointer<iMessage>& );
|
||||
|
||||
};
|
||||
|
||||
|
@ -6,26 +6,69 @@
|
||||
//#
|
||||
|
||||
#include "itelegrambot.h"
|
||||
#include <QNetworkAccessManager>
|
||||
|
||||
#include <qTbot/messages/telegramgetmsg.h>
|
||||
|
||||
#include <QNetworkReply>
|
||||
#include <QSharedPointer>
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
ITelegramBot::ITelegramBot() {
|
||||
_manager = new QNetworkAccessManager();
|
||||
}
|
||||
|
||||
ITelegramBot::~ITelegramBot() {
|
||||
delete _manager;
|
||||
}
|
||||
|
||||
bool ITelegramBot::login(const QByteArray &token) {
|
||||
if (token.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
setToken(token);
|
||||
|
||||
TelegramGetMsg msg;
|
||||
auto getInfoRquest = makePrefix() + msg.makeUpload();
|
||||
return sendMessage(QSharedPointer<TelegramGetMsg>::create());
|
||||
}
|
||||
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
//#
|
||||
//# Copyright (C) 2021-2023 QuasarApp.
|
||||
//# 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.
|
||||
@ -11,18 +11,27 @@
|
||||
#define ITELEGRAMBOT_H
|
||||
|
||||
#include "ibot.h"
|
||||
#include <QObject>
|
||||
|
||||
class QNetworkAccessManager;
|
||||
class QNetworkReply;
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
class ITelegramMessage;
|
||||
|
||||
/**
|
||||
* @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:
|
||||
ITelegramBot();
|
||||
~ITelegramBot();
|
||||
|
||||
bool login(const QByteArray &token) override;
|
||||
bool sendMessage(const QSharedPointer<iMessage> &message) override;
|
||||
|
||||
protected:
|
||||
/**
|
||||
@ -31,8 +40,20 @@ protected:
|
||||
*/
|
||||
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:
|
||||
QNetworkReques request;
|
||||
QNetworkAccessManager *_manager = nullptr;
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -11,22 +11,22 @@
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
ItelegramMessage::ItelegramMessage():iMessage(){}
|
||||
|
||||
QByteArray ItelegramMessage::makeUpload() const {
|
||||
ITelegramMessage::ITelegramMessage():iMessage(){}
|
||||
|
||||
QByteArray ITelegramMessage::makeUpload() const {
|
||||
return QJsonDocument(_rawJson).toJson(QJsonDocument::Compact);
|
||||
}
|
||||
|
||||
const QJsonObject &ItelegramMessage::rawJson() const {
|
||||
const QJsonObject &ITelegramMessage::rawJson() const {
|
||||
return _rawJson;
|
||||
}
|
||||
|
||||
void ItelegramMessage::setRawJson(const QJsonObject &newRawJson) {
|
||||
void ITelegramMessage::setRawJson(const QJsonObject &newRawJson) {
|
||||
_rawJson = newRawJson;
|
||||
}
|
||||
|
||||
void ItelegramMessage::setRawData(const QByteArray &newRawData) {
|
||||
setRawData(newRawData);
|
||||
void ITelegramMessage::setRawData(const QByteArray &newRawData) {
|
||||
iMessage::setRawData(newRawData);
|
||||
|
||||
auto doc = QJsonDocument::fromJson(newRawData);
|
||||
if (!doc.isObject()) {
|
||||
@ -35,4 +35,9 @@ void ItelegramMessage::setRawData(const QByteArray &newRawData) {
|
||||
setRawJson(doc.object());
|
||||
|
||||
}
|
||||
|
||||
bool ITelegramMessage::isValid() const {
|
||||
return !_rawJson.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,23 +9,5 @@
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
TelegramGetMsg::TelegramGetMsg() {
|
||||
|
||||
}
|
||||
|
||||
QByteArray TelegramGetMsg::makeUpload() const {
|
||||
return "/get";
|
||||
}
|
||||
|
||||
bool TelegramGetMsg::isValid() const {
|
||||
return true;
|
||||
}
|
||||
|
||||
void TelegramGetMsg::setRawData(const QByteArray &) {
|
||||
return;
|
||||
}
|
||||
|
||||
void TelegramGetMsg::setRawJson(const QJsonObject &) {
|
||||
return;
|
||||
}
|
||||
TelegramGetMsg::TelegramGetMsg():TelegramSingleRquest("getMe") {}
|
||||
}
|
||||
|
@ -8,21 +8,16 @@
|
||||
#ifndef TELEGRAMGETMSG_H
|
||||
#define TELEGRAMGETMSG_H
|
||||
|
||||
#include "qTbot/itelegrammessage.h"
|
||||
#include "qTbot/messages/telegramsinglerquest.h"
|
||||
namespace qTbot {
|
||||
|
||||
/**
|
||||
* @brief The TelegramGetMsg class just prepare get request to tellegram
|
||||
*/
|
||||
class QTBOT_EXPORT TelegramGetMsg: public ITelegramMessage
|
||||
class QTBOT_EXPORT TelegramGetMsg final: public TelegramSingleRquest
|
||||
{
|
||||
public:
|
||||
TelegramGetMsg();
|
||||
|
||||
QByteArray makeUpload() const override;
|
||||
bool isValid() const override;
|
||||
void setRawData(const QByteArray &newRawData) override;
|
||||
void setRawJson(const QJsonObject &newRawJson) override;
|
||||
};
|
||||
}
|
||||
#endif // TELEGRAMGETMSG_H
|
||||
|
15
src/qTbot/src/public/qTbot/messages/telegramgetupdate.cpp
Normal file
15
src/qTbot/src/public/qTbot/messages/telegramgetupdate.cpp
Normal 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"){}
|
||||
|
||||
}
|
25
src/qTbot/src/public/qTbot/messages/telegramgetupdate.h
Normal file
25
src/qTbot/src/public/qTbot/messages/telegramgetupdate.h
Normal 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
|
35
src/qTbot/src/public/qTbot/messages/telegramsinglerquest.cpp
Normal file
35
src/qTbot/src/public/qTbot/messages/telegramsinglerquest.cpp
Normal 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;
|
||||
|
||||
}
|
||||
|
||||
}
|
48
src/qTbot/src/public/qTbot/messages/telegramsinglerquest.h
Normal file
48
src/qTbot/src/public/qTbot/messages/telegramsinglerquest.h
Normal 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
|
@ -7,18 +7,41 @@
|
||||
|
||||
#include "telegramrestbot.h"
|
||||
|
||||
#include <qTbot/messages/telegramgetupdate.h>
|
||||
|
||||
#include <QTimer>
|
||||
|
||||
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) {
|
||||
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());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,6 +11,8 @@
|
||||
|
||||
#include "itelegrambot.h"
|
||||
|
||||
class QTimer;
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
/**
|
||||
@ -18,12 +20,32 @@ namespace qTbot {
|
||||
*/
|
||||
class QTBOT_EXPORT TelegramRestBot: public ITelegramBot
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
TelegramRestBot();
|
||||
~TelegramRestBot();
|
||||
|
||||
// IBot interface
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user