qTbot 0.2.123.6f57966
qTbot is base back end library for your c++ Qt projects.
telegramsendfile.cpp
Go to the documentation of this file.
1//#
2//# Copyright (C) 2023-2026 QuasarApp.
3//# Distributed under the GPLv3 software license, see the accompanying
4//# Everyone is permitted to copy and distribute verbatim copies
5//# of this license document, but changing it is not allowed.
6//#
7#include "telegramsendfile.h"
8
9#include <QJsonDocument>
10
11namespace qTbot {
12
13
15 const QString &fileName,
16 const QString &fileType,
17 const QByteArray& data,
18 const TelegramArgs &args,
19 const ExtraJsonObjects& extraObjects
20 ):
21 TelegramSingleRquest(request, args.toMap(true)) {
23
24
25 for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
26 addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact));
27 }
28
29 addArg(QString("%0:%1:%2").arg(REQUEST_UPLOAD_FILE_KEY, fileName, fileType), data);
30}
31
33 const QFileInfo &file,
34 const TelegramArgs &args,
35 const QHash<QString, QSharedPointer<QJsonObject> > &extraObjects):
36 TelegramSingleRquest(request, args.toMap(true)) {
38
39 QFile readFile(file.absoluteFilePath());
40 if (!readFile.open(QIODevice::ReadOnly)) {
41 qWarning() << "Fail to open file" << file.absoluteFilePath();
42 }
43
44 for (auto it = extraObjects.begin(); it != extraObjects.end(); it = std::next(it)) {
45 addArg(it.key(), QJsonDocument(*it.value()).toJson(QJsonDocument::Compact));
46 }
47
48 auto&& sufix = file.suffix();
49 if (sufix.contains("png") || sufix.contains("jpg") || sufix.contains("jepg") || sufix.contains("gif")) {
50 addArg(QString("%0:%1:%2").arg(REQUEST_UPLOAD_FILE_KEY, file.fileName(), TELEGRAM_PHOTO), readFile.readAll());
51
52 } else {
53 addArg(QString("%0:%1:%2").arg(REQUEST_UPLOAD_FILE_KEY, file.fileName(), TELEGRAM_DOCUMENT), readFile.readAll());
54
55 }
56
57 readFile.close();
58}
59
63
64QString TelegramSendFile::type() const {
66}
67}
RequestMethod method() const override
method Returns the method of the request. The file upload requests always use the Upload method.
TelegramSendFile(const QString &request, const QString &fileName, const QString &fileType, const QByteArray &data, const TelegramArgs &args, const QHash< QString, QSharedPointer< QJsonObject > > &extraObjects={})
TelegramSendFile prepares the send file request from a byte array.
virtual QString type() const
type Returns the file type. By default it is REQUEST_UPLOAD_FILE_KEY.
TelegramSingleRquest(const QString &request, const QMap< QString, QVariant > &args)
TelegramSingleRquest This is the main constructor of the single requests.
RequestMethod
RequestMethod This is the list of supported request types.
Definition irequest.h:43
@ Upload
Upload this is a post request to upload big data to telegram.
Definition irequest.h:55
const QMap< QString, QVariant > & args() const
args This method returns the the current list of arguments
Definition irequest.cpp:30
const QString & request() const
request Returns the current request command.
Definition irequest.cpp:102
void addArg(const QString &key, const QVariant &val)
addArg This method pushes a new argument to the arguments list
Definition irequest.cpp:34
void setPriority(RequestPriority newPriority)
setPriority This method sets a new priority for the request.
Definition irequest.cpp:98
#define REQUEST_UPLOAD_FILE_KEY
Definition irequest.h:19
QHash< QString, QSharedPointer< QJsonObject > > ExtraJsonObjects
ExtraJsonObjects hash map of the extra objects of the message.
Definition irequest.h:26
The TelegramArgs class is the base structure for all telegram message arguments.
iRequest::RequestPriority requestPriority
requestPriority This is the priority of the request.
#define TELEGRAM_DOCUMENT
#define TELEGRAM_PHOTO