Add support for parse mode in messages.

This commit is contained in:
Kirill Kirilenko 2016-10-02 19:14:14 +03:00
parent bb59c65848
commit 5eb0d10909
3 changed files with 36 additions and 5 deletions

View file

@ -11,6 +11,13 @@ namespace telebotxx
class BotApi class BotApi
{ {
public: public:
enum class ParseMode
{
Plain,
Markdown,
Html
};
/// \param [in] token bot's secret token /// \param [in] token bot's secret token
BotApi(const std::string& token); BotApi(const std::string& token);
@ -23,7 +30,8 @@ namespace telebotxx
/// \brief Send text message /// \brief Send text message
/// \param [in] chat chat identifier /// \param [in] chat chat identifier
/// \param [in] text message text /// \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 /// \brief Send image
/// \param [in] chat chat identifier /// \param [in] chat chat identifier

View file

@ -101,7 +101,7 @@ public:
return parseUser(parseResponse(doc)); 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 // Construct JSON body
using namespace rapidjson; using namespace rapidjson;
@ -113,6 +113,11 @@ public:
writer.String(chat.c_str()); writer.String(chat.c_str());
writer.String("text"); writer.String("text");
writer.String(text.c_str()); writer.String(text.c_str());
if (parseMode != ParseMode::Plain)
{
writer.String("parse_mode");
writer.String((parseMode == ParseMode::Markdown) ? "Markdown" : "HTML");
}
writer.EndObject(); writer.EndObject();
std::string request = s.GetString(); std::string request = s.GetString();
@ -174,9 +179,9 @@ User BotApi::getMe()
return impl_->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) void BotApi::sendPhoto(const std::string& chat, const std::string& filename, const std::string& caption)

View file

@ -86,7 +86,25 @@ BOOST_AUTO_TEST_SUITE(TestBotApi)
{ {
PRINT_TESTNAME; PRINT_TESTNAME;
BOOST_REQUIRE(bot); 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,
"<a href=\"http://google.com/\">Sample text in HTML</a>",
telebotxx::BotApi::ParseMode::Html));
} }
BOOST_AUTO_TEST_CASE(SendPhoto) BOOST_AUTO_TEST_CASE(SendPhoto)