qTbot 0.2.123.6f57966
qTbot is base back end library for your c++ Qt projects.
telegrammsg.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
8#include "telegrammsg.h"
10#include "qjsonarray.h"
11namespace qTbot {
12
13
18
19TelegramMsg::TelegramMsg(const QJsonObject &obj) {
21}
22
23unsigned long long TelegramMsg::messageId() const {
24 return rawJson()["message_id"].toInteger();
25}
26
27unsigned long long TelegramMsg::fromId() const {
28 return rawJson()["from"]["id"].toInteger();
29}
30
31bool TelegramMsg::isBot() const {
32 return rawJson()["from"]["is_bot"].toBool();
33}
34
35QString TelegramMsg::firstName() const {
36 return rawJson()["from"]["first_name"].toString();
37}
38
39QString TelegramMsg::lastName() const {
40 return rawJson()["from"]["last_name"].toString();
41}
42
43QString TelegramMsg::username() const {
44 return rawJson()["from"]["username"].toString();
45}
46
48 return rawJson()["from"]["language_code"].toString();
49}
50
51QVariant TelegramMsg::chatId() const {
52 return rawJson()["chat"]["id"].toVariant();
53}
54
56 return rawJson().contains("message_id");
57}
58
59QString TelegramMsg::from() const {
60 return chatUsername();
61}
62
64 return rawJson()["chat"]["first_name"].toString();
65}
66
68 return rawJson()["chat"]["last_name"].toString();
69}
70
72 return rawJson()["chat"]["username"].toString();
73}
74
75QString TelegramMsg::chatType() const {
76 return rawJson()["chat"]["type"].toString();
77}
78
79qint64 TelegramMsg::date() const {
80 return rawJson()["date"].toInteger();
81}
82
83QString TelegramMsg::text() const {
84 return rawJson()["text"].toString();
85}
86
87QString TelegramMsg::fullText() const {
88 return rawJson()["text"].toString() + rawJson()["caption"].toString();
89}
90
91QString TelegramMsg::caption() const {
92 return rawJson()["caption"].toString();
93}
94
95bool TelegramMsg::contains(const Type &type) {
96 return rawJson().contains(type);
97}
98
99QList<QSharedPointer<TelegramImage>> TelegramMsg::images() const {
100 const QJsonArray&& array = rawJson()["photo"].toArray();
101 QList<QSharedPointer<TelegramImage>> result;
102
103 for (const auto& photo: array) {
104 result.push_back(QSharedPointer<TelegramImage>::create(photo.toObject()));
105 }
106
107 return result;
108}
109
110QSharedPointer<TelegramImage> TelegramMsg::image(QualitySelector behavior, int size) const {
111 const QJsonArray&& array = rawJson()["photo"].toArray();
112 auto&& result = QSharedPointer<TelegramImage>::create();
113
114 switch (behavior) {
116 {
117 int oldBestMath = std::numeric_limits<decltype(oldBestMath)>::max();
118 auto it = array.cbegin();
119
120 result->setRawJson(it->toObject());
121 it = std::next(it);
122
123 while (it != array.end() && std::abs(result->fileSize() - size) < oldBestMath) {
124 oldBestMath = std::abs(result->fileSize() - size);
125 result->setRawJson(it->toObject());
126 it = std::next(it);
127 }
128
129 return result;
130 }
132 for (const auto& photo: array) {
133 auto photoObj = TelegramImage{photo.toObject()};
134 if (photoObj.fileSize() < size) {
135 result->setRawJson(photoObj.rawJson());
136 } else {
137 return result;
138 }
139 }
140
141 return result;
142 }
144 if (array.size()) {
145 result->setRawJson(std::prev(array.end())->toObject());
146 }
147
148 return result;
149 }
150
152 if (array.size()) {
153 result->setRawJson(array.begin()->toObject());
154 }
155
156 return result;
157 }
158
159 break;
160 default:
161 break;
162 }
163
164 return result;
165}
166
167QSharedPointer<TelegramDocument> TelegramMsg::documents() const {
168 return QSharedPointer<TelegramDocument>::create(rawJson()[Document].toObject());
169}
170
171QSharedPointer<TelegramAudio> TelegramMsg::audio() const {
172 return QSharedPointer<TelegramAudio>::create(rawJson()[Audio].toObject());
173}
174
175QSharedPointer<TelegramContact> TelegramMsg::contact() const {
176 return QSharedPointer<TelegramContact>::create(rawJson()[Contact].toObject());
177}
178
179QSharedPointer<TelegramLocation> TelegramMsg::location() const {
180 return QSharedPointer<TelegramLocation>::create(rawJson()[Location].toObject());
181}
182
183unsigned long long TelegramMsg::updateId() const {
184 return 0;
185}
186
187}
const QJsonObject & rawJson() const
rawJson Telegram uses a rest api with json objects. So all received messages are parsed into a json o...
virtual void setRawJson(const QJsonObject &newRawJson)
setRawJson This method sets a new raw json data of the object.
The TelegramImage class This is implementation of the photos of telegram.
bool isBot() const
isBot checks if the sender is a bot.
QString from() const override
from This virtual function should return the id of the user that sent this update to the bot.
QString languageCode() const
languageCode returns the sender's language code.
QString fullText() const
fullText Returns the text + caption value.
unsigned long long fromId() const
fromId returns the sender's ID.
const Type Document
Document This is the type of the documents.
Definition telegrammsg.h:70
const Type Contact
Contact This is type of the files.
Definition telegrammsg.h:75
QSharedPointer< TelegramLocation > location() const
location Returns the location object of the message.
const Type Location
Location This is the type of location data.
Definition telegrammsg.h:80
QString username() const
username returns the sender's username.
const Type Audio
Audio This is the type of audio files.
Definition telegrammsg.h:85
QString Type
Type This is a string value of the telegram message types.
Definition telegrammsg.h:55
unsigned long long messageId() const override
messageId returns the message ID.
QString firstName() const
firstName returns the sender's first name.
QString caption() const
caption Returns only the caption value.
qint64 date() const
date returns the date of the sent message.
unsigned long long updateId() const override
updateId This method returns the numeric id of the update.
bool isValid() const override
isValid returns true if the update is valid else false.
QSharedPointer< TelegramImage > image(QualitySelector behavior=AroundSize, int size=100000) const
Retrieve an image from the Telegram service with specified quality and size parameters.
QString chatFirstName() const
chatFirstName returns the first participant's first name in the chat.
QSharedPointer< TelegramContact > contact() const
contact Returns the contact data.
QString chatUsername() const
chatUsername returns the first participant's username in the chat.
QSharedPointer< TelegramDocument > documents() const
documents This method returns the contained document data.
QualitySelector
The QualitySelector enum This is the behavior of image and video selection.
Definition telegrammsg.h:31
@ BestOf
BestOf This selection tries to find the best image with a size limit. For example,...
Definition telegrammsg.h:41
@ Fast
Fast This selection returns the fastest image of the available ones (minimum size).
Definition telegrammsg.h:49
@ Best
Best This selection returns the best image of the available ones.
Definition telegrammsg.h:45
@ AroundSize
AroundSize This selection tries to find a file similar by size quality. For example,...
Definition telegrammsg.h:36
QString text() const
text returns text that contains this message object.
QList< QSharedPointer< TelegramImage > > images() const
images This function returns an array of available image qualities
QString chatLastName() const
chatLastName returns the first participant's last name in the chat.
QSharedPointer< TelegramAudio > audio() const
audio This method returns the contained audio data.
QVariant chatId() const override
chatId returns the chat ID.
QString lastName() const
lastName returns the sender's last name.
TelegramMsg()
TelegramMsg default constructor.
QString chatType() const
chatType returns the chat type.
bool contains(const Type &type)
contains This method returns true if the message contains the chosen data type.