From 376b88ec09ef8cf71dd823467406c58204485fcc Mon Sep 17 00:00:00 2001 From: Oleg Morozenkov Date: Tue, 11 Aug 2015 22:13:23 +0300 Subject: Fixed compiler's errors in TgTypeParser + fixed indentation + some other fixes --- include/tgbot/Api.h | 50 +++--- include/tgbot/Bot.h | 60 ++++---- include/tgbot/EventBroadcaster.h | 130 ++++++++-------- include/tgbot/EventHandler.h | 30 ++-- include/tgbot/TgException.h | 2 +- include/tgbot/TgTypeParser.h | 248 +++++++++++++++--------------- include/tgbot/doxygenMain.h | 2 +- include/tgbot/net/HttpClient.h | 16 +- include/tgbot/net/HttpParser.h | 40 ++--- include/tgbot/net/HttpReqArg.h | 42 ++--- include/tgbot/net/HttpServer.h | 106 ++++++------- include/tgbot/net/TgLongPoll.h | 18 +-- include/tgbot/net/TgWebhookLocalServer.h | 14 +- include/tgbot/net/TgWebhookServer.h | 32 ++-- include/tgbot/net/TgWebhookTcpServer.h | 14 +- include/tgbot/net/Url.h | 12 +- include/tgbot/tools/StringTools.h | 6 +- include/tgbot/types/Audio.h | 10 +- include/tgbot/types/Contact.h | 42 ++--- include/tgbot/types/Document.h | 12 +- include/tgbot/types/ForceReply.h | 18 +-- include/tgbot/types/GenericChat.h | 12 +- include/tgbot/types/GenericReply.h | 4 +- include/tgbot/types/GroupChat.h | 10 +- include/tgbot/types/InputFile.h | 18 +-- include/tgbot/types/Location.h | 18 +-- include/tgbot/types/Message.h | 222 +++++++++++++------------- include/tgbot/types/PhotoSize.h | 10 +- include/tgbot/types/ReplyKeyboardHide.h | 6 +- include/tgbot/types/ReplyKeyboardMarkup.h | 36 ++--- include/tgbot/types/Sticker.h | 12 +- include/tgbot/types/Update.h | 6 +- include/tgbot/types/User.h | 8 +- include/tgbot/types/UserProfilePhotos.h | 18 +-- include/tgbot/types/Video.h | 28 ++-- 35 files changed, 659 insertions(+), 653 deletions(-) (limited to 'include') diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h index 1b4987a..ebebfd7 100644 --- a/include/tgbot/Api.h +++ b/include/tgbot/Api.h @@ -49,13 +49,13 @@ class Api { friend Bot; public: - Api(const std::string& token); + Api(const std::string& token); - /** - * A simple method for testing your bot's auth token. - * @return Basic information about the bot in form of a User object. - */ - User::Ptr getMe() const; + /** + * A simple method for testing your bot's auth token. + * @return Basic information about the bot in form of a User object. + */ + User::Ptr getMe() const; /** * Use this method to send text messages. @@ -66,7 +66,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendMessage(int32_t chatId, const std::string& text, bool disableWebPagePreview = false, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendMessage(int32_t chatId, const std::string& text, bool disableWebPagePreview = false, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to forward messages of any kind. @@ -75,7 +75,7 @@ public: * @param messageId Unique message identifier. * @return On success, the sent message is returned. */ - Message::Ptr forwardMessage(int32_t chatId, int32_t fromChatId, int32_t messageId) const; + Message::Ptr forwardMessage(int32_t chatId, int32_t fromChatId, int32_t messageId) const; /** * Use this method to send photos. @@ -86,7 +86,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendPhoto(int32_t chatId, const InputFile::Ptr& photo, const std::string& caption = "", int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendPhoto(int32_t chatId, const InputFile::Ptr& photo, const std::string& caption = "", int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send photos. @@ -97,7 +97,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendPhoto(int32_t chatId, const std::string& photo, const std::string& caption = "", int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendPhoto(int32_t chatId, const std::string& photo, const std::string& caption = "", int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Document). @@ -108,7 +108,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t duration = 0, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendAudio(int32_t chatId, const InputFile::Ptr& audio, int32_t duration = 0, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send audio files, if you want Telegram clients to display the file as a playable voice message. For this to work, your audio must be in an .ogg file encoded with OPUS (other formats may be sent as Document). @@ -119,7 +119,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendAudio(int32_t chatId, const std::string& audio, int32_t duration = 0, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendAudio(int32_t chatId, const std::string& audio, int32_t duration = 0, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send general files. @@ -129,7 +129,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendDocument(int32_t chatId, const InputFile::Ptr& document, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendDocument(int32_t chatId, const InputFile::Ptr& document, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send general files. @@ -139,7 +139,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendDocument(int32_t chatId, const std::string& document, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendDocument(int32_t chatId, const std::string& document, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send .webp stickers. @@ -149,7 +149,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendSticker(int32_t chatId, const InputFile::Ptr& sticker, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendSticker(int32_t chatId, const InputFile::Ptr& sticker, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send .webp stickers. @@ -159,7 +159,7 @@ public: * @param replyMarkup Optional. Additional interface options. A object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendSticker(int32_t chatId, const std::string& sticker, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendSticker(int32_t chatId, const std::string& sticker, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). @@ -169,7 +169,7 @@ public: * @param replyMarkup Optional. Additional interface options. An object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendVideo(int32_t chatId, const InputFile::Ptr& video, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendVideo(int32_t chatId, const InputFile::Ptr& video, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send video files, Telegram clients support mp4 videos (other formats may be sent as Document). @@ -179,7 +179,7 @@ public: * @param replyMarkup Optional. Additional interface options. A object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendVideo(int32_t chatId, const std::string& video, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendVideo(int32_t chatId, const std::string& video, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method to send point on the map. @@ -190,7 +190,7 @@ public: * @param replyMarkup Optional. Additional interface options. A object for a custom reply keyboard, instructions to hide keyboard or to force a reply from the user. * @return On success, the sent message is returned. */ - Message::Ptr sendLocation(int32_t chatId, float latitude, float longitude, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; + Message::Ptr sendLocation(int32_t chatId, float latitude, float longitude, int32_t replyToMessageId = 0, const GenericReply::Ptr& replyMarkup = GenericReply::Ptr()) const; /** * Use this method when you need to tell the user that something is happening on the bot's side. The status is set for 5 seconds or less (when a message arrives from your bot, Telegram clients clear its typing status). @@ -199,7 +199,7 @@ public: * @param chatId Unique identifier for the message recipient — User or GroupChat id. * @param action Type of action to broadcast. Choose one, depending on what the user is about to receive: typing for text messages, upload_photo for photos, record_video or upload_video for videos, record_audio or upload_audio for audio files, upload_document for general files, find_location for location data. */ - void sendChatAction(int32_t chatId, const std::string& action) const; + void sendChatAction(int32_t chatId, const std::string& action) const; /** * Use this method to get a list of profile pictures for a user. @@ -208,7 +208,7 @@ public: * @param limit Optional. Limits the number of photos to be retrieved. Values between 1—100 are accepted. Defaults to 100. * @return A UserProfilePhotos object. */ - UserProfilePhotos::Ptr getUserProfilePhotos(int32_t userId, int32_t offset = 0, int32_t limit = 100) const; + UserProfilePhotos::Ptr getUserProfilePhotos(int32_t userId, int32_t offset = 0, int32_t limit = 100) const; /** * Use this method to receive incoming updates using long polling. @@ -219,7 +219,7 @@ public: * @param timeout Optional. Timeout in seconds for long polling. Defaults to 0, i.e. usual short polling. * @return An Array of Update objects */ - std::vector getUpdates(int32_t offset = 0, int32_t limit = 100, int32_t timeout = 0) const; + std::vector getUpdates(int32_t offset = 0, int32_t limit = 100, int32_t timeout = 0) const; /** * Use this method to specify a url and receive incoming updates via an outgoing webhook. Whenever there is an update for the bot, we will send an HTTPS POST request to the specified url, containing a JSON-serialized Update. In case of an unsuccessful request, we will give up after a reasonable amount of attempts. @@ -229,12 +229,12 @@ public: * Ports currently supported for Webhooks: 443, 80, 88, 8443. * @param url Optional. HTTPS url to send updates to. Use an empty string to remove webhook integration. */ - void setWebhook(const std::string& url = "") const; + void setWebhook(const std::string& url = "") const; private: - boost::property_tree::ptree sendRequest(const std::string& method, const std::vector& args = std::vector()) const; + boost::property_tree::ptree sendRequest(const std::string& method, const std::vector& args = std::vector()) const; - const std::string _token; + const std::string _token; }; } diff --git a/include/tgbot/Bot.h b/include/tgbot/Bot.h index 5e96642..36e723b 100644 --- a/include/tgbot/Bot.h +++ b/include/tgbot/Bot.h @@ -38,42 +38,42 @@ namespace TgBot { class Bot { public: - explicit Bot(const std::string& token) : _token(token), _api(token), _eventHandler(&_eventBroadcaster) { - } + explicit Bot(const std::string& token) : _token(token), _api(token), _eventHandler(&_eventBroadcaster) { + } - /** - * @return Token for accessing api. - */ - inline const std::string& getToken() const { - return _token; - } + /** + * @return Token for accessing api. + */ + inline const std::string& getToken() const { + return _token; + } - /** - * @return Object which can execute Telegram Bot API methods. - */ - inline const Api& getApi() const { - return _api; - } + /** + * @return Object which can execute Telegram Bot API methods. + */ + inline const Api& getApi() const { + return _api; + } - /** - * @return Object which holds all event listeners. - */ - inline EventBroadcaster& getEvents() { - return _eventBroadcaster; - } + /** + * @return Object which holds all event listeners. + */ + inline EventBroadcaster& getEvents() { + return _eventBroadcaster; + } - /** - * @return Object which handles new update objects. Usually it's only needed for TgLongPoll, TgWebhookLocalServer and TgWebhookTcpServer objects. - */ - inline const EventHandler& getEventHandler() const { - return _eventHandler; - } + /** + * @return Object which handles new update objects. Usually it's only needed for TgLongPoll, TgWebhookLocalServer and TgWebhookTcpServer objects. + */ + inline const EventHandler& getEventHandler() const { + return _eventHandler; + } private: - const std::string _token; - const Api _api; - EventBroadcaster _eventBroadcaster; - const EventHandler _eventHandler; + const std::string _token; + const Api _api; + EventBroadcaster _eventBroadcaster; + const EventHandler _eventHandler; }; } diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index 03d7df1..3ec8865 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -43,73 +43,73 @@ class EventBroadcaster { friend EventHandler; public: - typedef std::function MessageListener; - - /** - * Registers listener which receives all messages which the bot can ever receive. - * @param listener Listener. - */ - inline void onAnyMessage(const MessageListener& listener) { - _onAnyMessageListeners.push_back(listener); - } - - /** - * Registers listener which receives all messages with commands (messages with leading '/' char). - * @param commandName Command name which listener can handle. - * @param listener Listener. - */ - inline void onCommand(const std::string& commandName, const MessageListener& listener) { - _onCommandListeners[commandName] = listener; - } - - /** - * Registers listener which receives all messages with commands (messages with leading '/' char) which haven't been handled by other listeners. - * @param listener Listener. - */ - inline void onUnknownCommand(const MessageListener& listener) { - _onUnknownCommandListeners.push_back(listener); - } - - /** - * Registers listener which receives all messages without commands (messages with no leading '/' char) - * @param listener Listener. - */ - inline void onNonCommandMessage(const MessageListener& listener) { - _onNonCommandMessageListeners.push_back(listener); - } + typedef std::function MessageListener; + + /** + * Registers listener which receives all messages which the bot can ever receive. + * @param listener Listener. + */ + inline void onAnyMessage(const MessageListener& listener) { + _onAnyMessageListeners.push_back(listener); + } + + /** + * Registers listener which receives all messages with commands (messages with leading '/' char). + * @param commandName Command name which listener can handle. + * @param listener Listener. + */ + inline void onCommand(const std::string& commandName, const MessageListener& listener) { + _onCommandListeners[commandName] = listener; + } + + /** + * Registers listener which receives all messages with commands (messages with leading '/' char) which haven't been handled by other listeners. + * @param listener Listener. + */ + inline void onUnknownCommand(const MessageListener& listener) { + _onUnknownCommandListeners.push_back(listener); + } + + /** + * Registers listener which receives all messages without commands (messages with no leading '/' char) + * @param listener Listener. + */ + inline void onNonCommandMessage(const MessageListener& listener) { + _onNonCommandMessageListeners.push_back(listener); + } private: - inline void broadcastAnyMessage(const Message::Ptr& message) const { - for (const MessageListener& item : _onAnyMessageListeners) { - item(message); - } - } - - inline bool broadcastCommand(const std::string command, const Message::Ptr& message) const { - std::map::const_iterator iter = _onCommandListeners.find(command); - if (iter == _onCommandListeners.end()) { - return false; - } - iter->second(message); - return true; - } - - inline void broadcastUnknownCommand(const Message::Ptr& message) const { - for (const MessageListener& item : _onUnknownCommandListeners) { - item(message); - } - } - - inline void broadcastNonCommandMessage(const Message::Ptr& message) const { - for (const MessageListener& item : _onNonCommandMessageListeners) { - item(message); - } - } - - std::vector _onAnyMessageListeners; - std::map _onCommandListeners; - std::vector _onUnknownCommandListeners; - std::vector _onNonCommandMessageListeners; + inline void broadcastAnyMessage(const Message::Ptr& message) const { + for (const MessageListener& item : _onAnyMessageListeners) { + item(message); + } + } + + inline bool broadcastCommand(const std::string command, const Message::Ptr& message) const { + std::map::const_iterator iter = _onCommandListeners.find(command); + if (iter == _onCommandListeners.end()) { + return false; + } + iter->second(message); + return true; + } + + inline void broadcastUnknownCommand(const Message::Ptr& message) const { + for (const MessageListener& item : _onUnknownCommandListeners) { + item(message); + } + } + + inline void broadcastNonCommandMessage(const Message::Ptr& message) const { + for (const MessageListener& item : _onNonCommandMessageListeners) { + item(message); + } + } + + std::vector _onAnyMessageListeners; + std::map _onCommandListeners; + std::vector _onUnknownCommandListeners; + std::vector _onNonCommandMessageListeners; }; } diff --git a/include/tgbot/EventHandler.h b/include/tgbot/EventHandler.h index abcf925..1ec2cbe 100644 --- a/include/tgbot/EventHandler.h +++ b/include/tgbot/EventHandler.h @@ -32,23 +32,23 @@ namespace TgBot { class EventHandler { public: - explicit EventHandler(const EventBroadcaster* broadcaster) : _broadcaster(broadcaster) { - } - - inline void handleUpdate(const Update::Ptr& update) const { - _broadcaster->broadcastAnyMessage(update->message); - if (StringTools::startsWith(update->message->text, "/")) { - std::string command = update->message->text.substr(1, update->message->text.find(' ') - 2); - if (!_broadcaster->broadcastCommand(command, update->message)) { - _broadcaster->broadcastUnknownCommand(update->message); - } - } else { - _broadcaster->broadcastNonCommandMessage(update->message); - } - } + explicit EventHandler(const EventBroadcaster* broadcaster) : _broadcaster(broadcaster) { + } + + inline void handleUpdate(const Update::Ptr& update) const { + _broadcaster->broadcastAnyMessage(update->message); + if (StringTools::startsWith(update->message->text, "/")) { + std::string command = update->message->text.substr(1, update->message->text.find(' ') - 2); + if (!_broadcaster->broadcastCommand(command, update->message)) { + _broadcaster->broadcastUnknownCommand(update->message); + } + } else { + _broadcaster->broadcastNonCommandMessage(update->message); + } + } private: - const EventBroadcaster* _broadcaster; + const EventBroadcaster* _broadcaster; }; } diff --git a/include/tgbot/TgException.h b/include/tgbot/TgException.h index 6f01fd3..1a3e172 100644 --- a/include/tgbot/TgException.h +++ b/include/tgbot/TgException.h @@ -35,7 +35,7 @@ namespace TgBot { class TgException : public std::runtime_error { public: - explicit TgException(const std::string description); + explicit TgException(const std::string description); }; } diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 0571c25..759dc2f 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -50,129 +50,135 @@ namespace TgBot { class TgTypeParser { public: - static TgTypeParser& getInstance(); - - User::Ptr parseUser(const boost::property_tree::ptree& data) const; - std::string parseUser(const User::Ptr& object) const; - GroupChat::Ptr parseGroupChat(const boost::property_tree::ptree& data) const; - std::string parseGroupChat(const GroupChat::Ptr& object) const; - Message::Ptr parseMessage(const boost::property_tree::ptree& data) const; - std::string parseMessage(const Message::Ptr& object) const; - PhotoSize::Ptr parsePhotoSize(const boost::property_tree::ptree& data) const; - std::string parsePhotoSize(const PhotoSize::Ptr& object) const; - Audio::Ptr parseAudio(const boost::property_tree::ptree& data) const; - std::string parseAudio(const Audio::Ptr& object) const; - Document::Ptr parseDocument(const boost::property_tree::ptree& data) const; - std::string parseDocument(const Document::Ptr& object) const; - Sticker::Ptr parseSticker(const boost::property_tree::ptree& data) const; - std::string parseSticker(const Sticker::Ptr& object) const; - Video::Ptr parseVideo(const boost::property_tree::ptree& data) const; - std::string parseVideo(const Video::Ptr& object) const; - Contact::Ptr parseContact(const boost::property_tree::ptree& data) const; - std::string parseContact(const Contact::Ptr& object) const; - Location::Ptr parseLocation(const boost::property_tree::ptree& data) const; - std::string parseLocation(const Location::Ptr& object) const; - Update::Ptr parseUpdate(const boost::property_tree::ptree& data) const; - std::string parseUpdate(const Update::Ptr& object) const; - UserProfilePhotos::Ptr parseUserProfilePhotos(const boost::property_tree::ptree& data) const; - std::string parseUserProfilePhotos(const UserProfilePhotos::Ptr& object) const; - ReplyKeyboardMarkup::Ptr parseReplyKeyboardMarkup(const boost::property_tree::ptree& data) const; - std::string parseReplyKeyboardMarkup(const ReplyKeyboardMarkup::Ptr& object) const; - ReplyKeyboardHide::Ptr parseReplyKeyboardHide(const boost::property_tree::ptree& data) const; - std::string parseReplyKeyboardHide(const ReplyKeyboardHide::Ptr& object) const; - ForceReply::Ptr parseForceReply(const boost::property_tree::ptree& data) const; - std::string parseForceReply(const ForceReply::Ptr& object) const; - GenericChat::Ptr parseGenericChat(const boost::property_tree::ptree& data) const; - std::string parseGenericChat(const GenericChat::Ptr& object) const; - GenericReply::Ptr parseGenericReply(const boost::property_tree::ptree& data) const; - std::string parseGenericReply(const GenericReply::Ptr& object) const; - - inline boost::property_tree::ptree parseJson(const std::string& json) const { - boost::property_tree::ptree tree; - std::istringstream input(json); - boost::property_tree::read_json(input, tree); - return tree; - } - - template - std::shared_ptr tryParse(std::shared_ptr (TgTypeParser::*const parseFunc)(const boost::property_tree::ptree&) const, const boost::property_tree::ptree& data, const std::string& keyName) const { - auto treeItem = data.find(keyName); - if (treeItem == data.not_found()) { - return std::shared_ptr(); - } - return (this->*parseFunc)(treeItem->second); - } - - template - std::vector> parseArray(std::shared_ptr (TgTypeParser::*const parseFunc)(const boost::property_tree::ptree&) const, const boost::property_tree::ptree& data, const std::string& keyName) const { - std::vector> result; - auto treeItem = data.find(keyName); - if (treeItem == data.not_found()) { - return result; - } - for (const std::pair& innerTreeItem : treeItem->second) { - result.push_back((this->*parseFunc)(innerTreeItem.second)); - } - return result; - } - - template - std::vector>> parse2DArray(std::shared_ptr (TgTypeParser::*const parseFunc)(const boost::property_tree::ptree&) const, const boost::property_tree::ptree& data, const std::string& keyName) const { - std::vector>> result; - auto treeItem = data.find(keyName); - if (treeItem == data.not_found()) { - return result; - } - for (const std::pair& innerTreeItem : treeItem->second) { - std::vector> innerResult; - for (const std::pair& innerInnerTreeItem : innerTreeItem.second) { - innerResult.push_back((this->*parseFunc)(innerInnerTreeItem.second)); - } - result.push_back(innerResult); - } - return result; - } - - template - std::string parseArray(std::string (TgTypeParser::*const parseFunc)(const std::shared_ptr&) const, const std::vector>& objects) const { - std::string result; - result += '['; - for (const std::shared_ptr& item : objects) { - result += (this->*parseFunc)(item); - result += ','; - } - result.erase(result.length() - 1); - result += ']'; - return result; - } - - template - std::string parse2DArray(std::string (TgTypeParser::*const parseFunc)(const std::shared_ptr&) const, const std::vector>>& objects) const { - std::string result; - result += '['; - for (const std::vector>& item : objects) { - result += parseArray(parseFunc, item); - result += ','; - } - result.erase(result.length() - 1); - result += ']'; - return result; - } + template + using JsonToTgTypeFunc = std::shared_ptr (TgTypeParser::*)(const boost::property_tree::ptree&) const; + + template + using TgTypeToJsonFunc = std::string (TgTypeParser::*)(const std::shared_ptr&) const; + + static TgTypeParser& getInstance(); + + User::Ptr parseJsonAndGetUser(const boost::property_tree::ptree& data) const; + std::string parseUser(const User::Ptr& object) const; + GroupChat::Ptr parseJsonAndGetGroupChat(const boost::property_tree::ptree& data) const; + std::string parseGroupChat(const GroupChat::Ptr& object) const; + Message::Ptr parseJsonAndGetMessage(const boost::property_tree::ptree& data) const; + std::string parseMessage(const Message::Ptr& object) const; + PhotoSize::Ptr parseJsonAndGetPhotoSize(const boost::property_tree::ptree& data) const; + std::string parsePhotoSize(const PhotoSize::Ptr& object) const; + Audio::Ptr parseJsonAndGetAudio(const boost::property_tree::ptree& data) const; + std::string parseAudio(const Audio::Ptr& object) const; + Document::Ptr parseJsonAndGetDocument(const boost::property_tree::ptree& data) const; + std::string parseDocument(const Document::Ptr& object) const; + Sticker::Ptr parseJsonAndGetSticker(const boost::property_tree::ptree& data) const; + std::string parseSticker(const Sticker::Ptr& object) const; + Video::Ptr parseJsonAndGetVideo(const boost::property_tree::ptree& data) const; + std::string parseVideo(const Video::Ptr& object) const; + Contact::Ptr parseJsonAndGetContact(const boost::property_tree::ptree& data) const; + std::string parseContact(const Contact::Ptr& object) const; + Location::Ptr parseJsonAndGetLocation(const boost::property_tree::ptree& data) const; + std::string parseLocation(const Location::Ptr& object) const; + Update::Ptr parseJsonAndGetUpdate(const boost::property_tree::ptree& data) const; + std::string parseUpdate(const Update::Ptr& object) const; + UserProfilePhotos::Ptr parseJsonAndGetUserProfilePhotos(const boost::property_tree::ptree& data) const; + std::string parseUserProfilePhotos(const UserProfilePhotos::Ptr& object) const; + ReplyKeyboardMarkup::Ptr parseJsonAndGetReplyKeyboardMarkup(const boost::property_tree::ptree& data) const; + std::string parseReplyKeyboardMarkup(const ReplyKeyboardMarkup::Ptr& object) const; + ReplyKeyboardHide::Ptr parseJsonAndGetReplyKeyboardHide(const boost::property_tree::ptree& data) const; + std::string parseReplyKeyboardHide(const ReplyKeyboardHide::Ptr& object) const; + ForceReply::Ptr parseJsonAndGetForceReply(const boost::property_tree::ptree& data) const; + std::string parseForceReply(const ForceReply::Ptr& object) const; + GenericChat::Ptr parseJsonAndGetGenericChat(const boost::property_tree::ptree& data) const; + std::string parseGenericChat(const GenericChat::Ptr& object) const; + GenericReply::Ptr parseJsonAndGetGenericReply(const boost::property_tree::ptree& data) const; + std::string parseGenericReply(const GenericReply::Ptr& object) const; + + inline boost::property_tree::ptree parseJson(const std::string& json) const { + boost::property_tree::ptree tree; + std::istringstream input(json); + boost::property_tree::read_json(input, tree); + return tree; + } + + template + std::shared_ptr tryParseJson(JsonToTgTypeFunc parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { + auto treeItem = data.find(keyName); + if (treeItem == data.not_found()) { + return std::shared_ptr(); + } + return (this->*parseFunc)(treeItem->second); + } + + template + std::vector> parseJsonAndGetArray(JsonToTgTypeFunc parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { + std::vector> result; + auto treeItem = data.find(keyName); + if (treeItem == data.not_found()) { + return result; + } + for (const std::pair& innerTreeItem : treeItem->second) { + result.push_back((this->*parseFunc)(innerTreeItem.second)); + } + return result; + } + + template + std::vector>> parseJsonAndGet2DArray(JsonToTgTypeFunc parseFunc, const boost::property_tree::ptree& data, const std::string& keyName) const { + std::vector>> result; + auto treeItem = data.find(keyName); + if (treeItem == data.not_found()) { + return result; + } + for (const std::pair& innerTreeItem : treeItem->second) { + std::vector> innerResult; + for (const std::pair& innerInnerTreeItem : innerTreeItem.second) { + innerResult.push_back((this->*parseFunc)(innerInnerTreeItem.second)); + } + result.push_back(innerResult); + } + return result; + } + + template + std::string parseArray(TgTypeToJsonFunc parseFunc, const std::vector>& objects) const { + std::string result; + result += '['; + for (const std::shared_ptr& item : objects) { + result += (this->*parseFunc)(item); + result += ','; + } + result.erase(result.length() - 1); + result += ']'; + return result; + } + + template + std::string parse2DArray(TgTypeToJsonFunc parseFunc, const std::vector>>& objects) const { + std::string result; + result += '['; + for (const std::vector>& item : objects) { + result += parseArray(parseFunc, item); + result += ','; + } + result.erase(result.length() - 1); + result += ']'; + return result; + } private: - template - void appendToJson(std::string& json, const std::string& varName, const T& value) const { - if (value == 0) { - return; - } - json += '"'; - json += varName; - json += "\":"; - json += value; - json += ','; - } - - void appendToJson(std::string& json, const std::string& varName, const std::string& value) const; + template + void appendToJson(std::string& json, const std::string& varName, const T& value) const { + if (value == 0) { + return; + } + json += '"'; + json += varName; + json += "\":"; + json += value; + json += ','; + } + + void appendToJson(std::string& json, const std::string& varName, const std::string& value) const; }; } diff --git a/include/tgbot/doxygenMain.h b/include/tgbot/doxygenMain.h index 0d25e61..d77efa4 100644 --- a/include/tgbot/doxygenMain.h +++ b/include/tgbot/doxygenMain.h @@ -36,7 +36,7 @@ * Firstly you need to install some dependencies. You have to have boost library at the runtime and cmake at the compilation step to be able to use this library. * On Debian-based distibutives you can do it with these commands: * @code{.sh} - * sudo apt-get install cmake libboost-dev + * sudo apt-get install g++ make binutils cmake libssl-dev libboost-system-dev libboost-iostreams-dev libboost-test-dev * @endcode * * To compile the library execute this commands: diff --git a/include/tgbot/net/HttpClient.h b/include/tgbot/net/HttpClient.h index d39e108..e695398 100644 --- a/include/tgbot/net/HttpClient.h +++ b/include/tgbot/net/HttpClient.h @@ -43,17 +43,17 @@ public: /** * Returns instance which lives during all application lifetime. */ - static HttpClient& getInstance(); + static HttpClient& getInstance(); - /** - * Sends a request to the url. - * If there's no args specified, a GET request will be sent, otherwise a POST request will be sent. - * If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded. - */ - std::string makeRequest(const Url& url, const std::vector& args); + /** + * Sends a request to the url. + * If there's no args specified, a GET request will be sent, otherwise a POST request will be sent. + * If at least 1 arg is marked as file, the content type of a request will be multipart/form-data, otherwise it will be application/x-www-form-urlencoded. + */ + std::string makeRequest(const Url& url, const std::vector& args); private: - boost::asio::io_service _ioService; + boost::asio::io_service _ioService; }; } diff --git a/include/tgbot/net/HttpParser.h b/include/tgbot/net/HttpParser.h index dcb857b..83068fb 100644 --- a/include/tgbot/net/HttpParser.h +++ b/include/tgbot/net/HttpParser.h @@ -34,33 +34,33 @@ namespace TgBot { class HttpParser { public: - static HttpParser& getInstance(); + static HttpParser& getInstance(); - std::string generateRequest(const Url& url, const std::vector& args, bool isKeepAlive = false); - std::string generateMultipartFormData(const std::vector& args, const std::string& bondary); - std::string generateMultipartBoundary(const std::vector& args); - std::string generateWwwFormUrlencoded(const std::vector& args); - std::string generateResponse(const std::string& data, const std::string& mimeType = "text/plain", short unsigned statusCode = 200, const std::string& statusStr = "OK", bool isKeepAlive = false); + std::string generateRequest(const Url& url, const std::vector& args, bool isKeepAlive = false); + std::string generateMultipartFormData(const std::vector& args, const std::string& bondary); + std::string generateMultipartBoundary(const std::vector& args); + std::string generateWwwFormUrlencoded(const std::vector& args); + std::string generateResponse(const std::string& data, const std::string& mimeType = "text/plain", short unsigned statusCode = 200, const std::string& statusStr = "OK", bool isKeepAlive = false); - inline std::string parseRequest(const std::string& data, std::map& headers) { - return parseHttp(true, data, headers); - } + inline std::string parseRequest(const std::string& data, std::map& headers) { + return parseHttp(true, data, headers); + } - inline std::string parseRequest(const std::string& data) { - return parseHttp(true, data); - } + inline std::string parseRequest(const std::string& data) { + return parseHttp(true, data); + } - inline std::string parseResponse(const std::string& data, std::map& headers) { - return parseHttp(false, data, headers); - } + inline std::string parseResponse(const std::string& data, std::map& headers) { + return parseHttp(false, data, headers); + } - inline std::string parseResponse(const std::string& data) { - return parseHttp(false, data); - } + inline std::string parseResponse(const std::string& data) { + return parseHttp(false, data); + } private: - std::string parseHttp(bool isRequest, const std::string& data, std::map& headers); - std::string parseHttp(bool isRequest, const std::string& data); + std::string parseHttp(bool isRequest, const std::string& data, std::map& headers); + std::string parseHttp(bool isRequest, const std::string& data); }; } diff --git a/include/tgbot/net/HttpReqArg.h b/include/tgbot/net/HttpReqArg.h index d70e8b5..65f4f52 100644 --- a/include/tgbot/net/HttpReqArg.h +++ b/include/tgbot/net/HttpReqArg.h @@ -36,31 +36,31 @@ namespace TgBot { class HttpReqArg { public: - template - HttpReqArg(const std::string& name, const T& value, bool isFile = false, const std::string& mimeType = "text/plain") : - name(name), value(boost::lexical_cast(value)), isFile(isFile), mimeType(mimeType) - { - } + template + HttpReqArg(const std::string& name, const T& value, bool isFile = false, const std::string& mimeType = "text/plain") : + name(name), value(boost::lexical_cast(value)), isFile(isFile), mimeType(mimeType) + { + } - /** - * Name of an argument. - */ - std::string name; + /** + * Name of an argument. + */ + std::string name; - /** - * Value of an argument. - */ - std::string value; + /** + * Value of an argument. + */ + std::string value; - /** - * Should be true if an argument value hold some file contents - */ - bool isFile = false; + /** + * Should be true if an argument value hold some file contents + */ + bool isFile = false; - /** - * Mime type of an argument value. This field makes sense only if isFile is true. - */ - std::string mimeType = "text/plain"; + /** + * Mime type of an argument value. This field makes sense only if isFile is true. + */ + std::string mimeType = "text/plain"; }; } diff --git a/include/tgbot/net/HttpServer.h b/include/tgbot/net/HttpServer.h index 0a66900..bac7382 100644 --- a/include/tgbot/net/HttpServer.h +++ b/include/tgbot/net/HttpServer.h @@ -39,63 +39,63 @@ template class HttpServer { private: - class Connection; + class Connection; public: - typedef std::function)> ServerHandler; - - HttpServer(std::shared_ptr>& acceptor, const ServerHandler& handler) : _acceptor(acceptor), _handler(handler) { - } - - /** - * Starts receiving new connections. - */ - void start() { - std::shared_ptr> socket(new boost::asio::basic_stream_socket(acceptor->get_io_service())); - std::shared_ptr> connection(new Connection(socket, _handler)); - acceptor->async_accept(*connection->socket, [this, connection]() { - connection->start(); - start(); - }); - _ioService.run(); - } - - /** - * Stops receiving new connections. - */ - void stop() { - _ioService.stop(); - } + typedef std::function)> ServerHandler; + + HttpServer(std::shared_ptr>& acceptor, const ServerHandler& handler) : _acceptor(acceptor), _handler(handler) { + } + + /** + * Starts receiving new connections. + */ + void start() { + std::shared_ptr> socket(new boost::asio::basic_stream_socket(acceptor->get_io_service())); + std::shared_ptr> connection(new Connection(socket, _handler)); + acceptor->async_accept(*connection->socket, [this, connection]() { + connection->start(); + start(); + }); + _ioService.run(); + } + + /** + * Stops receiving new connections. + */ + void stop() { + _ioService.stop(); + } private: - template - class Connection { - - public: - Connection(std::shared_ptr>& socket, const ServerHandler& handler) : socket(socket), _handler(handler) { - boost::asio::socket_base::keep_alive option(true); - socket.set_option(option); - } - - void start() { - data.reserve(10240); - socket->async_receive(data, [this]() { - std::map headers; - std::string body = HttpParser::parseResponse(data, headers); - socket->async_send(_handler(body, headers)); - }); - } - - std::shared_ptr> socket; - std::string data; - - private: - const ServerHandler _handler; - }; - - boost::asio::io_service _ioService; - std::shared_ptr> _acceptor; - const ServerHandler _handler; + template + class Connection { + + public: + Connection(std::shared_ptr>& socket, const ServerHandler& handler) : socket(socket), _handler(handler) { + boost::asio::socket_base::keep_alive option(true); + socket.set_option(option); + } + + void start() { + data.reserve(10240); + socket->async_receive(data, [this]() { + std::map headers; + std::string body = HttpParser::parseResponse(data, headers); + socket->async_send(_handler(body, headers)); + }); + } + + std::shared_ptr> socket; + std::string data; + + private: + const ServerHandler _handler; + }; + + boost::asio::io_service _ioService; + std::shared_ptr> _acceptor; + const ServerHandler _handler; }; } diff --git a/include/tgbot/net/TgLongPoll.h b/include/tgbot/net/TgLongPoll.h index 932fc6d..949a2a4 100644 --- a/include/tgbot/net/TgLongPoll.h +++ b/include/tgbot/net/TgLongPoll.h @@ -36,18 +36,18 @@ namespace TgBot { class TgLongPoll { public: - TgLongPoll(const Api* api, const EventHandler* eventHandler); - TgLongPoll(const Bot& bot); + TgLongPoll(const Api* api, const EventHandler* eventHandler); + TgLongPoll(const Bot& bot); - /** - * Starts long poll. After new update will come, this method will parse it and send to EventHandler which invokes your listeners. Designed to be executed in a loop. - */ - void start(); + /** + * Starts long poll. After new update will come, this method will parse it and send to EventHandler which invokes your listeners. Designed to be executed in a loop. + */ + void start(); private: - int32_t _lastUpdateId = 0; - const Api* _api; - const EventHandler* _eventHandler; + int32_t _lastUpdateId = 0; + const Api* _api; + const EventHandler* _eventHandler; }; } diff --git a/include/tgbot/net/TgWebhookLocalServer.h b/include/tgbot/net/TgWebhookLocalServer.h index 7835f28..8faf61d 100644 --- a/include/tgbot/net/TgWebhookLocalServer.h +++ b/include/tgbot/net/TgWebhookLocalServer.h @@ -34,16 +34,16 @@ namespace TgBot { class TgWebhookLocalServer : public TgWebhookServer { public: - TgWebhookLocalServer(std::shared_ptr>& acceptor, const std::string& path, EventHandler* eventHandler) = delete; + TgWebhookLocalServer(std::shared_ptr>& acceptor, const std::string& path, EventHandler* eventHandler) = delete; - TgWebhookLocalServer(const std::string& path, const EventHandler* eventHandler) : - TgWebhookServer(std::shared_ptr>(new boost::asio::local::stream_protocol::acceptor(_ioService, boost::asio::local::stream_protocol::endpoint(path))), path, eventHandler) - { - } + TgWebhookLocalServer(const std::string& path, const EventHandler* eventHandler) : + TgWebhookServer(std::shared_ptr>(new boost::asio::local::stream_protocol::acceptor(_ioService, boost::asio::local::stream_protocol::endpoint(path))), path, eventHandler) + { + } - TgWebhookLocalServer(const std::string& path, const Bot& bot) : TgWebhookLocalServer(path, &bot.getEventHandler()) { + TgWebhookLocalServer(const std::string& path, const Bot& bot) : TgWebhookLocalServer(path, &bot.getEventHandler()) { - } + } }; } diff --git a/include/tgbot/net/TgWebhookServer.h b/include/tgbot/net/TgWebhookServer.h index a8155ff..f583093 100644 --- a/include/tgbot/net/TgWebhookServer.h +++ b/include/tgbot/net/TgWebhookServer.h @@ -34,22 +34,22 @@ template class TgWebhookServer : public HttpServer { public: - TgWebhookServer(std::shared_ptr>& acceptor, const ServerHandler& handler) = delete; - - TgWebhookServer(std::shared_ptr>& acceptor, const std::string& path, const EventHandler* eventHandler) : - HttpServer(acceptor, [this, eventHandler, &path](const std::string& data, const std::map& headers) -> std::string { - if (headers["method"] == "POST" && headers["path"] == path) { - eventHandler->handleUpdate(TgTypeParser::getInstance().parseUpdate(TgTypeParser::getInstance().parseJson(data))); - } - return HttpParser::generateResponse(""); - }) - { - } - - TgWebhookServer(std::shared_ptr>& acceptor, const std::string& path, const Bot& bot) : - TgWebhookServer(acceptor, path, &bot.getEventHandler()) - { - } + TgWebhookServer(std::shared_ptr>& acceptor, const ServerHandler& handler) = delete; + + TgWebhookServer(std::shared_ptr>& acceptor, const std::string& path, const EventHandler* eventHandler) : + HttpServer(acceptor, [this, eventHandler, &path](const std::string& data, const std::map& headers) -> std::string { + if (headers["method"] == "POST" && headers["path"] == path) { + eventHandler->handleUpdate(TgTypeParser::getInstance().parseUpdate(TgTypeParser::getInstance().parseJson(data))); + } + return HttpParser::generateResponse(""); + }) + { + } + + TgWebhookServer(std::shared_ptr>& acceptor, const std::string& path, const Bot& bot) : + TgWebhookServer(acceptor, path, &bot.getEventHandler()) + { + } }; } diff --git a/include/tgbot/net/TgWebhookTcpServer.h b/include/tgbot/net/TgWebhookTcpServer.h index cac2638..417b162 100644 --- a/include/tgbot/net/TgWebhookTcpServer.h +++ b/include/tgbot/net/TgWebhookTcpServer.h @@ -34,15 +34,15 @@ namespace TgBot { class TgWebhookTcpServer : public TgWebhookServer { public: - TgWebhookTcpServer(std::shared_ptr>& acceptor, const std::string& path, EventHandler* eventHandler) = delete; + TgWebhookTcpServer(std::shared_ptr>& acceptor, const std::string& path, EventHandler* eventHandler) = delete; - TgWebhookTcpServer(unsigned short port, const std::string& path, const EventHandler* eventHandler) : - TgWebhookServer(std::shared_ptr>(new boost::asio::ip::tcp::acceptor(_ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))), path, eventHandler) - { - } + TgWebhookTcpServer(unsigned short port, const std::string& path, const EventHandler* eventHandler) : + TgWebhookServer(std::shared_ptr>(new boost::asio::ip::tcp::acceptor(_ioService, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port))), path, eventHandler) + { + } - TgWebhookTcpServer(const std::string& path, const Bot& bot) : TgWebhookTcpServer(path, &bot.getEventHandler()) { - } + TgWebhookTcpServer(const std::string& path, const Bot& bot) : TgWebhookTcpServer(path, &bot.getEventHandler()) { + } }; } diff --git a/include/tgbot/net/Url.h b/include/tgbot/net/Url.h index 818e934..56f502a 100644 --- a/include/tgbot/net/Url.h +++ b/include/tgbot/net/Url.h @@ -34,32 +34,32 @@ namespace TgBot { class Url { public: - Url(const std::string& url); + Url(const std::string& url); /** * Protocol part of an url. Example: https:// */ - std::string protocol; + std::string protocol; /** * Host part of an url. Example: www.example.com */ - std::string host; + std::string host; /** * Path part of an url including preceding '/' char. Example: /index.html */ - std::string path; + std::string path; /** * Query part of an url without '?' char. Example: a=1&b=2&c=3 */ - std::string query; + std::string query; /** * Fragment part of an url without '#' char. Example: section1 */ - std::string fragment; + std::string fragment; }; } diff --git a/include/tgbot/tools/StringTools.h b/include/tgbot/tools/StringTools.h index 6c01575..8df7434 100644 --- a/include/tgbot/tools/StringTools.h +++ b/include/tgbot/tools/StringTools.h @@ -82,9 +82,9 @@ std::string urlDecode(const std::string& value); * @return Array of substrings */ inline std::vector split(const std::string& str, char delimiter) { - std::vector result; - split(str, delimiter, result); - return result; + std::vector result; + split(str, delimiter, result); + return result; } } diff --git a/include/tgbot/types/Audio.h b/include/tgbot/types/Audio.h index 8e255fe..e39eb00 100644 --- a/include/tgbot/types/Audio.h +++ b/include/tgbot/types/Audio.h @@ -35,27 +35,27 @@ namespace TgBot { class Audio { public: - typedef std::shared_ptr