summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/tgbot/EventBroadcaster.h19
-rw-r--r--include/tgbot/EventHandler.h2
-rw-r--r--include/tgbot/TgTypeParser.h9
-rw-r--r--include/tgbot/types/CallbackQuery.h52
-rw-r--r--include/tgbot/types/InlineKeyboardButton.h43
-rw-r--r--include/tgbot/types/InlineKeyboardMarkup.h32
-rw-r--r--include/tgbot/types/Update.h6
-rw-r--r--src/TgTypeParser.cpp89
8 files changed, 250 insertions, 2 deletions
diff --git a/include/tgbot/EventBroadcaster.h b/include/tgbot/EventBroadcaster.h
index d97dbbb..12daef3 100644
--- a/include/tgbot/EventBroadcaster.h
+++ b/include/tgbot/EventBroadcaster.h
@@ -31,6 +31,7 @@
#include "tgbot/types/Message.h"
#include "tgbot/types/InlineQuery.h"
#include "tgbot/types/ChosenInlineResult.h"
+#include "tgbot/types/CallbackQuery.h"
namespace TgBot {
@@ -48,6 +49,7 @@ 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;
/**
* Registers listener which receives all messages which the bot can ever receive.
@@ -82,14 +84,26 @@ public:
_onNonCommandMessageListeners.push_back(listener);
}
+ /**
+ * Registers listener which receives all the inline query.
+ * @param listener Listener.
+ */
inline void onInlineQuery(const InlineQueryListener& listener) {
_onInlineQueryListeners.push_back(listener);
}
+ /**
+ * Registers listener which receives all the chosen inline result.
+ * @param listener Listener.
+ */
inline void onChosenInlineResult(const ChosenInlineResultListener& listener){
_onChosenInlineResultListeners.push_back(listener);
}
+ inline void onCallbackQuery(const CallbackQueryListener& listener){
+ _onCallbackQueryListeners.push_back(listener);
+ }
+
private:
template<typename ListenerType, typename ObjectType>
inline void broadcast(const std::vector<ListenerType>& listeners, const ObjectType& object) const {
@@ -130,12 +144,17 @@ private:
broadcast<ChosenInlineResultListener, ChosenInlineResult::Ptr>(_onChosenInlineResultListeners, result);
}
+ inline void broadcastCallbackQuery(const CallbackQuery::Ptr& result) const {
+ broadcast<CallbackQueryListener, CallbackQuery::Ptr>(_onCallbackQueryListeners, result);
+ }
+
std::vector<MessageListener> _onAnyMessageListeners;
std::map<std::string, MessageListener> _onCommandListeners;
std::vector<MessageListener> _onUnknownCommandListeners;
std::vector<MessageListener> _onNonCommandMessageListeners;
std::vector<InlineQueryListener> _onInlineQueryListeners;
std::vector<ChosenInlineResultListener> _onChosenInlineResultListeners;
+ std::vector<CallbackQueryListener> _onCallbackQueryListeners;
};
}
diff --git a/include/tgbot/EventHandler.h b/include/tgbot/EventHandler.h
index 64f74cd..63ecde3 100644
--- a/include/tgbot/EventHandler.h
+++ b/include/tgbot/EventHandler.h
@@ -67,6 +67,8 @@ public:
_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);
}
diff --git a/include/tgbot/TgTypeParser.h b/include/tgbot/TgTypeParser.h
index f52d106..17e9553 100644
--- a/include/tgbot/TgTypeParser.h
+++ b/include/tgbot/TgTypeParser.h
@@ -52,6 +52,9 @@
#include "tgbot/types/InlineQueryResultMpeg4Gif.h"
#include "tgbot/types/InlineQueryResultVideo.h"
#include "tgbot/types/ChosenInlineResult.h"
+#include "tgbot/types/CallbackQuery.h"
+#include "tgbot/types/InlineKeyboardMarkup.h"
+#include "tgbot/types/InlineKeyboardButton.h"
namespace TgBot {
@@ -114,6 +117,12 @@ public:
std::string parseInlineQueryResultVideo(const InlineQueryResultVideo::Ptr& object) const;
ChosenInlineResult::Ptr parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const;
std::string parseChosenInlineResult(const ChosenInlineResult::Ptr& object) const;
+ CallbackQuery::Ptr parseJsonAndGetCallbackQuery(const boost::property_tree::ptree& data) const;
+ std::string parseCallbackQuery(const CallbackQuery::Ptr& object) const;
+ InlineKeyboardMarkup::Ptr parseJsonAndGetInlineKeyboardMarkup(const boost::property_tree::ptree& data) const;
+ std::string parseInlineKeyboardMarkup(const InlineKeyboardMarkup::Ptr& object) const;
+ InlineKeyboardButton::Ptr parseJsonAndGetInlineKeyboardButton(const boost::property_tree::ptree& data) const;
+ std::string parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const;
inline boost::property_tree::ptree parseJson(const std::string& json) const {
boost::property_tree::ptree tree;
diff --git a/include/tgbot/types/CallbackQuery.h b/include/tgbot/types/CallbackQuery.h
new file mode 100644
index 0000000..5f6871d
--- /dev/null
+++ b/include/tgbot/types/CallbackQuery.h
@@ -0,0 +1,52 @@
+//
+// Created by Andrea Giove on 17/04/16.
+//
+
+#ifndef TGBOT_CALLBACKQUERY_H
+#define TGBOT_CALLBACKQUERY_H
+
+#include <memory>
+#include <string>
+
+#include "tgbot/types/User.h"
+#include "tgbot/types/Message.h"
+
+namespace TgBot {
+
+/**
+ * This object represents an incoming callback query from a callback button in an inline keyboard.
+ * @ingroup types
+ */
+class CallbackQuery {
+public:
+ typedef std::shared_ptr<CallbackQuery> Ptr;
+
+ /**
+ * Unique identifier for this query.
+ */
+ std::string id;
+
+ /**
+ * Sender.
+ */
+ User::Ptr from;
+
+ /**
+ * Optional. Message with the callback button that originated the query. Note that message content and message date will not be available if the message is too old.
+ */
+ Message::Ptr message;
+
+ /**
+ * Optional. Identifier of the message sent via the bot in inline mode, that originated the query.
+ */
+ std::string inlineMessageId;
+
+ /**
+ * Data associated with the callback button. Be aware that a bad client can send arbitrary data in this field.
+ */
+ std::string data;
+
+};
+}
+
+#endif //TGBOT_CALLBACKQUERY_H
diff --git a/include/tgbot/types/InlineKeyboardButton.h b/include/tgbot/types/InlineKeyboardButton.h
new file mode 100644
index 0000000..1a71341
--- /dev/null
+++ b/include/tgbot/types/InlineKeyboardButton.h
@@ -0,0 +1,43 @@
+//
+// Created by Andrea Giove on 17/04/16.
+//
+
+#ifndef TGBOT_INLINEKEYBOARDBUTTON_H
+#define TGBOT_INLINEKEYBOARDBUTTON_H
+
+#include <string>
+#include <memory>
+
+namespace TgBot {
+
+/**
+ * This object represents one button of an inline keyboard. You must use exactly one of the optional fields.
+ * @ingroup types
+ */
+class InlineKeyboardButton {
+public:
+ typedef std::shared_ptr<InlineKeyboardButton> Ptr;
+
+ /**
+ * Label text on the button
+ */
+ std::string text;
+
+ /**
+ * Optional. HTTP url to be opened when button is pressed.
+ */
+ std::string url;
+
+ /**
+ * Optional. Data to be sent in a callback query to the bot when button is pressed.
+ */
+ std::string callbackData;
+
+ /**
+ * Optional. If set, pressing the button will prompt the user to select one of their chats, open that chat and insert the bot‘s username and the specified inline query in the input field. Can be empty, in which case just the bot’s username will be inserted.
+ */
+ std::string switchInlineQuery;
+};
+}
+
+#endif //TGBOT_INLINEKEYBOARDBUTTON_H
diff --git a/include/tgbot/types/InlineKeyboardMarkup.h b/include/tgbot/types/InlineKeyboardMarkup.h
new file mode 100644
index 0000000..9097fa5
--- /dev/null
+++ b/include/tgbot/types/InlineKeyboardMarkup.h
@@ -0,0 +1,32 @@
+//
+// Created by Andrea Giove on 17/04/16.
+//
+
+#ifndef TGBOT_INLINEKEYBOARDMARKUP_H
+#define TGBOT_INLINEKEYBOARDMARKUP_H
+
+#include <vector>
+#include <memory>
+
+#include "tgbot/types/GenericReply.h"
+#include "tgbot/types/InlineKeyboardButton.h"
+
+namespace TgBot {
+
+/**
+ * This object represents an inline keyboard that appears right next to the message it belongs to.
+ * @ingroup types
+ */
+class InlineKeyboardMarkup : public GenericReply {
+public:
+ typedef std::shared_ptr<InlineKeyboardMarkup> Ptr;
+
+ /**
+ * Array of button rows, each represented by an Array of InlineKeyboardButton objects.
+ */
+ std::vector<std::vector<InlineKeyboardButton::Ptr>> inlineKeyboard;
+
+};
+}
+
+#endif //TGBOT_INLINEKEYBOARDMARKUP_H
diff --git a/include/tgbot/types/Update.h b/include/tgbot/types/Update.h
index 9ae8d73..7a3c106 100644
--- a/include/tgbot/types/Update.h
+++ b/include/tgbot/types/Update.h
@@ -28,6 +28,7 @@
#include "tgbot/types/Message.h"
#include "tgbot/types/InlineQuery.h"
#include "tgbot/types/ChosenInlineResult.h"
+#include "tgbot/types/CallbackQuery.h"
namespace TgBot {
@@ -59,6 +60,11 @@ public:
* Optional. The result of an inline query that was chosen by a user and sent to their chat partner.
*/
ChosenInlineResult::Ptr chosenInlineResult;
+
+ /**
+ * Optional. New incoming callback query.
+ */
+ CallbackQuery::Ptr callbackQuery;
};
}
diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp
index 6672c04..f58bca5 100644
--- a/src/TgTypeParser.cpp
+++ b/src/TgTypeParser.cpp
@@ -350,6 +350,7 @@ Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const ptree& data) const {
result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message");
result->inlineQuery = tryParseJson<InlineQuery>(&TgTypeParser::parseJsonAndGetInlineQuery, data, "inline_query");
result->chosenInlineResult = tryParseJson<ChosenInlineResult>(&TgTypeParser::parseJsonAndGetChosenInlineResult, data, "chosen_inline_result");
+ result->callbackQuery = tryParseJson<CallbackQuery>(&TgTypeParser::parseJsonAndGetCallbackQuery, data, "callback_query");
return result;
}
@@ -363,6 +364,7 @@ string TgTypeParser::parseUpdate(const Update::Ptr& object) const {
appendToJson(result, "message", parseMessage(object->message));
appendToJson(result, "inline_query", parseInlineQuery(object->inlineQuery));
appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult));
+ appendToJson(result, "callback_query", parseCallbackQuery(object->callbackQuery));
result.erase(result.length() - 1);
result += '}';
return result;
@@ -473,8 +475,10 @@ GenericReply::Ptr TgTypeParser::parseJsonAndGetGenericReply(const boost::propert
return static_pointer_cast<GenericReply>(parseJsonAndGetForceReply(data));
} else if (data.find("hide_keyboard") != data.not_found()) {
return static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardHide(data));
- } else {
+ } else if (data.find("keyboard") != data.not_found()) {
return static_pointer_cast<GenericReply>(parseJsonAndGetReplyKeyboardMarkup(data));
+ } else if (data.find("inline_keyboard") != data.not_found()) {
+ return static_pointer_cast<GenericReply>(parseJsonAndGetInlineKeyboardMarkup(data));
}
}
@@ -486,8 +490,10 @@ std::string TgTypeParser::parseGenericReply(const GenericReply::Ptr& object) con
return parseForceReply(static_pointer_cast<ForceReply>(object));
} else if (dynamic_pointer_cast<ReplyKeyboardHide>(object) != nullptr) {
return parseReplyKeyboardHide(static_pointer_cast<ReplyKeyboardHide>(object));
- } else {
+ } else if (dynamic_pointer_cast<ReplyKeyboardMarkup>(object) != nullptr){
return parseReplyKeyboardMarkup(static_pointer_cast<ReplyKeyboardMarkup>(object));
+ } else if (dynamic_pointer_cast<InlineKeyboardMarkup>(object) != nullptr){
+ return parseInlineKeyboardMarkup(static_pointer_cast<InlineKeyboardMarkup>(object));
}
}
@@ -731,6 +737,85 @@ std::string TgTypeParser::parseChosenInlineResult(const ChosenInlineResult::Ptr&
return result;
}
+CallbackQuery::Ptr TgTypeParser::parseJsonAndGetCallbackQuery(const boost::property_tree::ptree& data) const {
+ CallbackQuery::Ptr result(new CallbackQuery);
+ result->id = data.get<string>("id");
+ result->from = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "from");
+ result->message = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "message");
+ result->inlineMessageId = data.get<string>("inline_message_id", "");
+ result->data = data.get<string>("data", "");
+ return result;
+}
+
+std::string TgTypeParser::parseCallbackQuery(const CallbackQuery::Ptr& object) const {
+ if (!object){
+ return "";
+ }
+
+ string result;
+ result += '{';
+ appendToJson(result, "id", object->id);
+ appendToJson(result, "from", parseUser(object->from));
+ appendToJson(result, "message", parseMessage(object->message));
+ appendToJson(result, "inline_message_id", object->inlineMessageId);
+ appendToJson(result, "data", object->data);
+ result.erase(result.length() - 1);
+ result += '}';
+ return result;
+}
+
+InlineKeyboardMarkup::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardMarkup(const boost::property_tree::ptree& data) const {
+ InlineKeyboardMarkup::Ptr result(new InlineKeyboardMarkup);
+ for (const boost::property_tree::ptree::value_type& item : data.find("inline_keyboard")->second){
+ result->inlineKeyboard.push_back(parseJsonAndGetArray<InlineKeyboardButton>(&TgTypeParser::parseJsonAndGetInlineKeyboardButton, item.second));
+ }
+ return result;
+}
+
+std::string TgTypeParser::parseInlineKeyboardMarkup(const InlineKeyboardMarkup::Ptr& object) const {
+ if (!object){
+ return "";
+ }
+ string result;
+ result += '{';
+ result += "\"inline_keyboard\":[";
+ for (vector<InlineKeyboardButton::Ptr>& item : object->inlineKeyboard){
+ result += '[';
+ for (InlineKeyboardButton::Ptr& innerItem : item){
+ result += parseInlineKeyboardButton(innerItem);
+ result += ',';
+ }
+ result.erase(result.length() - 1);
+ result += "],";
+ }
+ result.erase(result.length() - 1);
+ result += "]}";
+ return result;
+}
+
+InlineKeyboardButton::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardButton(const boost::property_tree::ptree& data) const {
+ InlineKeyboardButton::Ptr result(new InlineKeyboardButton);
+ result->text = data.get<string>("text");
+ result->url = data.get<string>("url", "");
+ result->callbackData = data.get<string>("callback_data", "");
+ result->switchInlineQuery = data.get<string>("switch_inline_query", "");
+ return result;
+}
+std::string TgTypeParser::parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const {
+ if (!object){
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "text", object->text);
+ appendToJson(result, "url", object->url);
+ appendToJson(result, "callback_data", object->callbackData);
+ appendToJson(result, "switch_inline_query", object->switchInlineQuery);
+ result.erase(result.length() - 1);
+ result += '}';
+ return result;
+}
+
void TgTypeParser::appendToJson(string& json, const string& varName, const string& value) const {
if (value.empty()) {
return;