diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | include/tgbot/Api.h | 65 | ||||
-rw-r--r-- | include/tgbot/EventBroadcaster.h | 16 | ||||
-rw-r--r-- | include/tgbot/EventHandler.h | 4 | ||||
-rw-r--r-- | include/tgbot/TgException.h | 2 | ||||
-rw-r--r-- | include/tgbot/net/HttpParser.h | 2 | ||||
-rw-r--r-- | include/tgbot/net/TgLongPoll.h | 4 | ||||
-rw-r--r-- | include/tgbot/types/Poll.h | 6 | ||||
-rw-r--r-- | include/tgbot/types/PollOption.h | 2 | ||||
-rw-r--r-- | src/Api.cpp | 50 | ||||
-rw-r--r-- | src/EventHandler.cpp | 4 | ||||
-rw-r--r-- | src/TgException.cpp | 2 | ||||
-rw-r--r-- | src/TgTypeParser.cpp | 12 | ||||
-rw-r--r-- | src/net/CurlHttpClient.cpp | 8 | ||||
-rw-r--r-- | src/net/HttpParser.cpp | 1 | ||||
-rw-r--r-- | src/net/TgLongPoll.cpp | 12 | ||||
-rw-r--r-- | src/net/Url.cpp | 2 | ||||
-rw-r--r-- | src/tools/FileTools.cpp | 2 | ||||
-rw-r--r-- | src/tools/StringTools.cpp | 11 | ||||
-rw-r--r-- | src/types/InputFile.cpp | 5 |
20 files changed, 104 insertions, 109 deletions
@@ -13,4 +13,5 @@ Thumbs.db TgBot_test .idea/ .vscode/ -docs/
\ No newline at end of file +docs/ +cmake-build-*
\ No newline at end of file diff --git a/include/tgbot/Api.h b/include/tgbot/Api.h index 9738596..ac37231 100644 --- a/include/tgbot/Api.h +++ b/include/tgbot/Api.h @@ -87,7 +87,7 @@ public: * @param disableNotification Optional. Sends the message silenty. * @return On success, the sent message is returned. */ - Message::Ptr sendPhoto(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> photo, const std::string& caption = "", std::int32_t replyToMessageId = 0, + Message::Ptr sendPhoto(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> photo, const std::string& caption = "", std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), const std::string& parseMode = "", bool disableNotification = false) const; /** @@ -105,8 +105,8 @@ public: * @param disableNotification Optional. Sends the message silenty. * @return On success, the sent message is returned. */ - Message::Ptr sendAudio(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> audio, const std::string &caption = "", std::int32_t duration = 0, - const std::string& performer = "", const std::string& title = "", const boost::variant<InputFile::Ptr, std::string> thumb = "", std::int32_t replyToMessageId = 0, + Message::Ptr sendAudio(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> audio, const std::string &caption = "", std::int32_t duration = 0, + const std::string& performer = "", const std::string& title = "", boost::variant<InputFile::Ptr, std::string> thumb = "", std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), const std::string& parseMode = "", bool disableNotification = false) const; /** @@ -121,7 +121,7 @@ public: * @param disableNotification Optional. Sends the message silenty. * @return On success, the sent message is returned. */ - Message::Ptr sendDocument(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> document, const boost::variant<InputFile::Ptr, std::string> thumb = "", const std::string &caption = "", std::int32_t replyToMessageId = 0, + Message::Ptr sendDocument(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> document, boost::variant<InputFile::Ptr, std::string> thumb = "", const std::string &caption = "", std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), const std::string& parseMode = "", bool disableNotification = false) const; /** @@ -194,7 +194,7 @@ public: * @param disableNotification Optional. Sends the message silenty. * @return On success, the sent message is returned. */ - Message::Ptr sendSticker(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId = 0, + Message::Ptr sendSticker(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), bool disableNotification = false) const; /** @@ -270,7 +270,7 @@ public: * @return On success, the sent message is returned. */ - Message::Ptr sendVideo(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming = false, std::int32_t duration = 0, std::int32_t width = 0, std::int32_t height = 0, const boost::variant<InputFile::Ptr, std::string> thumb = "", const std::string& caption = "", + Message::Ptr sendVideo(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming = false, std::int32_t duration = 0, std::int32_t width = 0, std::int32_t height = 0, boost::variant<InputFile::Ptr, std::string> thumb = "", const std::string& caption = "", std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), const std::string& parseMode = "", bool disableNotification = false) const; /** @@ -291,7 +291,7 @@ public: * @param disableNotification Optional. Sends the message silenty. * @return On success, the sent Message is returned. */ - Message::Ptr sendAnimation(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> animation, std::int32_t duration = 0, std::int32_t width = 0, std::int32_t height = 0, const boost::variant<InputFile::Ptr, std::string> thumb = "", const std::string &caption = "", + Message::Ptr sendAnimation(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> animation, std::int32_t duration = 0, std::int32_t width = 0, std::int32_t height = 0, boost::variant<InputFile::Ptr, std::string> thumb = "", const std::string &caption = "", std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), const std::string& parseMode = "", bool disableNotification = false) const; /** * @brief Use this method to send video messages. On success, the sent Message is returned. @@ -305,8 +305,8 @@ public: * @param replyMarkup Additional interface options. A object for an inline keyboard, custom reply keyboard, instructions to remove reply keyboard or to force a reply from the user. * @return On success, the sent Message is returned. */ - Message::Ptr sendVideoNote(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId = 0, bool disableNotification = false, - std::int32_t duration = 0, std::int32_t length = 0, const boost::variant<InputFile::Ptr, std::string> thumb = "", GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>()) const; + Message::Ptr sendVideoNote(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId = 0, bool disableNotification = false, + std::int32_t duration = 0, std::int32_t length = 0, boost::variant<InputFile::Ptr, std::string> thumb = "", GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>()) const; /** * @brief Use this method to send a group of photos or videos as an album. @@ -331,7 +331,7 @@ public: * @param disableNotification Optional. Sends the message silenty. * @return On success, the sent message is returned. */ - Message::Ptr sendVoice(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> voice, const std::string& caption = "", int duration = 0, std::int32_t replyToMessageId = 0, + Message::Ptr sendVoice(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> voice, const std::string& caption = "", int duration = 0, std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>(), const std::string& parseMode = "", bool disableNotification = false) const; /** @@ -560,7 +560,7 @@ public: * @return Message object on success, otherwise nullptr */ Message::Ptr editMessageReplyMarkup(std::int64_t chatId = 0, std::int32_t messageId = 0, const std::string& inlineMessageId = "", - const GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>()) const; + GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>()) const; /** * @brief Use this method to delete messages sent by bot (or by other users if bot is admin). @@ -787,29 +787,26 @@ public: std::string downloadFile(const std::string& filePath, const std::vector<HttpReqArg>& args = std::vector<HttpReqArg>()) const; /** - * @brief Use this method to send a poll. - * @param chatId Unique identifier for the target chat or username of the target channel. - * @param question Poll question, 1-255 characters. - * @param options List of answer options, 2-10 strings 1-100 characters each. - * @param disable_notification Optional. Sends the message silenty. - * @param reply_to_message_id Optional. If the message is a reply, ID of the original message. - * @param reply_markup 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 sendPoll(std::int64_t chatId, std::string question, std::vector<std::string> options, bool disable_notification=false, std::int32_t reply_to_message_id=0,GenericReply::Ptr reply_markup = std::make_shared<GenericReply>()) const; - - /** - * @brief Use this method to stop a poll which was sent by the bot. On success, the stopped Poll with the final results is returned.. - * @param chatId Unique identifier for the target chat or username of the target channel. - * @param question Poll question, 1-255 characters. - * @param options List of answer options, 2-10 strings 1-100 characters each. - * @param disable_notification Optional. Sends the message silenty. - * @param reply_to_message_id Optional. If the message is a reply, ID of the original message. - * @param reply_markup 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. - */ + * @brief Use this method to send a poll. + * @param chatId Unique identifier for the target chat or username of the target channel. + * @param question Poll question, 1-255 characters. + * @param options List of answer options, 2-10 strings 1-100 characters each. + * @param disableNotification Optional. Sends the message silenty. + * @param replyToMessageId Optional. If the message is a reply, ID of the original message. + * @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 sendPoll(std::int64_t chatId, const std::string& question, const std::vector<std::string>& options, bool disableNotification = false, std::int32_t replyToMessageId = 0, GenericReply::Ptr replyMarkup = std::make_shared<GenericReply>()) const; + + /** + * @brief Use this method to stop a poll which was sent by the bot. + * @param chatId Unique identifier for the target chat or username of the target channel. + * @param messageId Identifier of the original message with the poll. + * @param replyMarkup Optional. A JSON-serialized object for a new message inline keyboard. + * + * @return On success, the stopped Poll with the final results is returned. + */ Poll::Ptr stopPoll(std::int64_t chatId, std::int64_t messageId, InlineKeyboardMarkup::Ptr replyMarkup = std::make_shared<InlineKeyboardMarkup>()) const; diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index 38d5be6..978a6fe 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -107,12 +107,12 @@ private: } } - inline void broadcastAnyMessage(const Message::Ptr message) const { + 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::unordered_map<std::string, MessageListener>::const_iterator iter = _onCommandListeners.find(command); + inline bool broadcastCommand(const std::string& command, const Message::Ptr& message) const { + auto iter = _onCommandListeners.find(command); if (iter == _onCommandListeners.end()) { return false; } @@ -120,23 +120,23 @@ private: return true; } - inline void broadcastUnknownCommand(const Message::Ptr message) const { + inline void broadcastUnknownCommand(const Message::Ptr& message) const { broadcast<MessageListener, Message::Ptr>(_onUnknownCommandListeners, message); } - inline void broadcastNonCommandMessage(const Message::Ptr message) const { + inline void broadcastNonCommandMessage(const Message::Ptr& message) const { broadcast<MessageListener, Message::Ptr>(_onNonCommandMessageListeners, message); } - inline void broadcastInlineQuery(const InlineQuery::Ptr query) const { + inline void broadcastInlineQuery(const InlineQuery::Ptr& query) const { broadcast<InlineQueryListener, InlineQuery::Ptr>(_onInlineQueryListeners, query); } - inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr result) const { + inline void broadcastChosenInlineResult(const ChosenInlineResult::Ptr& result) const { broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result); } - inline void broadcastCallbackQuery(const CallbackQuery::Ptr result) const { + inline void broadcastCallbackQuery(const CallbackQuery::Ptr& result) const { broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result); } diff --git a/include/tgbot/EventHandler.h b/include/tgbot/EventHandler.h index 4e77e33..3f92ca2 100644 --- a/include/tgbot/EventHandler.h +++ b/include/tgbot/EventHandler.h @@ -13,12 +13,12 @@ public: explicit EventHandler(const EventBroadcaster& broadcaster) : _broadcaster(broadcaster) { } - void handleUpdate(Update::Ptr update) const; + void handleUpdate(const Update::Ptr& update) const; private: const EventBroadcaster& _broadcaster; - void handleMessage(Message::Ptr message) const; + void handleMessage(const Message::Ptr& message) const; }; } diff --git a/include/tgbot/TgException.h b/include/tgbot/TgException.h index 47b11f2..e80711c 100644 --- a/include/tgbot/TgException.h +++ b/include/tgbot/TgException.h @@ -16,7 +16,7 @@ namespace TgBot { class TGBOT_API TgException : public std::runtime_error { public: - explicit TgException(const std::string description); + explicit TgException(const std::string& description); }; } diff --git a/include/tgbot/net/HttpParser.h b/include/tgbot/net/HttpParser.h index 3f1095b..fabeacf 100644 --- a/include/tgbot/net/HttpParser.h +++ b/include/tgbot/net/HttpParser.h @@ -14,7 +14,7 @@ class TGBOT_API HttpParser { public: std::string generateRequest(const Url& url, const std::vector<HttpReqArg>& args, bool isKeepAlive = false) const; - std::string generateMultipartFormData(const std::vector<HttpReqArg>& args, const std::string& bondary) const; + std::string generateMultipartFormData(const std::vector<HttpReqArg>& args, const std::string& boundary) const; std::string generateMultipartBoundary(const std::vector<HttpReqArg>& args) const; std::string generateWwwFormUrlencoded(const std::vector<HttpReqArg>& args) const; std::string generateResponse(const std::string& data, const std::string& mimeType, unsigned short statusCode, const std::string& statusStr, bool isKeepAlive) const; diff --git a/include/tgbot/net/TgLongPoll.h b/include/tgbot/net/TgLongPoll.h index b55338c..017c012 100644 --- a/include/tgbot/net/TgLongPoll.h +++ b/include/tgbot/net/TgLongPoll.h @@ -17,7 +17,7 @@ namespace TgBot { class TGBOT_API TgLongPoll { public: - TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t, std::int32_t, const std::shared_ptr<std::vector<std::string>>&); + TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t, std::int32_t, std::shared_ptr<std::vector<std::string>>); TgLongPoll(const Bot& bot, std::int32_t = 100, std::int32_t = 10, const std::shared_ptr<std::vector<std::string>>& = nullptr); /** @@ -31,7 +31,7 @@ private: std::int32_t _lastUpdateId = 0; std::int32_t _limit; std::int32_t _timeout; - std::shared_ptr<std::vector<std::string>> _allowupdates; + std::shared_ptr<std::vector<std::string>> _allowUpdates; }; } diff --git a/include/tgbot/types/Poll.h b/include/tgbot/types/Poll.h index e3b0bd9..c4781a8 100644 --- a/include/tgbot/types/Poll.h +++ b/include/tgbot/types/Poll.h @@ -25,17 +25,17 @@ namespace TgBot { /** * @brief Poll question, 1-255 characters. */ - std::string question; + std::string question; /** * @brief List of poll options. */ - PollOption::Ptr options; + std::vector<PollOption::Ptr> options; /** * @brief True, if the poll is closed. */ - bool is_closed; + bool isClosed; }; } diff --git a/include/tgbot/types/PollOption.h b/include/tgbot/types/PollOption.h index b487b35..864aaf5 100644 --- a/include/tgbot/types/PollOption.h +++ b/include/tgbot/types/PollOption.h @@ -21,7 +21,7 @@ public: /** * @brief Number of users that voted for this option. */ - std::int64_t voter_count; + std::int64_t voterCount; }; } diff --git a/src/Api.cpp b/src/Api.cpp index 26e9ada..c1b112c 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -1,10 +1,11 @@ #include "tgbot/Api.h" #include <cstdint> +#include <tgbot/tools/StringTools.h> #include <utility> -#include "tgbot/TgTypeParser.h" #include "tgbot/TgException.h" +#include "tgbot/TgTypeParser.h" using namespace std; using namespace boost::property_tree; @@ -54,7 +55,7 @@ Message::Ptr Api::forwardMessage(std::int64_t chatId, std::int64_t fromChatId, s return _tgTypeParser.parseJsonAndGetMessage(sendRequest("forwardMessage", args)); } -Message::Ptr Api::sendPhoto(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> photo, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendPhoto(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> photo, const string& caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(7); args.emplace_back("chat_id", chatId); @@ -82,7 +83,7 @@ Message::Ptr Api::sendPhoto(std::int64_t chatId, const boost::variant<InputFile: return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPhoto", args)); } -Message::Ptr Api::sendAudio(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> audio, const string &caption, std::int32_t duration, const string& performer, const string& title, const boost::variant<InputFile::Ptr, std::string> thumb, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendAudio(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> audio, const string &caption, std::int32_t duration, const string& performer, const string& title, boost::variant<InputFile::Ptr, std::string> thumb, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(11); args.emplace_back("chat_id", chatId); @@ -128,7 +129,7 @@ Message::Ptr Api::sendAudio(std::int64_t chatId, const boost::variant<InputFile: return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAudio", args)); } -Message::Ptr Api::sendDocument(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> document, const boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendDocument(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> document, boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -256,7 +257,7 @@ bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok, } -Message::Ptr Api::sendSticker(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { +Message::Ptr Api::sendSticker(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> sticker, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(5); args.emplace_back("chat_id", chatId); @@ -293,7 +294,7 @@ File::Ptr Api::uploadStickerFile(std::int32_t userId, const InputFile::Ptr pngSt return _tgTypeParser.parseJsonAndGetFile(sendRequest("uploadStickerFile", args)); } -bool Api::createNewStickerSet(std::int32_t userId, const string& name, const string& title, const boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { +bool Api::createNewStickerSet(std::int32_t userId, const string& name, const string& title, boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, bool containsMasks, MaskPosition::Ptr maskPosition) const { vector<HttpReqArg> args; args.reserve(7); args.emplace_back("user_id", userId); @@ -315,7 +316,7 @@ bool Api::createNewStickerSet(std::int32_t userId, const string& name, const str return sendRequest("createNewStickerSet", args).get<bool>("", false); } -bool Api::addStickerToSet(std::int32_t userId, const string& name, const boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { +bool Api::addStickerToSet(std::int32_t userId, const string& name, boost::variant<InputFile::Ptr, std::string> pngSticker, const string& emojis, MaskPosition::Ptr maskPosition) const { vector<HttpReqArg> args; args.reserve(6); args.emplace_back("user_id", userId); @@ -348,7 +349,7 @@ bool Api::deleteStickerFromSet(const string& sticker) const { return sendRequest("deleteStickerFromSet", args).get<bool>("", false); } -Message::Ptr Api::sendVideo(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, const boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendVideo(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> video, bool supportsStreaming, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant<InputFile::Ptr, std::string> thumb, const string &caption, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(12); args.emplace_back("chat_id", chatId); @@ -397,7 +398,7 @@ Message::Ptr Api::sendVideo(std::int64_t chatId, const boost::variant<InputFile: return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendVideo", args)); } -Message::Ptr Api::sendAnimation(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> animation, std::int32_t duration, std::int32_t width, std::int32_t height, const boost::variant<InputFile::Ptr, std::string> thumb, const string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendAnimation(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> animation, std::int32_t duration, std::int32_t width, std::int32_t height, boost::variant<InputFile::Ptr, std::string> thumb, const string& caption, std::int32_t replyToMessageId, GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(11); args.emplace_back("chat_id", chatId); @@ -443,7 +444,7 @@ Message::Ptr Api::sendAnimation(std::int64_t chatId, const boost::variant<InputF return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendAnimation", args)); } -Message::Ptr Api::sendVideoNote(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId, bool disableNotification, std::int32_t duration, std::int32_t length, const boost::variant<InputFile::Ptr, std::string> thumb, const GenericReply::Ptr replyMarkup) const { +Message::Ptr Api::sendVideoNote(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> videoNote, std::int64_t replyToMessageId, bool disableNotification, std::int32_t duration, std::int32_t length, boost::variant<InputFile::Ptr, std::string> thumb, const GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -491,7 +492,7 @@ vector<Message::Ptr> Api::sendMediaGroup(std::int64_t chatId, const vector<Input return _tgTypeParser.parseJsonAndGetArray<Message>(&TgTypeParser::parseJsonAndGetMessage, sendRequest("sendMediaGroup", args)); } -Message::Ptr Api::sendVoice(std::int64_t chatId, const boost::variant<InputFile::Ptr, std::string> voice, const string &caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { +Message::Ptr Api::sendVoice(std::int64_t chatId, boost::variant<InputFile::Ptr, std::string> voice, const string &caption, int duration, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, const string& parseMode, bool disableNotification) const { vector<HttpReqArg> args; args.reserve(8); args.emplace_back("chat_id", chatId); @@ -1119,28 +1120,23 @@ void Api::deleteMessage(std::int64_t chatId, std::int32_t messageId) const { sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) }); } -Message::Ptr Api::sendPoll(std::int64_t chat_id, std::string question, std::vector<std::string> options, bool disable_notification, std::int32_t reply_to_message_id,GenericReply::Ptr reply_markup) const { +Message::Ptr Api::sendPoll(std::int64_t chatId, const std::string& question, const std::vector<std::string>& options, bool disableNotification, std::int32_t replyToMessageId, const GenericReply::Ptr replyMarkup) const { vector<HttpReqArg> args; args.reserve(6); - args.emplace_back("chat_id", chat_id); + args.emplace_back("chat_id", chatId); args.emplace_back("question", question); - std::string json_array; - json_array = "["; - for(const auto &arr : options){ - json_array += "\"" + arr + "\"" + ","; - } - json_array.pop_back(); - json_array += "]"; - args.emplace_back("options", json_array); + args.emplace_back("options", _tgTypeParser.parseArray<std::string>([] (const std::string& option) -> std::string { + return StringTools::urlEncode(option); + }, options)); - if (disable_notification){ - args.emplace_back("disable_notification", disable_notification); + if (disableNotification){ + args.emplace_back("disable_notification", disableNotification); } - if (reply_to_message_id!=0){ - args.emplace_back("reply_to_message_id", reply_to_message_id); + if (replyToMessageId != 0){ + args.emplace_back("reply_to_message_id", replyToMessageId); } - if (reply_markup){ - args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(reply_markup)); + if (replyMarkup){ + args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup)); } return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args)); } diff --git a/src/EventHandler.cpp b/src/EventHandler.cpp index cd67379..43f2eb1 100644 --- a/src/EventHandler.cpp +++ b/src/EventHandler.cpp @@ -7,7 +7,7 @@ using namespace std; namespace TgBot { -void EventHandler::handleUpdate(Update::Ptr update) const { +void EventHandler::handleUpdate(const Update::Ptr& update) const { if (update->inlineQuery != nullptr) { _broadcaster.broadcastInlineQuery(update->inlineQuery); } @@ -22,7 +22,7 @@ void EventHandler::handleUpdate(Update::Ptr update) const { } } -void EventHandler::handleMessage(Message::Ptr message) const { +void EventHandler::handleMessage(const Message::Ptr& message) const { _broadcaster.broadcastAnyMessage(message); if (StringTools::startsWith(message->text, "/")) { diff --git a/src/TgException.cpp b/src/TgException.cpp index 6ccd3d9..03a942c 100644 --- a/src/TgException.cpp +++ b/src/TgException.cpp @@ -2,7 +2,7 @@ namespace TgBot { -TgBot::TgException::TgException(const std::string description) : runtime_error(description) { +TgBot::TgException::TgException(const std::string& description) : runtime_error(description) { } } diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index e002c60..4d91d28 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -377,8 +377,8 @@ Poll::Ptr TgTypeParser::parseJsonAndGetPoll(const ptree& data) const { auto result(make_shared<Poll>()); result->id = data.get("id", 0); result->question = data.get("question", ""); - result->options = tryParseJson<PollOption>(&TgTypeParser::parseJsonAndGetPollOption, data, "options"); - result->is_closed = data.get<bool>("is_closed"); + result->options = parseJsonAndGetArray<PollOption>(&TgTypeParser::parseJsonAndGetPollOption, data, "options"); + result->isClosed = data.get<bool>("is_closed"); return result; } @@ -390,8 +390,8 @@ string TgTypeParser::parsePoll(const Poll::Ptr& object) const { result += '{'; appendToJson(result, "id", object->id); appendToJson(result, "question", object->question); - appendToJson(result, "options", parsePollOption(object->options)); - appendToJson(result, "is_closed", object->is_closed); + appendToJson(result, "options", parseArray(&TgTypeParser::parsePollOption, object->options)); + appendToJson(result, "is_closed", object->isClosed); removeLastComma(result); result += '}'; return result; @@ -400,7 +400,7 @@ string TgTypeParser::parsePoll(const Poll::Ptr& object) const { PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const ptree& data) const { auto result(make_shared<PollOption>()); result->text = data.get("text", ""); - result->voter_count = data.get("voter_count", 0); + result->voterCount = data.get("voter_count", 0); return result; } @@ -411,7 +411,7 @@ string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const { string result; result += '{'; appendToJson(result, "text", object->text); - appendToJson(result, "voter_count", object->voter_count); + appendToJson(result, "voter_count", object->voterCount); removeLastComma(result); result += '}'; return result; diff --git a/src/net/CurlHttpClient.cpp b/src/net/CurlHttpClient.cpp index 5bd9313..6255974 100644 --- a/src/net/CurlHttpClient.cpp +++ b/src/net/CurlHttpClient.cpp @@ -17,9 +17,9 @@ CurlHttpClient::~CurlHttpClient() { } static std::size_t curlWriteString(char* ptr, std::size_t size, std::size_t nmemb, void* userdata) { - static_cast<std::string *>(userdata)->append(ptr, size * nmemb); + static_cast<std::string*>(userdata)->append(ptr, size * nmemb); return size * nmemb; -}; +} std::string CurlHttpClient::makeRequest(const Url& url, const std::vector<HttpReqArg>& args) const { // Copy settings for each call because we change CURLOPT_URL and other stuff. @@ -34,8 +34,8 @@ std::string CurlHttpClient::makeRequest(const Url& url, const std::vector<HttpRe headers = curl_slist_append(headers, "Connection: close"); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); - curl_mime *mime; - curl_mimepart *part; + curl_mime* mime; + curl_mimepart* part; mime = curl_mime_init(curl); if (!args.empty()) { for (const HttpReqArg& a : args) { diff --git a/src/net/HttpParser.cpp b/src/net/HttpParser.cpp index 8006c85..2d3085f 100644 --- a/src/net/HttpParser.cpp +++ b/src/net/HttpParser.cpp @@ -81,7 +81,6 @@ string HttpParser::generateMultipartFormData(const vector<HttpReqArg>& args, con string HttpParser::generateMultipartBoundary(const vector<HttpReqArg>& args) const { string result; - srand((std::uint32_t) time(nullptr)); for (const HttpReqArg& item : args) { if (item.isFile) { while (result.empty() || item.value.find(result) != string::npos) { diff --git a/src/net/TgLongPoll.cpp b/src/net/TgLongPoll.cpp index 0138620..6d266f8 100644 --- a/src/net/TgLongPoll.cpp +++ b/src/net/TgLongPoll.cpp @@ -1,19 +1,21 @@ #include "tgbot/net/TgLongPoll.h" #include <cstdint> +#include <utility> namespace TgBot { -TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowupdates) - : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout), _allowupdates(allowupdates) { +TgLongPoll::TgLongPoll(const Api* api, const EventHandler* eventHandler, std::int32_t limit, std::int32_t timeout, std::shared_ptr<std::vector<std::string>> allowUpdates) + : _api(api), _eventHandler(eventHandler), _limit(limit), _timeout(timeout), + _allowUpdates(std::move(allowUpdates)) { } -TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowupdates) : - TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowupdates) { +TgLongPoll::TgLongPoll(const Bot& bot, std::int32_t limit, std::int32_t timeout, const std::shared_ptr<std::vector<std::string>>& allowUpdates) : + TgLongPoll(&bot.getApi(), &bot.getEventHandler(), limit, timeout, allowUpdates) { } void TgLongPoll::start() { - std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowupdates); + std::vector<Update::Ptr> updates = _api->getUpdates(_lastUpdateId, _limit, _timeout, _allowUpdates); for (Update::Ptr& item : updates) { if (item->updateId >= _lastUpdateId) { _lastUpdateId = item->updateId + 1; diff --git a/src/net/Url.cpp b/src/net/Url.cpp index 016a97f..95e31c7 100644 --- a/src/net/Url.cpp +++ b/src/net/Url.cpp @@ -2,8 +2,6 @@ #include <cstddef> -#include "tgbot/tools/StringTools.h" - using namespace std; namespace TgBot { diff --git a/src/tools/FileTools.cpp b/src/tools/FileTools.cpp index 408c710..d2c303c 100644 --- a/src/tools/FileTools.cpp +++ b/src/tools/FileTools.cpp @@ -23,4 +23,4 @@ void write(const string& content, const string& filePath) { out.close(); } -}; +} diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp index 1246c50..f49a78e 100644 --- a/src/tools/StringTools.cpp +++ b/src/tools/StringTools.cpp @@ -3,6 +3,7 @@ #include <cstdlib> #include <iomanip> #include <cstdio> +#include <random> using namespace std; @@ -58,8 +59,13 @@ string generateRandomString(std::size_t length) { static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\;',./!@#$%^&*()_+{}|:\"<>?`~"); static const std::size_t charsLen = chars.length(); string result; + + random_device randomDevice; + mt19937 randomSeed(randomDevice()); + uniform_int_distribution<int> generator(0, charsLen - 1); + for (std::size_t i = 0; i < length; ++i) { - result += chars[rand() % charsLen]; + result += chars[generator(randomSeed)]; } return result; } @@ -84,8 +90,7 @@ string urlDecode(const string& value) { for (std::size_t i = 0, count = value.length(); i < count; ++i) { const char c = value[i]; if (c == '%') { - int t = 0; - sscanf(value.substr(i + 1, 2).c_str(), "%x", &t); + int t = stoi(value.substr(i + 1, 2), nullptr, 16); result += (char) t; i += 2; } else { diff --git a/src/types/InputFile.cpp b/src/types/InputFile.cpp index e0885f0..0ca1991 100644 --- a/src/types/InputFile.cpp +++ b/src/types/InputFile.cpp @@ -1,7 +1,4 @@ #include "tgbot/types/InputFile.h" - -#include <fstream> - #include "tgbot/tools/StringTools.h" #include "tgbot/tools/FileTools.h" @@ -17,4 +14,4 @@ InputFile::Ptr InputFile::fromFile(const string& filePath, const string& mimeTyp return result; } -}; +} |