qTbot 0.87.9547b0c
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-2024 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
87bool TelegramMsg::contains(const Type &type) {
88 return rawJson().contains(type);
89}
90
91QList<QSharedPointer<TelegramImage>> TelegramMsg::images() const {
92 const QJsonArray&& array = rawJson()["photo"].toArray();
93 QList<QSharedPointer<TelegramImage>> result;
94
95 for (const auto& photo: array) {
96 result.push_back(QSharedPointer<TelegramImage>::create(photo.toObject()));
97 }
98
99 return result;
100}
101
102QSharedPointer<TelegramImage> TelegramMsg::image(QualitySelector behavior, int size) const {
103 const QJsonArray&& array = rawJson()["photo"].toArray();
104 auto&& result = QSharedPointer<TelegramImage>::create();
105
106 switch (behavior) {
108 {
109 int oldBestMath = std::numeric_limits<decltype(oldBestMath)>::max();
110 auto it = array.cbegin();
111
112 result->setRawJson(it->toObject());
113 it = std::next(it);
114
115 while (it != array.end() && std::abs(result->fileSize() - size) < oldBestMath) {
116 oldBestMath = std::abs(result->fileSize() - size);
117 result->setRawJson(it->toObject());
118 it = std::next(it);
119 }
120
121 return result;
122 }
124 for (const auto& photo: array) {
125 auto photoObj = TelegramImage{photo.toObject()};
126 if (photoObj.fileSize() < size) {
127 result->setRawJson(photoObj.rawJson());
128 } else {
129 return result;
130 }
131 }
132
133 return result;
134 }
136 if (array.size()) {
137 result->setRawJson(std::prev(array.end())->toObject());
138 }
139
140 return result;
141 }
142
144 if (array.size()) {
145 result->setRawJson(array.begin()->toObject());
146 }
147
148 return result;
149 }
150
151 break;
152 default:
153 break;
154 }
155
156 return result;
157}
158
159QSharedPointer<TelegramDocument> TelegramMsg::documents() const {
160 return QSharedPointer<TelegramDocument>::create(rawJson()[Document].toObject());
161}
162
163QSharedPointer<TelegramAudio> TelegramMsg::audio() const {
164 return QSharedPointer<TelegramAudio>::create(rawJson()[Audio].toObject());
165}
166
167QSharedPointer<TelegramContact> TelegramMsg::contact() const {
168 return QSharedPointer<TelegramContact>::create(rawJson()[Contact].toObject());
169}
170
171QSharedPointer<TelegramLocation> TelegramMsg::location() const {
172 return QSharedPointer<TelegramLocation>::create(rawJson()[Location].toObject());
173}
174
175unsigned long long TelegramMsg::updateId() const {
176 return 0;
177}
178
179}
const QJsonObject & rawJson() const
rawJson Telegram use rest api with json objects. So all received messages will be parsed in to jsobje...
virtual void setRawJson(const QJsonObject &newRawJson)
setRawJson this method convert jsobject into telegram message.
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 name of the user that sent this message to bot.
QString languageCode() const
languageCode returns the sender's language code.
unsigned long long fromId() const
fromId returns the sender's ID.
const Type Document
Document This is type of the files.
Definition telegrammsg.h:63
const Type Contact
Contact This is type of the files.
Definition telegrammsg.h:68
QSharedPointer< TelegramLocation > location() const
location returns location object of message.
const Type Location
Location This is type of location data.
Definition telegrammsg.h:73
QString username() const
username returns the sender's username.
const Type Audio
Audio This is type of Audio files.
Definition telegrammsg.h:78
QString Type
Type just string value of the telegram messages types.
Definition telegrammsg.h:48
unsigned long long messageId() const override
messageId returns the message ID.
QString firstName() const
firstName returns the sender's first name.
qint64 date() const
date returns date of the snet message.
unsigned long long updateId() const override
updateId This method returns numeric id of the update.
bool isValid() const override
isValid return true if the message 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 return contact
QString chatUsername() const
chatUsername returns the first participant's username in the chat.
QSharedPointer< TelegramDocument > documents() const
documents This method returns contained document data.
QualitySelector
The QualitySelector enum This is behavior of image and video selection.
Definition telegrammsg.h:31
@ Fast
This selection returns faster iamge of available. (minimum size)
Definition telegrammsg.h:42
@ Best
This selection returns best iamge of available.
Definition telegrammsg.h:40
QString text() const
text returns text that contains this message object.
QList< QSharedPointer< TelegramImage > > images() const
images This function returns array of available images qualites
QString chatLastName() const
chatLastName returns the first participant's last name in the chat.
QSharedPointer< TelegramAudio > audio() const
audio This method returns contained audio data.
QVariant chatId() const override
chatId returns the chat ID.
QString lastName() const
lastName returns the sender's last name.
QString chatType() const
chatType returns the chat type.
bool contains(const Type &type)
contains This method returns true if the message contains choosed data type.