diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/tgbot/EventBroadcaster.h | 24 | ||||
-rw-r--r-- | include/tgbot/EventHandler.h | 40 | ||||
-rw-r--r-- | include/tgbot/TgTypeParser.h | 3 | ||||
-rw-r--r-- | include/tgbot/tgbot.h | 15 | ||||
-rw-r--r-- | include/tgbot/tools/FileTools.h | 25 | ||||
-rw-r--r-- | include/tgbot/types/InputFile.h | 5 |
6 files changed, 57 insertions, 55 deletions
diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h index ebcf72a..2bb2d2a 100644 --- a/include/tgbot/EventBroadcaster.h +++ b/include/tgbot/EventBroadcaster.h @@ -46,10 +46,10 @@ class EventBroadcaster { 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; - typedef std::function<void (const CallbackQuery::Ptr&)> CallbackQueryListener; + typedef std::function<void (const Message::Ptr)> MessageListener; + typedef std::function<void (const InlineQuery::Ptr)> InlineQueryListener; + typedef std::function<void (const ChosenInlineResult::Ptr)> ChosenInlineResultListener; + typedef std::function<void (const CallbackQuery::Ptr)> CallbackQueryListener; /** * Registers listener which receives all messages which the bot can ever receive. @@ -118,7 +118,7 @@ public: private: template<typename ListenerType, typename ObjectType> - inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType& object) const { + inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType object) const { if (!object) return; @@ -127,11 +127,11 @@ 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 { + 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()) { return false; @@ -140,23 +140,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 63ecde3..7fa68ee 100644 --- a/include/tgbot/EventHandler.h +++ b/include/tgbot/EventHandler.h @@ -31,50 +31,16 @@ namespace TgBot { class EventHandler { - void handleMessage(const Message::Ptr& message) const { - _broadcaster->broadcastAnyMessage(message); - - if (StringTools::startsWith(message->text, "/")) { - unsigned long splitPosition; - 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 == message->text.npos) { - splitPosition = spacePosition; - } else { - splitPosition = std::min(spacePosition, atSymbolPosition); - } - std::string command = message->text.substr(1, splitPosition - 1); - if (!_broadcaster->broadcastCommand(command, message)) { - _broadcaster->broadcastUnknownCommand(message); - } - } else { - _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->callbackQuery != NULL) - _broadcaster->broadcastCallbackQuery(update->callbackQuery); - if (update->message != NULL) - handleMessage(update->message); - } + void handleUpdate(const Update::Ptr update) const; private: const EventBroadcaster* _broadcaster; + + void handleMessage(const Message::Ptr message) const; }; } diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h index 89c6dec..e593d6a 100644 --- a/include/tgbot/TgTypeParser.h +++ b/include/tgbot/TgTypeParser.h @@ -361,9 +361,6 @@ private: } void appendToJson(std::string& json, const std::string& varName, const bool& value) const { - if (value == 0) { - return; - } json += '"'; json += varName; json += "\":"; diff --git a/include/tgbot/tgbot.h b/include/tgbot/tgbot.h index 351410b..b9d93c3 100644 --- a/include/tgbot/tgbot.h +++ b/include/tgbot/tgbot.h @@ -54,6 +54,7 @@ #include "tgbot/types/InlineQueryResultVideo.h" #include "tgbot/types/ChosenInlineResult.h" #include "tgbot/tools/StringTools.h" +#include "tgbot/tools/FileTools.h" #include "tgbot/net/HttpClient.h" #include "tgbot/net/HttpParser.h" #include "tgbot/net/HttpReqArg.h" @@ -71,8 +72,9 @@ * @defgroup tools * * @mainpage + * [Go to GitHub](https://github.com/reo7sp) * - * @section Compilation + * @section lib_compile Library compilation * * Firstly you need to install some dependencies such as Boost and build tools such as CMake. On Debian-based distibutives you can do it with these commands: * @code{.sh} @@ -87,9 +89,16 @@ * sudo make install * @endcode * - * That's all. All you have to do now is just link compiled library to your project. + * @section bot_compile Bot compilation + * With CMake: + * [Example CMakeLists.txt](samples/echobot/CMakeLists.txt) * - * If you want, you can also use Docker to build and run your bot. Just set the base image of your's Dockerfile to reo7sp/tgbot-cpp. + * Without CMake: + * @code{.sh} + * g++ telegram_bot.cpp -o telegram_bot --std=c++11 -I/usr/local/include -lTgBot -lboost_system -lboost_iostreams -lssl -lcrypto -lpthread + * @endcode + * + * You can use Docker to build and run your bot. Set the base image of your's Dockerfile to [reo7sp/tgbot-cpp](https://hub.docker.com/r/reo7sp/tgbot-cpp/). * * @section Samples * All samples are located [here](https://github.com/reo7sp/tgbot-cpp/tree/master/samples) diff --git a/include/tgbot/tools/FileTools.h b/include/tgbot/tools/FileTools.h new file mode 100644 index 0000000..7329a8e --- /dev/null +++ b/include/tgbot/tools/FileTools.h @@ -0,0 +1,25 @@ +// +// Created by Oleg Morozenkov on 25.01.17. +// + +#ifndef TGBOT_FILETOOLS_H +#define TGBOT_FILETOOLS_H + +#include <string> + +/** + * @ingroup tools + */ +namespace FileTools { + +/** + * Reads whole file to string. + * @param filePath Path to a file + * @return string with file contents + */ +std::string read(const std::string& filePath); + +}; + + +#endif //TGBOT_FILETOOLS_H diff --git a/include/tgbot/types/InputFile.h b/include/tgbot/types/InputFile.h index f2403e3..c56ab7a 100644 --- a/include/tgbot/types/InputFile.h +++ b/include/tgbot/types/InputFile.h @@ -52,6 +52,11 @@ public: * File name. */ std::string fileName; + + /** + * Creates new InputFile::Ptr from an existing file. + */ + static InputFile::Ptr fromFile(const std::string& filePath, const std::string& mimeType); }; } |