From 5eb0d10909451fa46b40f474325786d9962c000d Mon Sep 17 00:00:00 2001 From: Kirill Kirilenko Date: Sun, 2 Oct 2016 19:14:14 +0300 Subject: [PATCH] Add support for parse mode in messages. --- include/telebotxx/BotApi.hpp | 10 +++++++++- src/BotApi.cpp | 11 ++++++++--- tests/TestApi.cpp | 20 +++++++++++++++++++- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/include/telebotxx/BotApi.hpp b/include/telebotxx/BotApi.hpp index f64e3f2..ad7a296 100644 --- a/include/telebotxx/BotApi.hpp +++ b/include/telebotxx/BotApi.hpp @@ -11,6 +11,13 @@ namespace telebotxx class BotApi { public: + enum class ParseMode + { + Plain, + Markdown, + Html + }; + /// \param [in] token bot's secret token BotApi(const std::string& token); @@ -23,7 +30,8 @@ namespace telebotxx /// \brief Send text message /// \param [in] chat chat identifier /// \param [in] text message text - void sendMessage(const std::string& chat, const std::string& text); + /// \param [in] parseMode parse mode + void sendMessage(const std::string& chat, const std::string& text, ParseMode parseMode = ParseMode::Plain); /// \brief Send image /// \param [in] chat chat identifier diff --git a/src/BotApi.cpp b/src/BotApi.cpp index 61634ed..f6e921d 100644 --- a/src/BotApi.cpp +++ b/src/BotApi.cpp @@ -101,7 +101,7 @@ public: return parseUser(parseResponse(doc)); } - inline void sendMessage(const std::string& chat, const std::string& text) + inline void sendMessage(const std::string& chat, const std::string& text, ParseMode parseMode) { // Construct JSON body using namespace rapidjson; @@ -113,6 +113,11 @@ public: writer.String(chat.c_str()); writer.String("text"); writer.String(text.c_str()); + if (parseMode != ParseMode::Plain) + { + writer.String("parse_mode"); + writer.String((parseMode == ParseMode::Markdown) ? "Markdown" : "HTML"); + } writer.EndObject(); std::string request = s.GetString(); @@ -174,9 +179,9 @@ User BotApi::getMe() return impl_->getMe(); } -void BotApi::sendMessage(const std::string& chat, const std::string& text) +void BotApi::sendMessage(const std::string& chat, const std::string& text, ParseMode parseMode) { - return impl_->sendMessage(chat, text); + return impl_->sendMessage(chat, text, parseMode); } void BotApi::sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption) diff --git a/tests/TestApi.cpp b/tests/TestApi.cpp index 65d6175..a0b2e73 100644 --- a/tests/TestApi.cpp +++ b/tests/TestApi.cpp @@ -86,7 +86,25 @@ BOOST_AUTO_TEST_SUITE(TestBotApi) { PRINT_TESTNAME; BOOST_REQUIRE(bot); - BOOST_REQUIRE_NO_THROW(bot->sendMessage(chat, "Hello from C++!")); + BOOST_REQUIRE_NO_THROW(bot->sendMessage(chat, "Sample text")); + } + + BOOST_AUTO_TEST_CASE(SendMessageWithMarkdown) + { + PRINT_TESTNAME; + BOOST_REQUIRE(bot); + BOOST_REQUIRE_NO_THROW(bot->sendMessage(chat, + "[Sample text in markdown](http://google.com/)", + telebotxx::BotApi::ParseMode::Markdown)); + } + + BOOST_AUTO_TEST_CASE(SendMessageWithHtml) + { + PRINT_TESTNAME; + BOOST_REQUIRE(bot); + BOOST_REQUIRE_NO_THROW(bot->sendMessage(chat, + "Sample text in HTML", + telebotxx::BotApi::ParseMode::Html)); } BOOST_AUTO_TEST_CASE(SendPhoto)