mirror of
https://github.com/QuasarApp/qTbot.git
synced 2025-05-09 11:49:36 +00:00
callback impl
This commit is contained in:
parent
c517fa3499
commit
c36164000c
src
@ -21,7 +21,7 @@
|
||||
],
|
||||
"extraLib": "crypto",
|
||||
"targetDir": "/media/D/builds/qTbot/Distro",
|
||||
"deployVersion": "0.6.8dafa00",
|
||||
"deployVersion": "0.7.c517fa3",
|
||||
|
||||
}
|
||||
|
||||
|
@ -5,6 +5,17 @@
|
||||
//# of this license document, but changing it is not allowed.
|
||||
//#
|
||||
|
||||
#include <qTbot/telegramrestbot.h>
|
||||
|
||||
#include <QCoreApplication>
|
||||
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
return 0;
|
||||
|
||||
QCoreApplication app(argc, argv);
|
||||
|
||||
qTbot::TelegramRestBot bot;
|
||||
|
||||
bot.login("6349356184:AAFotw9EC46sgAQrkGQ_jeHPyv3EAapZXcM");
|
||||
return app.exec();
|
||||
}
|
||||
|
@ -10,7 +10,7 @@
|
||||
|
||||
#include <QtCore/qglobal.h>
|
||||
|
||||
#define QTBOT_VERSION "0.6.8dafa00"
|
||||
#define QTBOT_VERSION "0.7.c517fa3"
|
||||
|
||||
#if defined(QTBOT_LIBRARY)
|
||||
# define QTBOT_EXPORT Q_DECL_EXPORT
|
||||
|
@ -36,7 +36,8 @@ public:
|
||||
* @param message This is data for sending.
|
||||
* @return true if the message sent successful else false.
|
||||
*/
|
||||
virtual bool sendMessage(const QSharedPointer<iMessage>& message) = 0;
|
||||
virtual bool sendMessage(const QSharedPointer<iMessage>& message,
|
||||
std::function<void(const QSharedPointer<iMessage>& responce)> cb) = 0;
|
||||
|
||||
/**
|
||||
* @brief token This is token value for authication on the remote server (bot)
|
||||
|
@ -32,4 +32,5 @@ void iMessage::setUserId(const QByteArray &newUserId) {
|
||||
bool iMessage::isValid() const {
|
||||
return _userId.size();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -61,6 +61,8 @@ public:
|
||||
*/
|
||||
virtual bool isValid() const;
|
||||
|
||||
|
||||
|
||||
private:
|
||||
QByteArray _rawData;
|
||||
QByteArray _userId;
|
||||
|
@ -12,6 +12,7 @@
|
||||
|
||||
#include <QNetworkReply>
|
||||
#include <QSharedPointer>
|
||||
#include <QDebug>
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
@ -40,6 +41,7 @@ bool ITelegramBot::sendMessage(const QSharedPointer<iMessage> &message) {
|
||||
auto getInfoRquest = makePrefix() + message->makeUpload();
|
||||
|
||||
QNetworkReply* networkReplay = _manager->get(QNetworkRequest(QUrl::fromEncoded(getInfoRquest)));
|
||||
networkReplay->setProperty("call_back", QVariant::fromValue(t));
|
||||
|
||||
if (!networkReplay)
|
||||
return false;
|
||||
@ -55,14 +57,29 @@ QByteArray ITelegramBot::makePrefix() const {
|
||||
return "https://api.telegram.org/bot" + token();
|
||||
}
|
||||
|
||||
void ITelegramBot::onMessageReceived(const QSharedPointer<ITelegramMessage> & message) {
|
||||
|
||||
setId(message->rawJson().value("id").toInt());
|
||||
setName(message->rawJson().value("first_name").toString());
|
||||
setUsername(message->rawJson().value("username").toString());
|
||||
}
|
||||
|
||||
void ITelegramBot::handleReplayIsFinished() {
|
||||
if (QNetworkReply* replay = dynamic_cast<QNetworkReply*>(sender())) {
|
||||
|
||||
auto rawData = replay->readAll();
|
||||
replay->property("request_id").toUInt();
|
||||
|
||||
auto message = QSharedPointer<ITelegramMessage>::create();
|
||||
message->setRawData(rawData);
|
||||
|
||||
if (!message->isValid()) {
|
||||
qDebug() << "Some request is wrong: code:" << message->rawJson().value("error_code").toInt();
|
||||
qDebug() << "What: " << message->rawJson().value("description").toString();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
replay->deleteLater();
|
||||
|
||||
onMessageReceived(message);
|
||||
@ -71,4 +88,28 @@ void ITelegramBot::handleReplayIsFinished() {
|
||||
}
|
||||
}
|
||||
|
||||
void ITelegramBot::setUsername(const QString &newUsername) {
|
||||
_username = newUsername;
|
||||
}
|
||||
|
||||
void ITelegramBot::setName(const QString &newName) {
|
||||
_name = newName;
|
||||
}
|
||||
|
||||
void ITelegramBot::setId(unsigned long long newId) {
|
||||
_id = newId;
|
||||
}
|
||||
|
||||
QString ITelegramBot::username() const {
|
||||
return _username;
|
||||
}
|
||||
|
||||
QString ITelegramBot::name() const {
|
||||
return _name;
|
||||
}
|
||||
|
||||
unsigned long long ITelegramBot::id() const {
|
||||
return _id;
|
||||
}
|
||||
|
||||
} // namespace qTbot
|
||||
|
@ -33,7 +33,43 @@ public:
|
||||
bool login(const QByteArray &token) override;
|
||||
bool sendMessage(const QSharedPointer<iMessage> &message) override;
|
||||
|
||||
/**
|
||||
* @brief id This method return bots id number.
|
||||
* @return bots id number.
|
||||
*/
|
||||
unsigned long long id() const;
|
||||
|
||||
/**
|
||||
* @brief name This is name of the bot.
|
||||
* @return name of the bot.
|
||||
*/
|
||||
QString name() const;
|
||||
|
||||
/**
|
||||
* @brief username This is bots login
|
||||
* @return bots login.
|
||||
*/
|
||||
QString username() const;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief setId This method sets new value for the ITelegramBot::id property.
|
||||
* @param newId this is new value of the ITelegramBot::id property.
|
||||
*/
|
||||
void setId(unsigned long long newId);
|
||||
|
||||
/**
|
||||
* @brief setName This method sets new value for the ITelegramBot::name property.
|
||||
* @param newName this is new value of the ITelegramBot::name property.
|
||||
*/
|
||||
void setName(const QString &newName);
|
||||
|
||||
/**
|
||||
* @brief setUsername This method sets new value for the ITelegramBot::username property.
|
||||
* @param newUsername this is new value of the ITelegramBot::username property.
|
||||
*/
|
||||
void setUsername(const QString &newUsername);
|
||||
|
||||
/**
|
||||
* @brief makePrefix This method prepare a prefix message for all telegramm bots.
|
||||
* @return telegramm request prefix/
|
||||
@ -44,7 +80,7 @@ protected:
|
||||
* @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;
|
||||
virtual void onMessageReceived(const QSharedPointer<ITelegramMessage>& replay);;
|
||||
|
||||
private slots:
|
||||
void handleReplayIsFinished();
|
||||
@ -53,6 +89,10 @@ signals:
|
||||
void receiveMessage(const QSharedPointer<iMessage>& );
|
||||
|
||||
private:
|
||||
unsigned long long _id = 0;
|
||||
QString _name;
|
||||
QString _username;
|
||||
|
||||
QNetworkAccessManager *_manager = nullptr;
|
||||
|
||||
};
|
||||
|
@ -37,7 +37,7 @@ void ITelegramMessage::setRawData(const QByteArray &newRawData) {
|
||||
}
|
||||
|
||||
bool ITelegramMessage::isValid() const {
|
||||
return !_rawJson.isEmpty();
|
||||
return _rawJson.value("ok").toBool();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -41,7 +41,6 @@ public:
|
||||
|
||||
void setRawData(const QByteArray &newRawData) override;
|
||||
|
||||
|
||||
private:
|
||||
QJsonObject _rawJson;
|
||||
|
||||
|
@ -15,7 +15,7 @@ namespace qTbot {
|
||||
|
||||
TelegramRestBot::TelegramRestBot() {
|
||||
_timer = new QTimer();
|
||||
_timer->start(1000);
|
||||
_timer->start(2000);
|
||||
|
||||
connect(_timer, &QTimer::timeout, this, &TelegramRestBot::handleTimeOut,
|
||||
Qt::QueuedConnection);
|
||||
@ -29,6 +29,9 @@ bool TelegramRestBot::login(const QByteArray &token) {
|
||||
if (!ITelegramBot::login(token)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
_timer->start();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user