mirror of
https://github.com/QuasarApp/qTbot.git
synced 2025-05-12 13:19:38 +00:00
fix works with photo
This commit is contained in:
parent
79abb46124
commit
722f56b158
src/qTbot/src
@ -12,7 +12,7 @@ TelegramSendDocument::TelegramSendDocument(const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QString& fileName,
|
||||
const QByteArray &data):
|
||||
TelegramSendFile("sendDocument", chatId, text, fileName, data) {}
|
||||
TelegramSendFile("sendDocument", chatId, text, fileName, TELEGRAM_DOCUMENT, data) {}
|
||||
|
||||
TelegramSendDocument::TelegramSendDocument(const QVariant &chatId,
|
||||
const QString &text,
|
||||
|
@ -6,6 +6,8 @@
|
||||
//#
|
||||
#include "telegramsendfile.h"
|
||||
|
||||
#include <QJsonDocument>
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
|
||||
@ -13,36 +15,71 @@ qTbot::TelegramSendFile::TelegramSendFile(const QString &request,
|
||||
const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QString &fileName,
|
||||
const QByteArray &data):
|
||||
const QString &fileType,
|
||||
const QByteArray &data,
|
||||
unsigned long long replyToMessageId,
|
||||
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects
|
||||
):
|
||||
TelegramSingleRquest(request) {
|
||||
|
||||
addArg("chat_id", chatId);
|
||||
if (text.size())
|
||||
addArg("caption", text);
|
||||
addArg("chat_id", chatId);
|
||||
if (text.size())
|
||||
addArg("caption", text);
|
||||
|
||||
addArg(QString("%0:%1").arg(REQUEST_UPLOAD_FILE_KEY, fileName), data);
|
||||
if (replyToMessageId > 0) {
|
||||
addArg("reply_to_message_id", replyToMessageId);
|
||||
}
|
||||
|
||||
for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
|
||||
addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
addArg(QString("%0:%1:%2").arg(REQUEST_UPLOAD_FILE_KEY, fileName, fileType), data);
|
||||
}
|
||||
|
||||
qTbot::TelegramSendFile::TelegramSendFile(const QString &request,
|
||||
const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QFileInfo &file):
|
||||
const QFileInfo &file,
|
||||
unsigned long long replyToMessageId,
|
||||
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects):
|
||||
TelegramSingleRquest(request) {
|
||||
|
||||
addArg("chat_id", chatId);
|
||||
addArg("chat_id", chatId);
|
||||
|
||||
if (text.size())
|
||||
addArg("text", text);
|
||||
if (text.size())
|
||||
addArg("text", text);
|
||||
|
||||
QFile readFile(file.absoluteFilePath());
|
||||
if (!readFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning() << "Fail to open file" << file.absoluteFilePath();
|
||||
}
|
||||
if (replyToMessageId > 0) {
|
||||
addArg("reply_to_message_id", replyToMessageId);
|
||||
}
|
||||
|
||||
addArg(REQUEST_UPLOAD_FILE_KEY, file.completeBaseName().toLatin1() + ":" + readFile.readAll());
|
||||
QFile readFile(file.absoluteFilePath());
|
||||
if (!readFile.open(QIODevice::ReadOnly)) {
|
||||
qWarning() << "Fail to open file" << file.absoluteFilePath();
|
||||
}
|
||||
|
||||
for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
|
||||
addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact));
|
||||
}
|
||||
|
||||
auto&& sufix = file.suffix();
|
||||
if (sufix.contains("png") || sufix.contains("jpg") || sufix.contains("jepg") || sufix.contains("gif")) {
|
||||
addArg(QString("%0:%1:%2").arg(REQUEST_UPLOAD_FILE_KEY, file.fileName(), TELEGRAM_PHOTO), readFile.readAll());
|
||||
|
||||
} else {
|
||||
addArg(QString("%0:%1:%2").arg(REQUEST_UPLOAD_FILE_KEY, file.fileName(), TELEGRAM_DOCUMENT), readFile.readAll());
|
||||
|
||||
}
|
||||
|
||||
readFile.close();
|
||||
}
|
||||
|
||||
iRequest::RequestMethod TelegramSendFile::method() const {
|
||||
return iRequest::RequestMethod::Upload;
|
||||
return iRequest::RequestMethod::Upload;
|
||||
}
|
||||
|
||||
QString TelegramSendFile::type() const {
|
||||
return REQUEST_UPLOAD_FILE_KEY;
|
||||
}
|
||||
}
|
||||
|
@ -9,8 +9,11 @@
|
||||
|
||||
#include "qfileinfo.h"
|
||||
#include "requests/telegramsinglerquest.h"
|
||||
namespace qTbot {
|
||||
|
||||
#define TELEGRAM_PHOTO "photo"
|
||||
#define TELEGRAM_DOCUMENT "document"
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
/**
|
||||
* @brief The TelegramSendFile class Base clas for upload files to server
|
||||
@ -23,16 +26,26 @@ public:
|
||||
const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QString &fileName,
|
||||
const QByteArray& data);
|
||||
const QString &fileType,
|
||||
const QByteArray& data,
|
||||
unsigned long long replyToMessageId = 0,
|
||||
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects = {});
|
||||
|
||||
TelegramSendFile(const QString &request,
|
||||
const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QFileInfo &file);
|
||||
const QFileInfo &file,
|
||||
unsigned long long replyToMessageId = 0,
|
||||
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects = {});
|
||||
|
||||
// iRequest interface
|
||||
public:
|
||||
RequestMethod method() const override;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief type return type file. By Default it is REQUEST_UPLOAD_FILE_KEY
|
||||
* @return
|
||||
*/
|
||||
virtual QString type() const;
|
||||
};
|
||||
}
|
||||
#endif // TELEGRAMSENDFILE_H
|
||||
|
@ -14,16 +14,20 @@ namespace qTbot {
|
||||
TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QString &fileName,
|
||||
const QByteArray &data):
|
||||
TelegramSendFile("sendPhoto", chatId, text, fileName, data) {
|
||||
const QByteArray &data,
|
||||
unsigned long long replyToMessageId,
|
||||
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects
|
||||
):
|
||||
TelegramSendFile("sendPhoto", chatId, text, fileName, TELEGRAM_PHOTO, data, replyToMessageId, extraObjects) {
|
||||
|
||||
}
|
||||
|
||||
TelegramSendPhoto::TelegramSendPhoto(const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QFileInfo &file):
|
||||
TelegramSendFile("sendPhoto", chatId, text, file) {
|
||||
|
||||
const QFileInfo &file,
|
||||
unsigned long long replyToMessageId,
|
||||
const QMap<QString, QSharedPointer<QJsonObject> > &extraObjects):
|
||||
TelegramSendFile("sendPhoto", chatId, text, file, replyToMessageId, extraObjects) {
|
||||
|
||||
}
|
||||
|
||||
|
@ -22,11 +22,17 @@ public:
|
||||
TelegramSendPhoto(const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QString &fileName,
|
||||
const QByteArray& data);
|
||||
const QByteArray& data,
|
||||
unsigned long long replyToMessageId = 0,
|
||||
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {}
|
||||
);
|
||||
|
||||
TelegramSendPhoto(const QVariant &chatId,
|
||||
const QString &text,
|
||||
const QFileInfo &file);
|
||||
const QFileInfo &file,
|
||||
unsigned long long replyToMessageId = 0,
|
||||
const QMap<QString, QSharedPointer<QJsonObject>>& extraObjects = {}
|
||||
);
|
||||
|
||||
};
|
||||
}
|
||||
|
@ -66,13 +66,16 @@ QSharedPointer<QHttpMultiPart> iRequest::argsToMultipartFormData() const {
|
||||
if (it.key().contains(REQUEST_UPLOAD_FILE_KEY)) {
|
||||
auto metaData = it.key().split(":");
|
||||
|
||||
if (metaData.size() == 2) {
|
||||
if (metaData.size() == 3) {
|
||||
const auto fileName = metaData[1];
|
||||
part.setHeader(QNetworkRequest::ContentDispositionHeader, "form-data; name=\"document\"; filename=\"" + fileName + "\"");
|
||||
const auto fileType = metaData[2];
|
||||
|
||||
part.setHeader(QNetworkRequest::ContentDispositionHeader,
|
||||
"form-data; name=\"" + fileType + "\"; filename=\"" + fileName + "\"");
|
||||
|
||||
part.setBody(value.toByteArray());
|
||||
} else {
|
||||
qWarning() << "the file arguments must be like _file_:fileName";
|
||||
qWarning() << "the file arguments must be like _file_:fileName:fileType";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,7 @@
|
||||
#include <QHttpMultiPart>
|
||||
|
||||
#define REQUEST_UPLOAD_FILE_KEY "_file_"
|
||||
|
||||
namespace qTbot {
|
||||
|
||||
/**
|
||||
|
@ -222,7 +222,7 @@ bool ITelegramBot::editSpecificMessageWithKeyboard(const QVariant &messageId,
|
||||
const QString &text,
|
||||
bool markdown,
|
||||
bool disableWebPagePreview,
|
||||
const QList<QMap<QString, std::function<void (const QString &, const QVariant&)> > > &keyboard,
|
||||
const KeyboardOnMessage &keyboard,
|
||||
const QString &callBackQueryId) {
|
||||
|
||||
if (!chatId.isValid() || chatId.isNull())
|
||||
@ -389,7 +389,9 @@ bool ITelegramBot::sendFile(const QByteArray &file, const QString &fileName, con
|
||||
|
||||
bool ITelegramBot::sendPhoto(const QFileInfo &photo,
|
||||
const QVariant &chatId,
|
||||
const QString &description) {
|
||||
const QString &description,
|
||||
unsigned long long replyToMessageId,
|
||||
const KeyboardOnMessage &keyboard) {
|
||||
if (!chatId.isValid() || chatId.isNull())
|
||||
return false;
|
||||
|
||||
@ -397,13 +399,16 @@ bool ITelegramBot::sendPhoto(const QFileInfo &photo,
|
||||
return false;
|
||||
}
|
||||
|
||||
return sendFileWithPrivate(QSharedPointer<TelegramSendPhoto>::create(chatId, description, photo));
|
||||
return sendFileWithPrivate(QSharedPointer<TelegramSendPhoto>::create(
|
||||
chatId, description, photo, replyToMessageId, prepareInlineKeyBoard(keyboard)));
|
||||
}
|
||||
|
||||
bool ITelegramBot::sendPhoto(const QByteArray &photo,
|
||||
const QString &fileName,
|
||||
const QVariant &chatId,
|
||||
const QString &description) {
|
||||
const QString &description,
|
||||
unsigned long long replyToMessageId,
|
||||
const KeyboardOnMessage &keyboard) {
|
||||
|
||||
if (!chatId.isValid() || chatId.isNull())
|
||||
return false;
|
||||
@ -416,7 +421,8 @@ bool ITelegramBot::sendPhoto(const QByteArray &photo,
|
||||
return false;
|
||||
}
|
||||
|
||||
return sendFileWithPrivate(QSharedPointer<TelegramSendPhoto>::create(chatId, description, fileName, photo));
|
||||
return sendFileWithPrivate(QSharedPointer<TelegramSendPhoto>::create(
|
||||
chatId, description, fileName, photo, replyToMessageId, prepareInlineKeyBoard(keyboard)));
|
||||
}
|
||||
|
||||
bool ITelegramBot::sendFileWithDescription(const QByteArray &file,
|
||||
|
@ -187,7 +187,7 @@ public:
|
||||
const QString &text,
|
||||
bool markdown = true,
|
||||
bool disableWebPagePreview = false,
|
||||
const QList<QMap<QString, std::function<void (const QString &, const QVariant &)> > > &keyboard = {},
|
||||
const KeyboardOnMessage &keyboard = {},
|
||||
const QString &callBackQueryId = "");
|
||||
|
||||
/**
|
||||
@ -235,18 +235,31 @@ public:
|
||||
* @brief sendPhoto This method will send image into chat with @a chatId
|
||||
* @param photo this is photo path.
|
||||
* @param chatId target chat
|
||||
* @param replyToMessageId The unique identifier of the message to reply to, if any.
|
||||
* @param keyboard A list of maps where each map represents a button with a callback function (optional).
|
||||
* @return true if photo will snt successful
|
||||
*/
|
||||
bool sendPhoto(const QFileInfo& photo, const QVariant& chatId, const QString &description);
|
||||
bool sendPhoto(const QFileInfo& photo,
|
||||
const QVariant& chatId,
|
||||
const QString &description,
|
||||
unsigned long long replyToMessageId = 0,
|
||||
const KeyboardOnMessage &keyboard = {});
|
||||
|
||||
/**
|
||||
* @brief sendPhoto This method will send image into chat with @a chatId
|
||||
* @param photo this is photo data.
|
||||
* @param chatId target chat
|
||||
* @param fileName This is dispalyed name of photo.
|
||||
* @param replyToMessageId The unique identifier of the message to reply to, if any.
|
||||
* @param keyboard A list of maps where each map represents a button with a callback function (optional).
|
||||
* @return true if photo will snt successful
|
||||
*/
|
||||
bool sendPhoto(const QByteArray& photo, const QString& fileName, const QVariant& chatId, const QString &description);
|
||||
bool sendPhoto(const QByteArray& photo,
|
||||
const QString& fileName,
|
||||
const QVariant& chatId,
|
||||
const QString &description,
|
||||
unsigned long long replyToMessageId = 0,
|
||||
const KeyboardOnMessage &keyboard = {});
|
||||
|
||||
/**
|
||||
* @brief sendFileWithDescription This method sents a byte array as a file into @a chatId with additional text @a description.
|
||||
|
Loading…
x
Reference in New Issue
Block a user