diff options
author | Andrea Giove <andreagiove@outlook.com> | 2016-03-27 11:16:28 +0200 |
---|---|---|
committer | Andrea Giove <andreagiove@outlook.com> | 2016-03-27 11:16:28 +0200 |
commit | f889903cd7132ce39e81da15d2f335ac511b2e1f (patch) | |
tree | 8de30b3f819da6343effd80f9018028d784c6a40 /include/tgbot | |
parent | 67a543eb596b68a166d7f790ea27d5d4e70e10b7 (diff) |
Add new types and new event broadcaster for support the inline mode.
Diffstat (limited to 'include/tgbot')
-rw-r--r-- | include/tgbot/EventBroadcaster.h | 44 | ||||
-rw-r--r-- | include/tgbot/EventHandler.h | 42 | ||||
-rw-r--r-- | include/tgbot/TgTypeParser.h | 24 | ||||
-rw-r--r-- | include/tgbot/types/ChosenInlineResult.h | 40 | ||||
-rw-r--r-- | include/tgbot/types/InlineQuery.h | 46 | ||||
-rw-r--r-- | include/tgbot/types/InlineQueryResult.h | 62 | ||||
-rw-r--r-- | include/tgbot/types/InlineQueryResultArticle.h | 54 | ||||
-rw-r--r-- | include/tgbot/types/InlineQueryResultGif.h | 50 | ||||
-rw-r--r-- | include/tgbot/types/InlineQueryResultMpeg4Gif.h | 46 | ||||
-rw-r--r-- | include/tgbot/types/InlineQueryResultPhoto.h | 54 | ||||
-rw-r--r-- | include/tgbot/types/InlineQueryResultVideo.h | 55 | ||||
-rw-r--r-- | include/tgbot/types/Update.h | 12 |
12 files changed, 504 insertions, 25 deletions
diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index 3ec8865..d97dbbb 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -29,6 +29,8 @@ #include <map> #include "tgbot/types/Message.h" +#include "tgbot/types/InlineQuery.h" +#include "tgbot/types/ChosenInlineResult.h" namespace TgBot { @@ -44,6 +46,8 @@ friend EventHandler; public: typedef std::function<void (const Message::Ptr&)> MessageListener; + typedef std::function<void (const InlineQuery::Ptr&)> InlineQueryListener; + typedef std::function<void (const ChosenInlineResult::Ptr&)> ChosenInlineResultListener; /** * Registers listener which receives all messages which the bot can ever receive. @@ -78,13 +82,29 @@ public: _onNonCommandMessageListeners.push_back(listener); } + inline void onInlineQuery(const InlineQueryListener& listener) { + _onInlineQueryListeners.push_back(listener); + } + + inline void onChosenInlineResult(const ChosenInlineResultListener& listener){ + _onChosenInlineResultListeners.push_back(listener); + } + private: - inline void broadcastAnyMessage(const Message::Ptr& message) const { - for (const MessageListener& item : _onAnyMessageListeners) { - item(message); + template<typename ListenerType, typename ObjectType> + inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType& object) const { + if (!object) + return; + + for (const ListenerType& item : listeners) { + item(object); } } + inline void broadcastAnyMessage(const Message::Ptr& message) const { + broadcast<MessageListener, Message::Ptr>(_onAnyMessageListeners, message); + } + inline bool broadcastCommand(const std::string command, const Message::Ptr& message) const { std::map<std::string, MessageListener>::const_iterator iter = _onCommandListeners.find(command); if (iter == _onCommandListeners.end()) { @@ -95,21 +115,27 @@ private: } inline void broadcastUnknownCommand(const Message::Ptr& message) const { - for (const MessageListener& item : _onUnknownCommandListeners) { - item(message); - } + broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message); } inline void broadcastNonCommandMessage(const Message::Ptr& message) const { - for (const MessageListener& item : _onNonCommandMessageListeners) { - item(message); - } + broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message); + } + + inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const { + broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query); + } + + inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr& result) const { + broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result); } std::vector<MessageListener> _onAnyMessageListeners; std::map<std::string, MessageListener> _onCommandListeners; std::vector<MessageListener> _onUnknownCommandListeners; std::vector<MessageListener> _onNonCommandMessageListeners; + std::vector<InlineQueryListener> _onInlineQueryListeners; + std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners; }; } diff --git a/include/tgbot/EventHandler.h b/include/tgbot/EventHandler.h index bb277a8..aee230a 100644 --- a/include/tgbot/EventHandler.h +++ b/include/tgbot/EventHandler.h @@ -31,34 +31,44 @@ namespace TgBot { class EventHandler { -public: - explicit EventHandler(const EventBroadcaster* broadcaster) : _broadcaster(broadcaster) { - } - - inline void handleUpdate(const Update::Ptr& update) const { + inline void handleMessage(const Message::Ptr& message){ _broadcaster->broadcastAnyMessage(update->message); - if (StringTools::startsWith(update->message->text, "/")) { + + if (StringTools::startsWith(message->text, "/")) { unsigned long splitPosition; - unsigned long spacePosition = update->message->text.find(' '); - unsigned long atSymbolPosition = update->message->text.find('@'); - if (spacePosition == update->message->text.npos) { - if (atSymbolPosition == update->message->text.npos) { - splitPosition = update->message->text.size(); + unsigned long spacePosition = message->text.find(' '); + unsigned long atSymbolPosition = message->text.find('@'); + if (spacePosition == message->text.npos) { + if (atSymbolPosition == message->text.npos) { + splitPosition = message->text.size(); } else { splitPosition = atSymbolPosition; } - } else if (atSymbolPosition == update->message->text.npos) { + } else if (atSymbolPosition == message->text.npos) { splitPosition = spacePosition; } else { splitPosition = std::min(spacePosition, atSymbolPosition); } - std::string command = update->message->text.substr(1, splitPosition - 1); - if (!_broadcaster->broadcastCommand(command, update->message)) { - _broadcaster->broadcastUnknownCommand(update->message); + std::string command = message->text.substr(1, splitPosition - 1); + if (!_broadcaster->broadcastCommand(command, message)) { + _broadcaster->broadcastUnknownCommand(message); } } else { - _broadcaster->broadcastNonCommandMessage(update->message); + _broadcaster->broadcastNonCommandMessage(message); } + }; + +public: + explicit EventHandler(const EventBroadcaster* broadcaster) : _broadcaster(broadcaster) { + } + + inline void handleUpdate(const Update::Ptr& update) const { + if (update->inlineQuery != NULL) + _broadcaster->broadcastInlineQuery(update->inlineQuery); + if (update->chosenInlineResult != NULL) + _broadcaster->broadcastChosenInlineResult(update->chosenInlineResult); + if (update->message != NULL) + handleMessag(update->message); } private: diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 670e35e..f52d106 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -44,6 +44,14 @@ #include "tgbot/types/ReplyKeyboardHide.h" #include "tgbot/types/ForceReply.h" #include "tgbot/types/GenericReply.h" +#include "tgbot/types/InlineQuery.h" +#include "tgbot/types/InlineQueryResult.h" +#include "tgbot/types/InlineQueryResultArticle.h" +#include "tgbot/types/InlineQueryResultPhoto.h" +#include "tgbot/types/InlineQueryResultGif.h" +#include "tgbot/types/InlineQueryResultMpeg4Gif.h" +#include "tgbot/types/InlineQueryResultVideo.h" +#include "tgbot/types/ChosenInlineResult.h" namespace TgBot { @@ -90,6 +98,22 @@ public: std::string parseForceReply(const ForceReply::Ptr& object) const; GenericReply::Ptr parseJsonAndGetGenericReply(const boost::property_tree::ptree& data) const; std::string parseGenericReply(const GenericReply::Ptr& object) const; + InlineQuery::Ptr parseJsonAndGetInlineQuery(const boost::property_tree::ptree& data) const; + std::string parseInlineQuery(const InlineQuery::Ptr& object) const; + InlineQueryResult::Ptr parseJsonAndGetInlineQueryResult(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResult(const InlineQueryResult::Ptr& object) const; + InlineQueryResultArticle::Ptr parseJsonAndGetInlineQueryResultArticle(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultArticle(const InlineQueryResultArticle::Ptr& object) const; + InlineQueryResultPhoto::Ptr parseJsonAndGetInlineQueryResultPhoto(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultPhoto(const InlineQueryResultPhoto::Ptr& object) const; + InlineQueryResultGif::Ptr parseJsonAndGetInlineQueryResultGif(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultGif(const InlineQueryResultGif::Ptr& object) const; + InlineQueryResultMpeg4Gif::Ptr parseJsonAndGetInlineQueryResultMpeg4Gif(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultMpeg4Gif(const InlineQueryResultMpeg4Gif::Ptr& object) const; + InlineQueryResultVideo::Ptr parseJsonAndGetInlineQueryResultVideo(const boost::property_tree::ptree& data) const; + std::string parseInlineQueryResultVideo(const InlineQueryResultVideo::Ptr& object) const; + ChosenInlineResult::Ptr parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const; + std::string parseChosenInlineResult(const ChosenInlineResult::Ptr& object) const; inline boost::property_tree::ptree parseJson(const std::string& json) const { boost::property_tree::ptree tree; diff --git a/include/tgbot/types/ChosenInlineResult.h b/include/tgbot/types/ChosenInlineResult.h new file mode 100644 index 0000000..3c71496 --- /dev/null +++ b/include/tgbot/types/ChosenInlineResult.h @@ -0,0 +1,40 @@ +// +// Created by Andrea Giove on 27/03/16. +// + +#ifndef TGBOT_CHOSENINLINERESULT_H +#define TGBOT_CHOSENINLINERESULT_H + +#include <string> +#include <memory> + +#include "tgbot/types/User.h" + +namespace TgBot { + +/** + * This object represents a result of an inline query that was chosen by the user and sent to their chat partner. + * @ingroup types + */ +class ChosenInlineResult { +public: + typedef std::shared_ptr<ChosenInlineResult> Ptr; + + /** + * The unique identifier for the result that was chosen. + */ + std::string resultId; + + /** + * The user that chose the result. + */ + User::Ptr user; + + /** + * The query that was used to obtain the result. + */ + std::string query; +}; +} + +#endif //TGBOT_CHOSENINLINERESULT_H diff --git a/include/tgbot/types/InlineQuery.h b/include/tgbot/types/InlineQuery.h new file mode 100644 index 0000000..a469ea7 --- /dev/null +++ b/include/tgbot/types/InlineQuery.h @@ -0,0 +1,46 @@ +// +// Created by Andrea Giove on 26/03/16. +// + +#ifndef TGBOT_INLINEQUERY_H +#define TGBOT_INLINEQUERY_H + +#include <memory> +#include <string> + +#include "tgbot/types/User.h" + +namespace TgBot { + +/** + * This object represents an incoming inline query. + * @ingroup types + */ +class InlineQuery { +public: + typedef std::shared_ptr<InlineQuery> Ptr; + + /** + * Unique query identifier. + */ + std::string id; + + /** + * Sender. + */ + User::Ptr from; + + /** + * Text of the query. + */ + std::string query; + + /** + * Offset of the results to be returned. + */ + std::string offset; +}; + +} + +#endif //TGBOT_INLINEQUERY_H diff --git a/include/tgbot/types/InlineQueryResult.h b/include/tgbot/types/InlineQueryResult.h new file mode 100644 index 0000000..b8309dc --- /dev/null +++ b/include/tgbot/types/InlineQueryResult.h @@ -0,0 +1,62 @@ +// +// Created by Andrea Giove on 26/03/16. +// + +#ifndef TGBOT_INLINEQUERYRESULT_H +#define TGBOT_INLINEQUERYRESULT_H + +#include <memory> +#include <string> + +namespace TgBot { + +/** + * This abstract class is base of all inline query results. + * @ingroup types + */ +class InlineQueryResult { +public: + typedef std::shared_ptr<InlineQueryResult> Ptr; + + virtual ~InlineQueryResult() { } + + /** + * Type of the result. + */ + std::string type; + + /** + * Unique identifier for this result. (1-64 bytes) + */ + std::string id; + + /** + * Optional. Title of the result. + */ + std::string title; + + /** + * Text of the message t be sent. (1-4096 characters) + */ + std::string messageText; + + /** + * Optional. Send Markdown or HTML, if you want Telegram apps to + * show bold, italic, fixed-width text or inline URLs in your bot's message. + */ + std::string parseMode; + + /** + * Optional. Disables link previews for links in the send message. + */ + bool disableWebPagePreview; + + /** + * Optional. Url of the thumbnail for the result. + */ + std::string thumbUrl; + +}; +} + +#endif //TGBOT_INLINEQUERYRESULT_H diff --git a/include/tgbot/types/InlineQueryResultArticle.h b/include/tgbot/types/InlineQueryResultArticle.h new file mode 100644 index 0000000..9d0b043 --- /dev/null +++ b/include/tgbot/types/InlineQueryResultArticle.h @@ -0,0 +1,54 @@ +// +// Created by Andrea Giove on 26/03/16. +// + +#ifndef TGBOT_INLINEQUERYRESULTARTICLE_H +#define TGBOT_INLINEQUERYRESULTARTICLE_H + +#include <string> +#include <memory> + +#include "tgbot/types/InlineQueryResult.h" + +namespace TgBot { + +/** + * Represents a link to an article of web page. + * @ingroup types + */ +class InlineQueryResultArticle : public InlineQueryResult { +public: + static const std::string TYPE = "article"; + + typedef std::shared_ptr<InlineQueryResultArticle> Ptr; + + InlineQueryResultArticle() : type(TYPE) {}; + + /** + * Optional. URL of the result. + */ + std::string url; + + /** + * Optional. Pass True if you don't want the URL to be shown in the message. + */ + bool hideUrl; + + /** + * Optional. Short description of the result. + */ + std::string description; + + /** + * Optional. Thumbnail width. + */ + int32_t thumbWidth; + + /** + * Optinal. Thumbnail height + */ + int32_t thumbHeight; +}; +} + +#endif //TGBOT_INLINEQUERYRESULTARTICLE_H diff --git a/include/tgbot/types/InlineQueryResultGif.h b/include/tgbot/types/InlineQueryResultGif.h new file mode 100644 index 0000000..06e63ae --- /dev/null +++ b/include/tgbot/types/InlineQueryResultGif.h @@ -0,0 +1,50 @@ +// +// Created by Andrea Giove on 27/03/16. +// + +#ifndef TGBOT_INLINEQUERYRESULTGIF_H +#define TGBOT_INLINEQUERYRESULTGIF_H + +#include <string> +#include <memory> + +#include "tgbot/types/InlineQueryResult.h" + +namespace TgBot { + +/** + * Represents a link to an animated GIF file. + * @ingroup types + */ +class InlineQueryResultGif : public InlineQueryResult { +public: + static const std::string TYPE = "gif"; + + typedef std::shared_ptr<InlineQueryResultGif> Ptr; + + InlineQueryResultGif() : type(TYPE) {}; + + /** + * A valid URL for the GIF file. + */ + std::string gifUrl; + + /** + * Optional. Width of the GIF. + */ + int32_t gifWidth; + + /** + * Optional. Height of the GIF. + */ + int32_t gifHeight; + + /** + * Optional. Caption for the GIF file to be sent. + */ + std::string caption; + +}; +} + +#endif //TGBOT_INLINEQUERYRESULTGIF_H diff --git a/include/tgbot/types/InlineQueryResultMpeg4Gif.h b/include/tgbot/types/InlineQueryResultMpeg4Gif.h new file mode 100644 index 0000000..170b0fa --- /dev/null +++ b/include/tgbot/types/InlineQueryResultMpeg4Gif.h @@ -0,0 +1,46 @@ +// +// Created by Andrea Giove on 27/03/16. +// + +#ifndef TGBOT_INLINEQUERYRESULTMPEG4GIF_H +#define TGBOT_INLINEQUERYRESULTMPEG4GIF_H + +namespace TgBot { + +/** + * Represents a link to a video animation (H.264/MPEG-4 AVC video without sound). + * @ingroup types + */ +class InlineQueryResultMpeg4Gif : public InlineQueryResult { +public: + static const std::string TYPE = "mpeg4_gif"; + + typedef std::shared_ptr<InlineQueryResultMpeg4Gif> Ptr; + + InlineQueryResultMpeg4Gif() : type(TYPE) {}; + + /** + * A valid URL for the MP4 file. + */ + std::string mpeg4Url; + + /** + * Optional. Video width. + */ + int32_t mpeg4Width; + + /** + * Optional. Video height. + */ + int32_t mpeg4Height; + + /** + * Optional. Caption of the MPEG-4 file to be sent. + */ + std::string caption; + + +}; +} + +#endif //TGBOT_INLINEQUERYRESULTMPEG4GIF_H diff --git a/include/tgbot/types/InlineQueryResultPhoto.h b/include/tgbot/types/InlineQueryResultPhoto.h new file mode 100644 index 0000000..07e1f35 --- /dev/null +++ b/include/tgbot/types/InlineQueryResultPhoto.h @@ -0,0 +1,54 @@ +// +// Created by Andrea Giove on 26/03/16. +// + +#ifndef TGBOT_INLINEQUERYRESULTPHOTO_H +#define TGBOT_INLINEQUERYRESULTPHOTO_H + +#include <string> +#include <memory> + +#include "tgbot/types/InlineQueryResult.h" + +namespace TgBot { + +/** + * Represents a link to a photo. + * @ingroup types + */ +class InlineQueryResultPhoto : public InlineQueryResult { +public: + static const std::string TYPE = "photo"; + + typedef std::shared_ptr<InlineQueryResultPhoto> Ptr; + + InlineQueryResultPhoto() : type(TYPE) {}; + + /** + * A valid URL of the photo. + */ + std::string photoUrl; + + /** + * Optional. Width of the photo. + */ + int32_t photoWidth; + + /** + * Optional. Height of the photo. + */ + int32_t photoHeight; + + /** + * Optional. Short description of the result. + */ + std::string description; + + /** + * Optional. Caption of the photo to be sent. + */ + std::string caption; +}; +} + +#endif //TGBOT_INLINEQUERYRESULTPHOTO_H diff --git a/include/tgbot/types/InlineQueryResultVideo.h b/include/tgbot/types/InlineQueryResultVideo.h new file mode 100644 index 0000000..f3e5029 --- /dev/null +++ b/include/tgbot/types/InlineQueryResultVideo.h @@ -0,0 +1,55 @@ +// +// Created by Andrea Giove on 27/03/16. +// + +#ifndef TGBOT_INLINEQUERYRESULTVIDEO_H +#define TGBOT_INLINEQUERYRESULTVIDEO_H + +namespace TgBot { + +/** + * Represents link to a page containing an embedded video player or a video file. + * @ingroup types + */ +class InlineQueryResultVideo : public InlineQueryResult { +public: + static const std::string TYPE = "video"; + + typedef std::shared_ptr<InlineQueryResultVideo> Ptr; + + InlineQueryResultVideo() : type(TYPE) {}; + + /** + * A valid URL for the embedded video player or video file. + */ + std::string videoUrl; + + /** + * Mime type of the content of video url, "text/html" or "video/mp4". + */ + std::string mimeType; + + /** + * Optional. Video width. + */ + int32_t videoWidth; + + /** + * Optional. Video height. + */ + int32_t videoHeight; + + /** + * Optional. Video duration. + */ + int32_t videoDuration; + + /** + * Optional. Short description of the result. + */ + std::string description; + +}; +} + +#endif //TGBOT_INLINEQUERYRESULTVIDEO_H diff --git a/include/tgbot/types/Update.h b/include/tgbot/types/Update.h index 9bf3a19..9ae8d73 100644 --- a/include/tgbot/types/Update.h +++ b/include/tgbot/types/Update.h @@ -26,6 +26,8 @@ #include <memory> #include "tgbot/types/Message.h" +#include "tgbot/types/InlineQuery.h" +#include "tgbot/types/ChosenInlineResult.h" namespace TgBot { @@ -47,6 +49,16 @@ public: * Optional. New incoming message of any kind — text, photo, sticker, etc. */ Message::Ptr message; + + /** + * Optional. New incoming inline query + */ + InlineQuery::Ptr inlineQuery; + + /** + * Optional. The result of an inline query that was chosen by a user and sent to their chat partner. + */ + ChosenInlineResult::Ptr chosenInlineResult; }; } |