inline_bot_send_data.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "history/history_location_manager.h"
  9. struct HistoryItemCommonFields;
  10. namespace Data {
  11. struct SendError;
  12. } // namespace Data
  13. namespace Main {
  14. class Session;
  15. } // namespace Main
  16. class History;
  17. namespace InlineBots {
  18. class Result;
  19. namespace internal {
  20. // Abstract class describing the message that will be
  21. // sent if the user chooses this inline bot result.
  22. // For each type of message that can be sent there will be a subclass.
  23. class SendData {
  24. public:
  25. explicit SendData(not_null<Main::Session*> session) : _session(session) {
  26. }
  27. SendData(const SendData &other) = delete;
  28. SendData &operator=(const SendData &other) = delete;
  29. virtual ~SendData() = default;
  30. [[nodiscard]] Main::Session &session() const {
  31. return *_session;
  32. }
  33. virtual bool isValid() const = 0;
  34. virtual not_null<HistoryItem*> makeMessage(
  35. const Result *owner,
  36. not_null<History*> history,
  37. HistoryItemCommonFields &&fields) const = 0;
  38. virtual Data::SendError getErrorOnSend(
  39. const Result *owner,
  40. not_null<History*> history) const = 0;
  41. virtual bool hasLocationCoords() const {
  42. return false;
  43. }
  44. virtual std::optional<Data::LocationPoint> getLocationPoint() const {
  45. return std::nullopt;
  46. }
  47. virtual QString getLayoutTitle(const Result *owner) const;
  48. virtual QString getLayoutDescription(const Result *owner) const;
  49. private:
  50. not_null<Main::Session*> _session;
  51. };
  52. // This class implements addHistory() for most of the types hiding
  53. // the differences in getSentMessageFields() method.
  54. // Only SendFile and SendPhoto work by their own.
  55. class SendDataCommon : public SendData {
  56. public:
  57. using SendData::SendData;
  58. struct SentMessageFields {
  59. TextWithEntities text;
  60. MTPMessageMedia media = MTP_messageMediaEmpty();
  61. };
  62. virtual SentMessageFields getSentMessageFields() const = 0;
  63. not_null<HistoryItem*> makeMessage(
  64. const Result *owner,
  65. not_null<History*> history,
  66. HistoryItemCommonFields &&fields) const override;
  67. Data::SendError getErrorOnSend(
  68. const Result *owner,
  69. not_null<History*> history) const override;
  70. };
  71. // Plain text message.
  72. class SendText : public SendDataCommon {
  73. public:
  74. SendText(
  75. not_null<Main::Session*> session,
  76. const QString &message,
  77. const EntitiesInText &entities,
  78. bool/* noWebPage*/)
  79. : SendDataCommon(session)
  80. , _message(message)
  81. , _entities(entities) {
  82. }
  83. bool isValid() const override {
  84. return !_message.isEmpty();
  85. }
  86. SentMessageFields getSentMessageFields() const override;
  87. private:
  88. QString _message;
  89. EntitiesInText _entities;
  90. };
  91. // Message with geo location point media.
  92. class SendGeo : public SendDataCommon {
  93. public:
  94. SendGeo(
  95. not_null<Main::Session*> session,
  96. const MTPDgeoPoint &point)
  97. : SendDataCommon(session)
  98. , _location(point) {
  99. }
  100. SendGeo(
  101. not_null<Main::Session*> session,
  102. const MTPDgeoPoint &point,
  103. int period,
  104. std::optional<int> heading,
  105. std::optional<int> proximityNotificationRadius)
  106. : SendDataCommon(session)
  107. , _location(point)
  108. , _period(period)
  109. , _heading(heading)
  110. , _proximityNotificationRadius(proximityNotificationRadius){
  111. }
  112. bool isValid() const override {
  113. return true;
  114. }
  115. SentMessageFields getSentMessageFields() const override;
  116. bool hasLocationCoords() const override {
  117. return true;
  118. }
  119. std::optional<Data::LocationPoint> getLocationPoint() const override {
  120. return _location;
  121. }
  122. private:
  123. Data::LocationPoint _location;
  124. std::optional<int> _period;
  125. std::optional<int> _heading;
  126. std::optional<int> _proximityNotificationRadius;
  127. };
  128. // Message with venue media.
  129. class SendVenue : public SendDataCommon {
  130. public:
  131. SendVenue(
  132. not_null<Main::Session*> session,
  133. const MTPDgeoPoint &point,
  134. const QString &venueId,
  135. const QString &provider,
  136. const QString &title,
  137. const QString &address)
  138. : SendDataCommon(session)
  139. , _location(point)
  140. , _venueId(venueId)
  141. , _provider(provider)
  142. , _title(title)
  143. , _address(address) {
  144. }
  145. bool isValid() const override {
  146. return true;
  147. }
  148. SentMessageFields getSentMessageFields() const override;
  149. bool hasLocationCoords() const override {
  150. return true;
  151. }
  152. std::optional<Data::LocationPoint> getLocationPoint() const override {
  153. return _location;
  154. }
  155. private:
  156. Data::LocationPoint _location;
  157. QString _venueId, _provider, _title, _address;
  158. };
  159. // Message with shared contact media.
  160. class SendContact : public SendDataCommon {
  161. public:
  162. SendContact(
  163. not_null<Main::Session*> session,
  164. const QString &firstName,
  165. const QString &lastName,
  166. const QString &phoneNumber)
  167. : SendDataCommon(session)
  168. , _firstName(firstName)
  169. , _lastName(lastName)
  170. , _phoneNumber(phoneNumber) {
  171. }
  172. bool isValid() const override {
  173. return (!_firstName.isEmpty() || !_lastName.isEmpty()) && !_phoneNumber.isEmpty();
  174. }
  175. SentMessageFields getSentMessageFields() const override;
  176. QString getLayoutDescription(const Result *owner) const override;
  177. private:
  178. QString _firstName, _lastName, _phoneNumber;
  179. };
  180. // Message with photo.
  181. class SendPhoto : public SendData {
  182. public:
  183. SendPhoto(
  184. not_null<Main::Session*> session,
  185. PhotoData *photo,
  186. const QString &message,
  187. const EntitiesInText &entities)
  188. : SendData(session)
  189. , _photo(photo)
  190. , _message(message)
  191. , _entities(entities) {
  192. }
  193. bool isValid() const override {
  194. return _photo != nullptr;
  195. }
  196. not_null<HistoryItem*> makeMessage(
  197. const Result *owner,
  198. not_null<History*> history,
  199. HistoryItemCommonFields &&fields) const override;
  200. Data::SendError getErrorOnSend(
  201. const Result *owner,
  202. not_null<History*> history) const override;
  203. private:
  204. PhotoData *_photo;
  205. QString _message;
  206. EntitiesInText _entities;
  207. };
  208. // Message with file.
  209. class SendFile : public SendData {
  210. public:
  211. SendFile(
  212. not_null<Main::Session*> session,
  213. DocumentData *document,
  214. const QString &message,
  215. const EntitiesInText &entities)
  216. : SendData(session)
  217. , _document(document)
  218. , _message(message)
  219. , _entities(entities) {
  220. }
  221. bool isValid() const override {
  222. return _document != nullptr;
  223. }
  224. not_null<HistoryItem*> makeMessage(
  225. const Result *owner,
  226. not_null<History*> history,
  227. HistoryItemCommonFields &&fields) const override;
  228. Data::SendError getErrorOnSend(
  229. const Result *owner,
  230. not_null<History*> history) const override;
  231. private:
  232. DocumentData *_document;
  233. QString _message;
  234. EntitiesInText _entities;
  235. };
  236. // Message with game.
  237. class SendGame : public SendData {
  238. public:
  239. SendGame(not_null<Main::Session*> session, GameData *game)
  240. : SendData(session)
  241. , _game(game) {
  242. }
  243. bool isValid() const override {
  244. return _game != nullptr;
  245. }
  246. not_null<HistoryItem*> makeMessage(
  247. const Result *owner,
  248. not_null<History*> history,
  249. HistoryItemCommonFields &&fields) const override;
  250. Data::SendError getErrorOnSend(
  251. const Result *owner,
  252. not_null<History*> history) const override;
  253. private:
  254. GameData *_game;
  255. };
  256. class SendInvoice : public SendDataCommon {
  257. public:
  258. SendInvoice(
  259. not_null<Main::Session*> session,
  260. MTPMessageMedia media)
  261. : SendDataCommon(session)
  262. , _media(media) {
  263. }
  264. bool isValid() const override {
  265. return true;
  266. }
  267. SentMessageFields getSentMessageFields() const override;
  268. QString getLayoutDescription(const Result *owner) const override;
  269. private:
  270. MTPMessageMedia _media;
  271. };
  272. } // namespace internal
  273. } // namespace InlineBots