mirror of
https://github.com/UltraCoderRU/telebotxx.git
synced 2026-01-28 04:05:13 +00:00
Added InlineKeyboard support for sendPhoto request.
This commit is contained in:
parent
c24136c461
commit
4c15e4c8de
7 changed files with 128 additions and 58 deletions
|
|
@ -18,10 +18,12 @@ public:
|
||||||
void setCaption(const Caption& caption);
|
void setCaption(const Caption& caption);
|
||||||
void setDisableNotification(const DisableNotification& disableNotification);
|
void setDisableNotification(const DisableNotification& disableNotification);
|
||||||
void setReplyToMessageId(const ReplyTo& replyToMessageId);
|
void setReplyToMessageId(const ReplyTo& replyToMessageId);
|
||||||
|
void setReplyMarkup(const ReplyMarkup& replyMarkup);
|
||||||
|
|
||||||
void setOption(const Caption& caption);
|
void setOption(const Caption& caption);
|
||||||
void setOption(const DisableNotification& disableNotification);
|
void setOption(const DisableNotification& disableNotification);
|
||||||
void setOption(const ReplyTo& replyToMessageId);
|
void setOption(const ReplyTo& replyToMessageId);
|
||||||
|
void setOption(const ReplyMarkup& replyMarkup);
|
||||||
|
|
||||||
Message execute();
|
Message execute();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ set(SOURCE_FILES Attachment.cpp
|
||||||
Message.cpp
|
Message.cpp
|
||||||
Update.cpp
|
Update.cpp
|
||||||
User.cpp
|
User.cpp
|
||||||
|
ReplyMarkup.cpp
|
||||||
RequestOptions.cpp
|
RequestOptions.cpp
|
||||||
SendMessageRequest.cpp
|
SendMessageRequest.cpp
|
||||||
SendPhotoRequest.cpp
|
SendPhotoRequest.cpp
|
||||||
|
|
|
||||||
66
src/ReplyMarkup.cpp
Normal file
66
src/ReplyMarkup.cpp
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
#include "ReplyMarkup.hpp"
|
||||||
|
|
||||||
|
namespace telebotxx {
|
||||||
|
|
||||||
|
void writeInlineKeyboardButton(rapidjson::Writer<rapidjson::StringBuffer>& writer, const InlineKeyboardButton& button)
|
||||||
|
{
|
||||||
|
writer.StartObject();
|
||||||
|
|
||||||
|
writer.String("text");
|
||||||
|
writer.String(button.getText().c_str());
|
||||||
|
|
||||||
|
switch (button.getActionType())
|
||||||
|
{
|
||||||
|
case InlineKeyboardButton::ActionType::Url:
|
||||||
|
{
|
||||||
|
writer.String("url");
|
||||||
|
writer.String(button.getUrl().getValue().c_str());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case InlineKeyboardButton::ActionType::CallbackData:
|
||||||
|
{
|
||||||
|
writer.String("callback_data");
|
||||||
|
writer.String(button.getCallbackData().getValue().c_str());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case InlineKeyboardButton::ActionType::SwitchInlineQuery:
|
||||||
|
{
|
||||||
|
writer.String("switch_inline_query");
|
||||||
|
writer.String(button.getSwitchInlineQuery().getValue().c_str());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case InlineKeyboardButton::ActionType::SwitchInlineQueryCurrentChat:
|
||||||
|
{
|
||||||
|
writer.String("switch_inline_query_current_chat");
|
||||||
|
writer.String(button.getSwitchInlineQueryCurrentChat().getValue().c_str());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
writer.EndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeInlineKeyboardMarkup(rapidjson::Writer<rapidjson::StringBuffer>& writer, const InlineKeyboardMarkup& markup)
|
||||||
|
{
|
||||||
|
writer.StartObject();
|
||||||
|
writer.String("inline_keyboard");
|
||||||
|
writer.StartArray();
|
||||||
|
auto rows = markup.getRows();
|
||||||
|
for (auto& row : rows)
|
||||||
|
{
|
||||||
|
writer.StartArray();
|
||||||
|
for (auto& button : row)
|
||||||
|
writeInlineKeyboardButton(writer, button);
|
||||||
|
writer.EndArray();
|
||||||
|
}
|
||||||
|
writer.EndArray();
|
||||||
|
writer.EndObject();
|
||||||
|
}
|
||||||
|
|
||||||
|
void writeReplyMarkup(rapidjson::Writer<rapidjson::StringBuffer>& writer, const ReplyMarkup& markup)
|
||||||
|
{
|
||||||
|
if (markup.getType() == ReplyMarkup::Type::InlineKeyboardMarkup)
|
||||||
|
writeInlineKeyboardMarkup(writer, markup.getInlineKeyboardMarkup());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
14
src/ReplyMarkup.hpp
Normal file
14
src/ReplyMarkup.hpp
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
#ifndef TELEBOTXX_REPLY_MARKUP_HPP
|
||||||
|
#define TELEBOTXX_REPLY_MARKUP_HPP
|
||||||
|
|
||||||
|
#include <telebotxx/RequestOptions.hpp>
|
||||||
|
|
||||||
|
#include <rapidjson/writer.h>
|
||||||
|
|
||||||
|
namespace telebotxx {
|
||||||
|
|
||||||
|
void writeReplyMarkup(rapidjson::Writer<rapidjson::StringBuffer>& writer, const ReplyMarkup& markup);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // TELEBOTXX_REPLY_MARKUP_HPP
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <telebotxx/SendMessageRequest.hpp>
|
#include <telebotxx/SendMessageRequest.hpp>
|
||||||
#include <telebotxx/Logging.hpp>
|
#include <telebotxx/Logging.hpp>
|
||||||
#include "JsonObjects.hpp"
|
#include "JsonObjects.hpp"
|
||||||
|
#include "ReplyMarkup.hpp"
|
||||||
|
|
||||||
#include <cpr/cpr.h>
|
#include <cpr/cpr.h>
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
|
|
@ -11,44 +12,6 @@
|
||||||
|
|
||||||
namespace telebotxx {
|
namespace telebotxx {
|
||||||
|
|
||||||
void writeInlineKeyboardButton(rapidjson::Writer<rapidjson::StringBuffer>& writer, const InlineKeyboardButton& button)
|
|
||||||
{
|
|
||||||
writer.StartObject();
|
|
||||||
|
|
||||||
writer.String("text");
|
|
||||||
writer.String(button.getText().c_str());
|
|
||||||
|
|
||||||
switch (button.getActionType())
|
|
||||||
{
|
|
||||||
case InlineKeyboardButton::ActionType::Url:
|
|
||||||
{
|
|
||||||
writer.String("url");
|
|
||||||
writer.String(button.getUrl().getValue().c_str());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case InlineKeyboardButton::ActionType::CallbackData:
|
|
||||||
{
|
|
||||||
writer.String("callback_data");
|
|
||||||
writer.String(button.getCallbackData().getValue().c_str());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case InlineKeyboardButton::ActionType::SwitchInlineQuery:
|
|
||||||
{
|
|
||||||
writer.String("switch_inline_query");
|
|
||||||
writer.String(button.getSwitchInlineQuery().getValue().c_str());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case InlineKeyboardButton::ActionType::SwitchInlineQueryCurrentChat:
|
|
||||||
{
|
|
||||||
writer.String("switch_inline_query_current_chat");
|
|
||||||
writer.String(button.getSwitchInlineQueryCurrentChat().getValue().c_str());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
writer.EndObject();
|
|
||||||
}
|
|
||||||
|
|
||||||
class SendMessageRequest::Impl
|
class SendMessageRequest::Impl
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
|
@ -134,26 +97,7 @@ public:
|
||||||
if (replyMarkup_)
|
if (replyMarkup_)
|
||||||
{
|
{
|
||||||
writer.String("reply_markup");
|
writer.String("reply_markup");
|
||||||
writer.StartObject();
|
writeReplyMarkup(writer, *replyMarkup_);
|
||||||
writer.String("inline_keyboard");
|
|
||||||
if (replyMarkup_->getType() == ReplyMarkup::Type::InlineKeyboardMarkup)
|
|
||||||
{
|
|
||||||
writer.StartArray();
|
|
||||||
auto rows = replyMarkup_->getInlineKeyboardMarkup().getRows();
|
|
||||||
for (auto& row : rows)
|
|
||||||
{
|
|
||||||
writer.StartArray();
|
|
||||||
for (auto& button : row)
|
|
||||||
{
|
|
||||||
writeInlineKeyboardButton(writer, button);
|
|
||||||
}
|
|
||||||
writer.EndArray();
|
|
||||||
}
|
|
||||||
writer.EndArray();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
writer.String(chatId_.getUsername().c_str());
|
|
||||||
writer.EndObject();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
writer.EndObject();
|
writer.EndObject();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#include <telebotxx/SendPhotoRequest.hpp>
|
#include <telebotxx/SendPhotoRequest.hpp>
|
||||||
#include <telebotxx/Logging.hpp>
|
#include <telebotxx/Logging.hpp>
|
||||||
#include "JsonObjects.hpp"
|
#include "JsonObjects.hpp"
|
||||||
|
#include "ReplyMarkup.hpp"
|
||||||
|
|
||||||
#include <cpr/cpr.h>
|
#include <cpr/cpr.h>
|
||||||
#include <rapidjson/document.h>
|
#include <rapidjson/document.h>
|
||||||
|
|
@ -34,6 +35,11 @@ public:
|
||||||
replyToMessageId_ = replyToMessageId;
|
replyToMessageId_ = replyToMessageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setReplyMarkup(const ReplyMarkup& replyMarkup)
|
||||||
|
{
|
||||||
|
replyMarkup_ = replyMarkup;
|
||||||
|
}
|
||||||
|
|
||||||
Message execute()
|
Message execute()
|
||||||
{
|
{
|
||||||
cpr::Multipart multipart{};
|
cpr::Multipart multipart{};
|
||||||
|
|
@ -71,6 +77,17 @@ public:
|
||||||
if (replyToMessageId_)
|
if (replyToMessageId_)
|
||||||
multipart.parts.push_back({"reply_to_message_id", replyToMessageId_->value()});
|
multipart.parts.push_back({"reply_to_message_id", replyToMessageId_->value()});
|
||||||
|
|
||||||
|
// Add reply_markup
|
||||||
|
if (replyMarkup_)
|
||||||
|
{
|
||||||
|
// Construct JSON body
|
||||||
|
using namespace rapidjson;
|
||||||
|
StringBuffer s;
|
||||||
|
Writer<StringBuffer> writer(s);
|
||||||
|
writeReplyMarkup(writer, *replyMarkup_);
|
||||||
|
multipart.parts.push_back({"reply_markup", s.GetString()});
|
||||||
|
}
|
||||||
|
|
||||||
auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendPhoto"}, multipart);
|
auto r = cpr::Post(cpr::Url{telegramMainUrl_ + "/sendPhoto"}, multipart);
|
||||||
auto& response = r.text;
|
auto& response = r.text;
|
||||||
|
|
||||||
|
|
@ -92,6 +109,7 @@ private:
|
||||||
boost::optional<Caption> caption_;
|
boost::optional<Caption> caption_;
|
||||||
boost::optional<DisableNotification> disableNotification_;
|
boost::optional<DisableNotification> disableNotification_;
|
||||||
boost::optional<ReplyTo> replyToMessageId_;
|
boost::optional<ReplyTo> replyToMessageId_;
|
||||||
|
boost::optional<ReplyMarkup> replyMarkup_;
|
||||||
};
|
};
|
||||||
|
|
||||||
SendPhotoRequest::SendPhotoRequest(const std::string& telegramMainUrl, const ChatId& chat, const Photo& photo)
|
SendPhotoRequest::SendPhotoRequest(const std::string& telegramMainUrl, const ChatId& chat, const Photo& photo)
|
||||||
|
|
@ -118,6 +136,11 @@ void SendPhotoRequest::setReplyToMessageId(const ReplyTo& replyToMessageId)
|
||||||
impl_->setReplyToMessageId(replyToMessageId);
|
impl_->setReplyToMessageId(replyToMessageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SendPhotoRequest::setReplyMarkup(const ReplyMarkup& replyMarkup)
|
||||||
|
{
|
||||||
|
impl_->setReplyMarkup(replyMarkup);
|
||||||
|
}
|
||||||
|
|
||||||
void SendPhotoRequest::setOption(const Caption& caption)
|
void SendPhotoRequest::setOption(const Caption& caption)
|
||||||
{
|
{
|
||||||
setCaption(caption);
|
setCaption(caption);
|
||||||
|
|
@ -133,6 +156,11 @@ void SendPhotoRequest::setOption(const ReplyTo& replyToMessageId)
|
||||||
setReplyToMessageId(replyToMessageId);
|
setReplyToMessageId(replyToMessageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SendPhotoRequest::setOption(const ReplyMarkup& replyMarkup)
|
||||||
|
{
|
||||||
|
setReplyMarkup(replyMarkup);
|
||||||
|
}
|
||||||
|
|
||||||
Message SendPhotoRequest::execute()
|
Message SendPhotoRequest::execute()
|
||||||
{
|
{
|
||||||
return impl_->execute();
|
return impl_->execute();
|
||||||
|
|
|
||||||
|
|
@ -214,6 +214,21 @@ BOOST_AUTO_TEST_SUITE(TestSend)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(SendPhotoWithInlineKeyboard)
|
||||||
|
{
|
||||||
|
PRINT_TESTNAME;
|
||||||
|
BOOST_REQUIRE(bot);
|
||||||
|
InlineKeyboardMarkup markup;
|
||||||
|
markup.addRow(InlineKeyboardButtonRow{
|
||||||
|
InlineKeyboardButton("Google", Url{"http://google.com/"})
|
||||||
|
});
|
||||||
|
BOOST_REQUIRE_NO_THROW(bot->sendPhoto(chat,
|
||||||
|
Photo{File{photoFile}},
|
||||||
|
Caption{"Photo with inline keyboard"},
|
||||||
|
ReplyMarkup{markup}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE_END()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
||||||
BOOST_AUTO_TEST_SUITE(TestParse)
|
BOOST_AUTO_TEST_SUITE(TestParse)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue