mirror of
https://github.com/UltraCoderRU/telebotxx.git
synced 2026-01-28 12:15:13 +00:00
InlineKeyboardMarkup test support.
This commit is contained in:
parent
014411a8a3
commit
299dced3dc
5 changed files with 296 additions and 1 deletions
|
|
@ -109,6 +109,79 @@ private:
|
||||||
boost::variant<int, Buffer, File, Url> value_;
|
boost::variant<int, Buffer, File, Url> value_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
TELEBOTXX_DECLARE_STRING_PARAM_CLASS(CallbackData)
|
||||||
|
|
||||||
|
TELEBOTXX_DECLARE_STRING_PARAM_CLASS(SwitchInlineQuery)
|
||||||
|
|
||||||
|
TELEBOTXX_DECLARE_STRING_PARAM_CLASS(SwitchInlineQueryCurrentChat)
|
||||||
|
|
||||||
|
class InlineKeyboardButton
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit InlineKeyboardButton(const std::string& text, const Url& url);
|
||||||
|
explicit InlineKeyboardButton(const std::string& text, const CallbackData&);
|
||||||
|
explicit InlineKeyboardButton(const std::string& text, const SwitchInlineQuery&);
|
||||||
|
explicit InlineKeyboardButton(const std::string& text, const SwitchInlineQueryCurrentChat&);
|
||||||
|
InlineKeyboardButton(const InlineKeyboardButton&);
|
||||||
|
InlineKeyboardButton(InlineKeyboardButton&&);
|
||||||
|
~InlineKeyboardButton();
|
||||||
|
|
||||||
|
enum class ActionType { Url, CallbackData, SwitchInlineQuery, SwitchInlineQueryCurrentChat };
|
||||||
|
ActionType getActionType() const;
|
||||||
|
|
||||||
|
const std::string& getText() const;
|
||||||
|
const Url& getUrl() const;
|
||||||
|
const CallbackData& getCallbackData() const;
|
||||||
|
const SwitchInlineQuery& getSwitchInlineQuery() const;
|
||||||
|
const SwitchInlineQueryCurrentChat& getSwitchInlineQueryCurrentChat() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
ActionType actionType_;
|
||||||
|
std::string text_;
|
||||||
|
boost::variant<Url, CallbackData, SwitchInlineQuery, SwitchInlineQueryCurrentChat> value_;
|
||||||
|
// \todo CallbackGame
|
||||||
|
};
|
||||||
|
|
||||||
|
using InlineKeyboardButtonRow = std::vector<InlineKeyboardButton>;
|
||||||
|
|
||||||
|
class InlineKeyboardMarkup
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
void addRow(const InlineKeyboardButtonRow& row);
|
||||||
|
|
||||||
|
const std::vector<InlineKeyboardButtonRow>& getRows() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<InlineKeyboardButtonRow> rows_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReplyKeyboardMarkup
|
||||||
|
{
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
class ReplyMarkup
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
explicit ReplyMarkup(const InlineKeyboardMarkup&);
|
||||||
|
explicit ReplyMarkup(const ReplyKeyboardMarkup&);
|
||||||
|
ReplyMarkup(const ReplyMarkup&);
|
||||||
|
ReplyMarkup(ReplyMarkup&&);
|
||||||
|
~ReplyMarkup();
|
||||||
|
|
||||||
|
enum class Type { InlineKeyboardMarkup, ReplyKeyboardMarkup, ReplyKeyboardRemove, ForceReply };
|
||||||
|
Type getType() const;
|
||||||
|
|
||||||
|
const InlineKeyboardMarkup& getInlineKeyboardMarkup() const;
|
||||||
|
|
||||||
|
void swap(ReplyMarkup&);
|
||||||
|
const ReplyMarkup& operator=(ReplyMarkup);
|
||||||
|
|
||||||
|
private:
|
||||||
|
Type type_;
|
||||||
|
boost::variant<InlineKeyboardMarkup, ReplyKeyboardMarkup> value_;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif // TELEBOTXX_REQUEST_OPTIONS_HPP
|
#endif // TELEBOTXX_REQUEST_OPTIONS_HPP
|
||||||
|
|
|
||||||
|
|
@ -19,11 +19,13 @@ public:
|
||||||
void setDisableWebPagePreview(const DisableWebPagePreview& disableWebPagePreview);
|
void setDisableWebPagePreview(const DisableWebPagePreview& disableWebPagePreview);
|
||||||
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(ParseMode mode);
|
void setOption(ParseMode mode);
|
||||||
void setOption(const DisableWebPagePreview& disableWebPagePreview);
|
void setOption(const DisableWebPagePreview& disableWebPagePreview);
|
||||||
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();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,9 @@ ChatId::ChatId(const std::string& str)
|
||||||
}
|
}
|
||||||
|
|
||||||
ChatId::ChatId(const ChatId& other) = default;
|
ChatId::ChatId(const ChatId& other) = default;
|
||||||
|
|
||||||
ChatId::ChatId(ChatId&& other) = default;
|
ChatId::ChatId(ChatId&& other) = default;
|
||||||
|
|
||||||
ChatId::~ChatId() = default;
|
ChatId::~ChatId() = default;
|
||||||
|
|
||||||
ChatId::Type ChatId::getType() const
|
ChatId::Type ChatId::getType() const
|
||||||
|
|
@ -125,7 +127,9 @@ Photo::Photo(const Url& url)
|
||||||
}
|
}
|
||||||
|
|
||||||
Photo::Photo(const Photo& other) = default;
|
Photo::Photo(const Photo& other) = default;
|
||||||
|
|
||||||
Photo::Photo(Photo&& other) = default;
|
Photo::Photo(Photo&& other) = default;
|
||||||
|
|
||||||
Photo::~Photo() = default;
|
Photo::~Photo() = default;
|
||||||
|
|
||||||
Photo::Type Photo::getType() const
|
Photo::Type Photo::getType() const
|
||||||
|
|
@ -153,4 +157,126 @@ const Url& Photo::getUrl() const
|
||||||
return boost::get<Url>(value_);
|
return boost::get<Url>(value_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
TELEBOTXX_DEFINE_STRING_PARAM_CLASS(CallbackData)
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
TELEBOTXX_DEFINE_STRING_PARAM_CLASS(SwitchInlineQuery)
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
TELEBOTXX_DEFINE_STRING_PARAM_CLASS(SwitchInlineQueryCurrentChat)
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
InlineKeyboardButton::InlineKeyboardButton(const std::string& text, const Url& url)
|
||||||
|
: actionType_(ActionType::Url), text_(text), value_(url)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
InlineKeyboardButton::InlineKeyboardButton(const std::string& text, const CallbackData& callbackData)
|
||||||
|
: actionType_(ActionType::CallbackData), text_(text), value_(callbackData)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
InlineKeyboardButton::InlineKeyboardButton(const std::string& text, const SwitchInlineQuery& switchInlineQuery)
|
||||||
|
: actionType_(ActionType::SwitchInlineQuery), text_(text), value_(switchInlineQuery)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
InlineKeyboardButton::InlineKeyboardButton(const std::string& text,
|
||||||
|
const SwitchInlineQueryCurrentChat& switchInlineQueryCurrentChat)
|
||||||
|
: actionType_(ActionType::SwitchInlineQueryCurrentChat), text_(text), value_(switchInlineQueryCurrentChat)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
InlineKeyboardButton::InlineKeyboardButton(const InlineKeyboardButton& other) = default;
|
||||||
|
|
||||||
|
InlineKeyboardButton::InlineKeyboardButton(InlineKeyboardButton&& other) = default;
|
||||||
|
|
||||||
|
InlineKeyboardButton::~InlineKeyboardButton() = default;
|
||||||
|
|
||||||
|
InlineKeyboardButton::ActionType InlineKeyboardButton::getActionType() const
|
||||||
|
{
|
||||||
|
return actionType_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::string& InlineKeyboardButton::getText() const
|
||||||
|
{
|
||||||
|
return text_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Url& InlineKeyboardButton::getUrl() const
|
||||||
|
{
|
||||||
|
return boost::get<Url>(value_);;
|
||||||
|
}
|
||||||
|
|
||||||
|
const CallbackData& InlineKeyboardButton::getCallbackData() const
|
||||||
|
{
|
||||||
|
return boost::get<CallbackData>(value_);
|
||||||
|
}
|
||||||
|
|
||||||
|
const SwitchInlineQuery& InlineKeyboardButton::getSwitchInlineQuery() const
|
||||||
|
{
|
||||||
|
return boost::get<SwitchInlineQuery>(value_);
|
||||||
|
}
|
||||||
|
|
||||||
|
const SwitchInlineQueryCurrentChat& InlineKeyboardButton::getSwitchInlineQueryCurrentChat() const
|
||||||
|
{
|
||||||
|
return boost::get<SwitchInlineQueryCurrentChat>(value_);
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
void InlineKeyboardMarkup::addRow(const InlineKeyboardButtonRow& row)
|
||||||
|
{
|
||||||
|
rows_.push_back(row);
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<InlineKeyboardButtonRow>& InlineKeyboardMarkup::getRows() const
|
||||||
|
{
|
||||||
|
return rows_;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
ReplyMarkup::ReplyMarkup(const InlineKeyboardMarkup& keyboard)
|
||||||
|
: type_(Type::InlineKeyboardMarkup), value_(keyboard)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ReplyMarkup::ReplyMarkup(const ReplyKeyboardMarkup& keyboard)
|
||||||
|
: type_(Type::ReplyKeyboardMarkup), value_(keyboard)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
ReplyMarkup::ReplyMarkup(const ReplyMarkup&) = default;
|
||||||
|
ReplyMarkup::ReplyMarkup(ReplyMarkup&&) = default;
|
||||||
|
ReplyMarkup::~ReplyMarkup() = default;
|
||||||
|
|
||||||
|
ReplyMarkup::Type ReplyMarkup::getType() const
|
||||||
|
{
|
||||||
|
return type_;
|
||||||
|
}
|
||||||
|
|
||||||
|
const InlineKeyboardMarkup& ReplyMarkup::getInlineKeyboardMarkup() const
|
||||||
|
{
|
||||||
|
return boost::get<InlineKeyboardMarkup>(value_);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ReplyMarkup::swap(ReplyMarkup& other)
|
||||||
|
{
|
||||||
|
using std::swap;
|
||||||
|
swap(type_, other.type_);
|
||||||
|
swap(value_, other.value_);
|
||||||
|
}
|
||||||
|
|
||||||
|
const ReplyMarkup& ReplyMarkup::operator=(ReplyMarkup other)
|
||||||
|
{
|
||||||
|
swap(other);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,44 @@
|
||||||
|
|
||||||
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:
|
||||||
|
|
@ -39,6 +77,11 @@ public:
|
||||||
replyToMessageId_ = replyToMessageId;
|
replyToMessageId_ = replyToMessageId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setReplyMarkup(const ReplyMarkup& replyMarkup)
|
||||||
|
{
|
||||||
|
replyMarkup_ = replyMarkup;
|
||||||
|
}
|
||||||
|
|
||||||
Message execute()
|
Message execute()
|
||||||
{
|
{
|
||||||
// Construct JSON body
|
// Construct JSON body
|
||||||
|
|
@ -87,7 +130,31 @@ public:
|
||||||
writer.Int(replyToMessageId_->value());
|
writer.Int(replyToMessageId_->value());
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \todo: Add reply_markup
|
// Add reply_markup
|
||||||
|
if (replyMarkup_)
|
||||||
|
{
|
||||||
|
writer.String("reply_markup");
|
||||||
|
writer.StartObject();
|
||||||
|
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();
|
||||||
|
|
||||||
|
|
@ -120,6 +187,7 @@ private:
|
||||||
boost::optional<DisableWebPagePreview> disableWebPagePreview_;
|
boost::optional<DisableWebPagePreview> disableWebPagePreview_;
|
||||||
boost::optional<DisableNotification> disableNotification_;
|
boost::optional<DisableNotification> disableNotification_;
|
||||||
boost::optional<ReplyTo> replyToMessageId_;
|
boost::optional<ReplyTo> replyToMessageId_;
|
||||||
|
boost::optional<ReplyMarkup> replyMarkup_;
|
||||||
};
|
};
|
||||||
|
|
||||||
SendMessageRequest::SendMessageRequest(const std::string& telegramMainUrl, const ChatId& chat, const Text& text)
|
SendMessageRequest::SendMessageRequest(const std::string& telegramMainUrl, const ChatId& chat, const Text& text)
|
||||||
|
|
@ -151,6 +219,11 @@ void SendMessageRequest::setReplyToMessageId(const ReplyTo& replyToMessageId)
|
||||||
impl_->setReplyToMessageId(replyToMessageId);
|
impl_->setReplyToMessageId(replyToMessageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SendMessageRequest::setReplyMarkup(const ReplyMarkup& replyMarkup)
|
||||||
|
{
|
||||||
|
impl_->setReplyMarkup(replyMarkup);
|
||||||
|
}
|
||||||
|
|
||||||
void SendMessageRequest::setOption(ParseMode mode)
|
void SendMessageRequest::setOption(ParseMode mode)
|
||||||
{
|
{
|
||||||
setParseMode(mode);
|
setParseMode(mode);
|
||||||
|
|
@ -171,6 +244,11 @@ void SendMessageRequest::setOption(const ReplyTo& replyToMessageId)
|
||||||
setReplyToMessageId(replyToMessageId);
|
setReplyToMessageId(replyToMessageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SendMessageRequest::setOption(const ReplyMarkup& replyMarkup)
|
||||||
|
{
|
||||||
|
setReplyMarkup(replyMarkup);
|
||||||
|
}
|
||||||
|
|
||||||
Message SendMessageRequest::execute()
|
Message SendMessageRequest::execute()
|
||||||
{
|
{
|
||||||
return impl_->execute();
|
return impl_->execute();
|
||||||
|
|
|
||||||
|
|
@ -143,6 +143,22 @@ BOOST_AUTO_TEST_SUITE(TestBotApi)
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BOOST_AUTO_TEST_CASE(SendMessageWithInlineKeyboard)
|
||||||
|
{
|
||||||
|
PRINT_TESTNAME;
|
||||||
|
BOOST_REQUIRE(bot);
|
||||||
|
InlineKeyboardMarkup markup;
|
||||||
|
markup.addRow(InlineKeyboardButtonRow{
|
||||||
|
InlineKeyboardButton("Google", Url{"http://google.com/"}),
|
||||||
|
InlineKeyboardButton("Google", Url{"http://google.com/"})
|
||||||
|
});
|
||||||
|
markup.addRow(InlineKeyboardButtonRow{InlineKeyboardButton("Google", CallbackData{"HELLO"})});
|
||||||
|
BOOST_REQUIRE_NO_THROW(bot->sendMessage(ChatId{chat},
|
||||||
|
Text{"Message with inline keyboard"},
|
||||||
|
ReplyMarkup{markup}
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
BOOST_AUTO_TEST_CASE(SendPhotoFile)
|
BOOST_AUTO_TEST_CASE(SendPhotoFile)
|
||||||
{
|
{
|
||||||
PRINT_TESTNAME;
|
PRINT_TESTNAME;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue