From 67a543eb596b68a166d7f790ea27d5d4e70e10b7 Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Sat, 26 Mar 2016 19:16:34 +0100 Subject: Updated Message class --- src/TgTypeParser.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index db44eff..53a00da 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -126,6 +126,10 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const { result->deleteChatPhoto = data.get("delete_chat_photo", false); result->groupChatCreated = data.get("group_chat_created", false); result->caption = data.get("caption", false); + result->supergroupChatCreated = data.get("supergroup_chat_created", false); + result->channelChatCreated = data.get("channel_chat_created", false); + result->migrateToChatId = data.get("migrate_to_chat_id", 0); + result->migrateFromChatId = data.get("migrate_from_chat_id", 0); return result; } @@ -157,6 +161,10 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const { appendToJson(result, "delete_chat_photo", object->deleteChatPhoto); appendToJson(result, "group_chat_created", object->groupChatCreated); appendToJson(result, "caption", object->caption); + appendToJson(result, "supergroup_chat_created", object->supergroupChatCreated); + appendToJson(result, "channel_chat_created", object->channelChatCreated); + appendToJson(result, "migrate_to_chat_id", object->migrateToChatId); + appendToJson(result, "migrate_from_chat_id", object->migrateFromChatId); result.erase(result.length() - 1); result += '}'; return result; -- cgit v1.2.3 From f889903cd7132ce39e81da15d2f335ac511b2e1f Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Sun, 27 Mar 2016 11:16:28 +0200 Subject: Add new types and new event broadcaster for support the inline mode. --- src/TgTypeParser.cpp | 244 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 244 insertions(+) (limited to 'src') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 53a00da..eb7f769 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -348,6 +348,8 @@ Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const ptree& data) const { Update::Ptr result(new Update); result->updateId = data.get("update_id"); result->message = parseJsonAndGetMessage(data.find("message")->second); + result->inlineQuery = parseJsonAndGetInlineQuery(data.find("inline_query")->second); + result->chosenInlineResult = parseJsonAndGetChosenInlineResult(data.find("chosen_inline_result")->second); return result; } @@ -359,6 +361,8 @@ string TgTypeParser::parseUpdate(const Update::Ptr& object) const { result += '{'; appendToJson(result, "update_id", object->updateId); appendToJson(result, "message", parseMessage(object->message)); + appendToJson(result, "inline_query", parseInlineQuery(object->inlineQuery)); + appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult)) result.erase(result.length() - 1); result += '}'; return result; @@ -487,6 +491,246 @@ std::string TgTypeParser::parseGenericReply(const GenericReply::Ptr& object) con } } +InlineQuery::Ptr TgTypeParser::parseJsonAndGetInlineQuery(const boost::property_tree::ptree& data) const { + InlineQuery::Ptr result(new InlineQuery); + result->id = data.get("id"); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); + result->query = data.get("query"); + result->offset = data.get("offset"); + + return result; +} + +std::string TgTypeParser::parseInlineQuery(const InlineQuery::Ptr& object) const{ + if (!object) { + return ""; + } + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "from", parseUser(object->from)); + appendToJson(result, "query", object->query); + appendToJson(result, "offset", object->offset); + result.erase(result.length() - 1); + result += '}'; + return result; +} + +InlineQueryResult::Ptr TgTypeParser::parseJsonAndGetInlineQueryResult(const boost::property_tree::ptree& data) const { + string type = data.get("type"); + InlineQueryResult::Ptr result; + + if (type == InlineQueryResultArticle::TYPE) { + result = static_pointer_cast(parseJsonAndGetInlineQueryResultArticle(data)); + } else if (type == InlineQueryResultPhoto::TYPE) { + result = static_pointer_cast(parseJsonAndGetInlineQueryResultPhoto(data)); + } else if (type == InlineQueryResultGif::TYPE) { + result = static_pointer_cast(parseJsonAndGetInlineQueryResultGif(data)); + } else if (type == InlineQueryResultMpeg4Gif::TYPE) { + result = static_pointer_cast(parseJsonAndGetInlineQueryResultMpeg4Gif(data)); + } else if (type == InlineQueryResultVideo::TYPE) { + result = static_pointer_cast(parseJsonAndGetInlineQueryResultVideo(data)); + } else { + result = make_shared(); + } + + result->id = data.get("id"); + result->title = data.get("title", ""); + result->messageText = data.get("message_text", ""); + result->parseMode = data.get("parse_mode", ""); + result->disableWebPagePreview = data.get("disable_web_page_preview", false); + result->thumbUrl = data.get("thumb_url", ""); + + return result; +} + +std::string TgTypeParser::parseInlineQueryResult(const InlineQueryResult::Ptr& object) const { + if (!object){ + return ""; + } + + string result; + result += '{'; + appendToJson(result, "id", object->id); + appendToJson(result, "type", object->type); + appendToJson(result, "title", object->title); + appendToJson(result, "message_text", object->messageText); + appendToJson(result, "parse_mode", object->parseMode); + appendToJson(result, "disable_web_page_preview", object->disableWebPagePreview); + appendToJson(result, "thumb_url", object->thumbUrl); + + if (object->type == InlineQueryResultArticle::TYPE){ + result += parseInlineQueryResultArticle(static_pointer_cast(object)); + } else if (object->type == InlineQueryResultPhoto::TYPE){ + result += parseInlineQueryResultPhoto(static_pointer_cast(object)); + } else if (object->type == InlineQueryResultGif::TYPE){ + result += parseInlineQueryResultGif(static_pointer_cast(object)); + } else if (object->type == InlineQueryResultMpeg4Gif::TYPE){ + result += parseInlineQueryResultMpeg4Gif(static_pointer_cast(object)); + } else if (object->type == InlineQueryResultVideo::TYPE){ + result += parseInlineQueryResultVideo(static_pointer_cast(object)); + } + + result.erase(result.length() - 1); + result += '}'; + return result; +} + +InlineQueryResultArticle::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultArticle(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + InlineQueryResultArticle::Ptr result(new InlineQueryResultArticle); + result->url = data.get("url", ""); + result->hideUrl = data.get("hide_url", false); + result->description = data.get("description", ""); + result->thumbWidth = data.get("thumb_width", 0); + result->thumbHeight = data.get("thumb_height", 0); + return result; +} + +std::string TgTypeParser::parseInlineQueryResultArticle(const InlineQueryResultArticle::Ptr& object) const { + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "url", object->url); + appendToJson(result, "hide_url", object->hideUrl); + appendToJson(result, "description", object->description); + appendToJson(result, "thumb_width", object->thumbWidth); + appendToJson(result, "thumb_height", object->thumbHeight); + // The last comma will be erased by parseInlineQueryResult(). + return result; +} + +InlineQueryResultPhoto::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultPhoto(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + InlineQueryResultPhoto::Ptr result(new InlineQueryResultPhoto); + result->photoUrl = data.get("photo_url", ""); + result->photoWidth = data.get("photo_width", 0); + result->photoHeight = data.get("photo_height", 0); + result->description = data.get("description", ""); + result->caption = data.get("caption", ""); + return result; +} + +std::string TgTypeParser::parseInlineQueryResultPhoto(const InlineQueryResultPhoto::Ptr& object) const{ + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "photo_url", object->photoUrl); + appendToJson(result, "photo_width", object->photoWidth); + appendToJson(result, "photo_height", object->photoHeight); + appendToJson(result, "description", object->description); + appendToJson(result, "caption", object->caption); + // The last comma will be erased by parseInlineQueryResult(). + return result; +} + +InlineQueryResultGif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultGif(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + InlineQueryResultGif::Ptr result(new InlineQueryResultGif); + result->gifUrl = data.get("gif_url", ""); + result->gifWidth = data.get("gif_width", 0); + result->gifHeight = data.get("gif_height", 0); + result->caption = data.get("caption", ""); + return result; +} +std::string TgTypeParser::parseInlineQueryResultGif(const InlineQueryResultGif::Ptr& object) const { + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "gif_url", object->gifUrl); + appendToJson(result, "gif_width", object->gifWidth); + appendToJson(result, "gif_height", object->gifHeight); + appendToJson(result, "caption", object->caption); + // The last comma will be erased by parseInlineQueryResult(). + return result; +} + +InlineQueryResultMpeg4Gif::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultMpeg4Gif(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + InlineQueryResultMpeg4Gif::Ptr result(new InlineQueryResultMpeg4Gif); + result->mpeg4Url = data.get("mpeg4_url"); + result->mpeg4Width = data.get("mpeg4_width", 0); + result->mpeg4Height = data.get("mpeg4_height", 0); + result->caption = data.get("caption", ""); + return result; +} + +std::string TgTypeParser::parseInlineQueryResultMpeg4Gif(const InlineQueryResultMpeg4Gif::Ptr& object) const { + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "mpeg4_url", object->mpeg4Url); + appendToJson(result, "mpeg4_width", object->mpeg4Width); + appendToJson(result, "mpeg4_height", object->mpeg4Height); + appendToJson(result, "caption", object->caption); + // The last comma will be erased by parseInlineQueryResult(). + return result; +} + +InlineQueryResultVideo::Ptr TgTypeParser::parseJsonAndGetInlineQueryResultVideo(const boost::property_tree::ptree& data) const { + // NOTE: This function will be called by parseJsonAndGgetInlineQueryResult(). + InlineQueryResultVideo::Ptr result(new InlineQueryResultVideo); + result->videoUrl = data.get("video_url"); + result->mimeType = data.get("mime_type"); + result->videoWidth = data.get("video_height", 0); + result->videoHeight = data.get("video_height", 0); + result->videoDuration = data.get("video_duration", 0); + result->description = data.get("description", ""); + return result; +} + +std::string TgTypeParser::parseInlineQueryResultVideo(const InlineQueryResultVideo::Ptr& object) const { + if (!object){ + return " "; + } + // This function will be called by parseInlineQueryResult(), so I don't add + // curly brackets to the result string. + string result; + appendToJson(result, "video_url", object->videoUrl); + appendToJson(result, "mime_type", object->mimeType); + appendToJson(result, "video_width", object->videoWidth); + appendToJson(result, "video_height", object->videoHeight); + appendToJson(result, "video_duration", object->videoDuration); + appendToJson(result, "description", object->description); + // The last comma will be erased by parseInlineQueryResult(). + return result; +} + +ChosenInlineResult::Ptr TgTypeParser::parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const { + ChosenInlineResult::Ptr result(new ChosenInlineResult); + result->resultId = data.get("result_id"); + result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->query = data.get("query"); + return result; +} + +std::string TgTypeParser::parseChosenInlineResult(const ChosenInlineResult::Ptr& object) const { + if (!object){ + return ""; + } + + string result; + result += '{'; + appendToJson(result, "result_id", object->resultId); + appendToJson(result, "user", parseUser(object->user)); + appendToJson(result, "query", object->query); + result.erase(result.length() - 1); + result += '}'; + return result; +} + void TgTypeParser::appendToJson(string& json, const string& varName, const string& value) const { if (value.empty()) { return; -- cgit v1.2.3 From d2baea74a0ac09db992ab36a861d09caccdab697 Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Sun, 27 Mar 2016 16:31:11 +0200 Subject: Add answerInlineQuery method --- src/Api.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index 2e27e1d..47c7882 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -255,6 +255,17 @@ void Api::setWebhook(const string& url) const { sendRequest("setWebhook", args); } +void Api::answerInlineQuery(const std::string inlineQueryId, const std::vector results, + int32_t cacheTime=300, bool isPersonal=false, const std::string& nextOffset=""){ + vector args; + args.push_back(HttpReqArg("inline_query_id", inlineQueryId)); + args.push_back(HttpReqArg("results", results, TgTypeParser::getInstance().parseInlineQueryResult)); + args.push_back(HttpReqArg("cache_time", cacheTime)); + args.push_back(HttpReqArg("is_personal", isPersonal)); + args.push_back(HttpReqArg("next_offset", nextOffset)); + sendRequest("answerInlineQuery", args); +} + ptree Api::sendRequest(const string& method, const vector& args) const { string url = "https://api.telegram.org/bot"; -- cgit v1.2.3 From 405d973af3460ee2860d01c7169cf6749fa98b07 Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Sun, 27 Mar 2016 18:24:03 +0200 Subject: Fixed compilation errors --- src/Api.cpp | 7 ++++--- src/TgTypeParser.cpp | 2 +- 2 files changed, 5 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index 47c7882..325978f 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -255,11 +255,12 @@ void Api::setWebhook(const string& url) const { sendRequest("setWebhook", args); } -void Api::answerInlineQuery(const std::string inlineQueryId, const std::vector results, - int32_t cacheTime=300, bool isPersonal=false, const std::string& nextOffset=""){ +void Api::answerInlineQuery(const std::string& inlineQueryId, const std::vector& results, + int32_t cacheTime, bool isPersonal, const std::string& nextOffset){ vector args; args.push_back(HttpReqArg("inline_query_id", inlineQueryId)); - args.push_back(HttpReqArg("results", results, TgTypeParser::getInstance().parseInlineQueryResult)); + string resultsJson = TgTypeParser::getInstance().parseArray(&TgTypeParser::parseInlineQueryResult, results); + args.push_back(HttpReqArg("results", resultsJson)); args.push_back(HttpReqArg("cache_time", cacheTime)); args.push_back(HttpReqArg("is_personal", isPersonal)); args.push_back(HttpReqArg("next_offset", nextOffset)); diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index eb7f769..079e073 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -362,7 +362,7 @@ string TgTypeParser::parseUpdate(const Update::Ptr& object) const { appendToJson(result, "update_id", object->updateId); appendToJson(result, "message", parseMessage(object->message)); appendToJson(result, "inline_query", parseInlineQuery(object->inlineQuery)); - appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult)) + appendToJson(result, "chosen_inline_result", parseChosenInlineResult(object->chosenInlineResult)); result.erase(result.length() - 1); result += '}'; return result; -- cgit v1.2.3 From 20f9f2c198590c0f47a81ea24c4c3c446cd5d74f Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Sun, 27 Mar 2016 21:05:45 +0200 Subject: Move InlineQueryResult.cpp in the right dir --- src/types/InlineQueryResult.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/types/InlineQueryResult.cpp (limited to 'src') diff --git a/src/types/InlineQueryResult.cpp b/src/types/InlineQueryResult.cpp new file mode 100644 index 0000000..be1f14a --- /dev/null +++ b/src/types/InlineQueryResult.cpp @@ -0,0 +1,16 @@ +// +// Created by Andrea Giove on 27/03/16. +// +#include "tgbot/types/InlineQueryResultArticle.h" +#include "tgbot/types/InlineQueryResultGif.h" +#include "tgbot/types/InlineQueryResultMpeg4Gif.h" +#include "tgbot/types/InlineQueryResultPhoto.h" +#include "tgbot/types/InlineQueryResultVideo.h" + +using namespace TgBot; + +const std::string InlineQueryResultArticle::TYPE = "article"; +const std::string InlineQueryResultGif::TYPE = "gif"; +const std::string InlineQueryResultMpeg4Gif::TYPE = "mpeg4_gif"; +const std::string InlineQueryResultPhoto::TYPE = "photo"; +const std::string InlineQueryResultVideo::TYPE = "video"; -- cgit v1.2.3 From c5a4f537b4a11455d1924ae737a92a8f16b2fcd7 Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Tue, 29 Mar 2016 15:23:15 +0200 Subject: Fix problem with Update parsing --- src/TgTypeParser.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 079e073..244496e 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -347,9 +347,9 @@ string TgTypeParser::parseLocation(const Location::Ptr& object) const { Update::Ptr TgTypeParser::parseJsonAndGetUpdate(const ptree& data) const { Update::Ptr result(new Update); result->updateId = data.get("update_id"); - result->message = parseJsonAndGetMessage(data.find("message")->second); - result->inlineQuery = parseJsonAndGetInlineQuery(data.find("inline_query")->second); - result->chosenInlineResult = parseJsonAndGetChosenInlineResult(data.find("chosen_inline_result")->second); + result->message = tryParseJson(&TgTypeParser::parseJsonAndGetMessage, data, "message"); + result->inlineQuery = tryParseJson(&TgTypeParser::parseJsonAndGetInlineQuery, data, "inline_query"); + result->chosenInlineResult = tryParseJson(&TgTypeParser::parseJsonAndGetChosenInlineResult, data, "chosen_inline_result"); return result; } -- cgit v1.2.3 From de33e2e08049c3698920789f769b7f361414f4b0 Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Tue, 29 Mar 2016 17:13:38 +0200 Subject: Edited answerInlineQuery --- src/Api.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/Api.cpp b/src/Api.cpp index 325978f..ed06d27 100644 --- a/src/Api.cpp +++ b/src/Api.cpp @@ -256,7 +256,7 @@ void Api::setWebhook(const string& url) const { } void Api::answerInlineQuery(const std::string& inlineQueryId, const std::vector& results, - int32_t cacheTime, bool isPersonal, const std::string& nextOffset){ + int32_t cacheTime, bool isPersonal, const std::string& nextOffset) const { vector args; args.push_back(HttpReqArg("inline_query_id", inlineQueryId)); string resultsJson = TgTypeParser::getInstance().parseArray(&TgTypeParser::parseInlineQueryResult, results); -- cgit v1.2.3 From b937e88dd43a7708f40af6a3c333046e3121a77a Mon Sep 17 00:00:00 2001 From: Andrea Giove Date: Wed, 30 Mar 2016 14:13:17 +0200 Subject: Fixed ChosenInlineResult issue --- src/TgTypeParser.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp index 244496e..6672c04 100644 --- a/src/TgTypeParser.cpp +++ b/src/TgTypeParser.cpp @@ -711,7 +711,7 @@ std::string TgTypeParser::parseInlineQueryResultVideo(const InlineQueryResultVid ChosenInlineResult::Ptr TgTypeParser::parseJsonAndGetChosenInlineResult(const boost::property_tree::ptree& data) const { ChosenInlineResult::Ptr result(new ChosenInlineResult); result->resultId = data.get("result_id"); - result->user = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "user"); + result->from = tryParseJson(&TgTypeParser::parseJsonAndGetUser, data, "from"); result->query = data.get("query"); return result; } @@ -724,7 +724,7 @@ std::string TgTypeParser::parseChosenInlineResult(const ChosenInlineResult::Ptr& string result; result += '{'; appendToJson(result, "result_id", object->resultId); - appendToJson(result, "user", parseUser(object->user)); + appendToJson(result, "from", parseUser(object->from)); appendToJson(result, "query", object->query); result.erase(result.length() - 1); result += '}'; -- cgit v1.2.3