120 lines
2.7 KiB
C++
120 lines
2.7 KiB
C++
#ifndef TELEGRAMBOTAPI_H
|
|
#define TELEGRAMBOTAPI_H
|
|
|
|
#include <QThread>
|
|
#include <QNetworkAccessManager>
|
|
#include <QNetworkRequest>
|
|
#include <QNetworkReply>
|
|
#include <QUrl>
|
|
#include <QDateTime>
|
|
#include <QMutex>
|
|
#include <QQueue>
|
|
#include <QImage>
|
|
|
|
class TelegramBotAPI : public QObject
|
|
{
|
|
Q_OBJECT
|
|
Q_PROPERTY(uint botID READ botID)
|
|
Q_PROPERTY(QString botName READ botName)
|
|
Q_PROPERTY(QString botToken READ botToken WRITE setBotToken)
|
|
public:
|
|
explicit TelegramBotAPI(QObject *parent = 0);
|
|
~TelegramBotAPI();
|
|
|
|
enum MessageType {
|
|
Text,
|
|
Photo,
|
|
Document,
|
|
};
|
|
|
|
struct Message {
|
|
Message() {
|
|
chat_id = id = user_id = 0;
|
|
}
|
|
uint id;
|
|
uint chat_id;
|
|
uint user_id;
|
|
QString user_name;
|
|
QString text;
|
|
QDateTime time;
|
|
};
|
|
struct File {
|
|
uint chat_id;
|
|
QString filename;
|
|
QByteArray data;
|
|
};
|
|
|
|
uint botID() const {return m_botID;}
|
|
QString botToken() const {return m_botToken;}
|
|
QString botName() const {return m_botName;}
|
|
bool isConnected() const {return is_connected;}
|
|
|
|
public slots:
|
|
void setBotToken(QString arg);
|
|
bool sendMessage(uint chat_id, QString msg) {return sendMessage(chat_id, TelegramBotAPI::Text, msg.toUtf8());}
|
|
bool sendMessage(uint chat_id, MessageType mtype, QByteArray data, QString filename = QString());
|
|
|
|
private:
|
|
|
|
struct SendQuery {
|
|
QString method;
|
|
QVariantMap params;
|
|
};
|
|
|
|
struct DownloadFile {
|
|
bool downloaded;
|
|
int chat_id;
|
|
QString id;
|
|
QString filename;
|
|
QUrl url;
|
|
QByteArray data;
|
|
};
|
|
|
|
static const char * send_methods[];
|
|
static const char * param_filename;
|
|
|
|
// void run();
|
|
void sendRequest(QString method, QVariantMap params);
|
|
void checkBot();
|
|
void updateBot();
|
|
bool fileDownload();
|
|
bool parseHeader(QString json, QVariant &res);
|
|
void parseCheckBot(QString json);
|
|
void parseMessages(QString json);
|
|
void parseFile(QString json);
|
|
Message parseMessage(QVariant msg);
|
|
void startDownloadFile(uint chat_id, QVariantMap file);
|
|
|
|
QNetworkAccessManager * qnam;
|
|
QNetworkReply *reply;
|
|
// QMutex mutex;
|
|
uint m_botID;
|
|
QString m_botToken;
|
|
QString botUrl;
|
|
QString botUrlFile;
|
|
QString m_botName;
|
|
bool is_connected;
|
|
int last_update;
|
|
bool wait_server;
|
|
bool has_stop;
|
|
QQueue<SendQuery> sendmsg_que;
|
|
QList<DownloadFile> download_list;
|
|
|
|
private slots:
|
|
void sslErrors(QNetworkReply*, const QList<QSslError> &errors);
|
|
void httpFinished();
|
|
// void httpFinished(QNetworkReply*);
|
|
|
|
signals:
|
|
void botOK();
|
|
void botFail();
|
|
void errorOccured(QString error);
|
|
void newMessage(TelegramBotAPI::Message msg);
|
|
void newMessage(uint chat_id, QString msg);
|
|
void newFile(TelegramBotAPI::File file);
|
|
};
|
|
|
|
Q_DECLARE_METATYPE(TelegramBotAPI::Message)
|
|
|
|
#endif // TELEGRAMBOTAPI_H
|