summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Api.cpp67
-rw-r--r--src/TgTypeParser.cpp89
2 files changed, 139 insertions, 17 deletions
diff --git a/src/Api.cpp b/src/Api.cpp
index 9996108..19681f7 100644
--- a/src/Api.cpp
+++ b/src/Api.cpp
@@ -276,6 +276,7 @@ bool Api::answerPreCheckoutQuery(const std::string& preCheckoutQueryId, bool ok,
return sendRequest("answerPreCheckoutQuery", args).get<bool>("", false);
}
+
Message::Ptr Api::sendSticker(int64_t chatId, const boost::variant<InputFile::Ptr, std::string> sticker, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const {
vector<HttpReqArg> args;
args.reserve(5);
@@ -981,28 +982,17 @@ bool Api::unbanChatMember(int64_t chatId, int32_t userId) const {
args.emplace_back("user_id", userId);
return sendRequest("unbanChatMember", args).get<bool>("", false);
}
-
-bool Api::restrictChatMember(int64_t chatId, int32_t userId, uint64_t untilDate, bool canSendMessages,
- bool canSendMediaMessages, bool canSendOtherMessages, bool canAddWebPagePreviews) const {
+bool Api::restrictChatMember(int64_t chatId, int32_t userId, TgBot::ChatPermissions::Ptr permissions,
+ uint64_t untilDate) const {
vector<HttpReqArg> args;
- args.reserve(7);
+ args.reserve(4);
args.emplace_back("chat_id", chatId);
args.emplace_back("user_id", userId);
+ args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions));
if (untilDate) {
args.emplace_back("until_date", untilDate);
}
- if (canSendMessages) {
- args.emplace_back("can_send_messages", canSendMessages);
- }
- if (canSendMediaMessages) {
- args.emplace_back("can_send_media_messages", canSendMediaMessages);
- }
- if (canSendOtherMessages) {
- args.emplace_back("can_send_other_messages", canSendOtherMessages);
- }
- if (canAddWebPagePreviews) {
- args.emplace_back("can_add_web_page_previews", canAddWebPagePreviews);
- }
+
return sendRequest("restrictChatMember", args).get<bool>("", false);
}
@@ -1036,6 +1026,14 @@ bool Api::promoteChatMember(int64_t chatId, int32_t userId, bool canChangeInfo,
return sendRequest("promoteChatMember", args).get<bool>("", false);
}
+bool Api::setChatPermissions(int64_t chatId, ChatPermissions::Ptr permissions) const{
+ vector<HttpReqArg> args;
+ args.reserve(2);
+ args.emplace_back("chat_id", chatId);
+ args.emplace_back("permissions", _tgTypeParser.parseChatPermissions(permissions));
+ return sendRequest("setChatPermissions", args).get<bool>("", false);
+}
+
string Api::exportChatInviteLink(int64_t chatId) const {
vector<HttpReqArg> args;
args.reserve(1);
@@ -1142,6 +1140,43 @@ void Api::deleteMessage(int64_t chatId, int32_t messageId) const {
sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) });
}
+Message::Ptr Api::sendPoll(int64_t chat_id, std::string question, std::vector<std::string> options, bool disable_notification, int32_t reply_to_message_id,GenericReply::Ptr reply_markup) const {
+ vector<HttpReqArg> args;
+ args.reserve(6);
+ args.emplace_back("chat_id", chat_id);
+ args.emplace_back("question", question);
+ std::string json_array;
+ json_array = "[";
+ for(const auto &arr : options){
+ json_array += "\"" + arr + "\"" + ",";
+ }
+ json_array.pop_back();
+ json_array += "]";
+ args.emplace_back("options", json_array);
+
+ if (disable_notification){
+ args.emplace_back("disable_notification", disable_notification);
+ }
+ if (reply_to_message_id!=0){
+ args.emplace_back("reply_to_message_id", reply_to_message_id);
+ }
+ if (reply_markup){
+ args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(reply_markup));
+ }
+ return _tgTypeParser.parseJsonAndGetMessage(sendRequest("sendPoll", args));
+}
+
+Poll::Ptr Api::stopPoll(int64_t chatId, int64_t messageId, const InlineKeyboardMarkup::Ptr replyMarkup) const {
+ vector<HttpReqArg> args;
+ args.reserve(3);
+ args.emplace_back("chat_id", chatId);
+ args.emplace_back("message_id", messageId);
+ if (replyMarkup){
+ args.emplace_back("reply_markup", _tgTypeParser.parseGenericReply(replyMarkup));
+ }
+ return _tgTypeParser.parseJsonAndGetPoll(sendRequest("stopPoll", args));
+}
+
ptree Api::sendRequest(const string& method, const vector<HttpReqArg>& args) const {
string url = "https://api.telegram.org/bot";
url += _token;
diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp
index daa7b4b..999dbc1 100644
--- a/src/TgTypeParser.cpp
+++ b/src/TgTypeParser.cpp
@@ -144,6 +144,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const {
result->forwardFromChat = tryParseJson<Chat>(&TgTypeParser::parseJsonAndGetChat, data, "forward_from_chat");
result->forwardFromMessageId = data.get<int32_t>("forward_from_message_id", 0);
result->forwardSignature = data.get("forward_signature", "");
+ result->forwardSenderName = data.get("forward_sender_name", "");
result->forwardDate = data.get("forward_date", 0);
result->replyToMessage = tryParseJson<Message>(&TgTypeParser::parseJsonAndGetMessage, data, "reply_to_message");
result->editDate = data.get<int32_t>("edit_date", 0);
@@ -161,6 +162,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const {
result->voice = tryParseJson<Voice>(&TgTypeParser::parseJsonAndGetVoice, data, "voice");
result->contact = tryParseJson<Contact>(&TgTypeParser::parseJsonAndGetContact, data, "contact");
result->location = tryParseJson<Location>(&TgTypeParser::parseJsonAndGetLocation, data, "location");
+ result->poll = tryParseJson<Poll>(&TgTypeParser::parseJsonAndGetPoll, data, "poll");
result->newChatMember = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_participant");
result->newChatMembers = parseJsonAndGetArray<User>(&TgTypeParser::parseJsonAndGetUser, data, "new_chat_members");
result->leftChatMember = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "left_chat_participant");
@@ -177,6 +179,7 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const {
result->invoice = tryParseJson<Invoice>(&TgTypeParser::parseJsonAndGetInvoice, data, "invoice");
result->successfulPayment = tryParseJson<SuccessfulPayment>(&TgTypeParser::parseJsonAndGetSuccessfulPayment, data, "successful_payment");
result->connectedWebsite = data.get("connected_website", "");
+ result->replyMarkup = tryParseJson<InlineKeyboardMarkup>(&TgTypeParser::parseJsonAndGetInlineKeyboardMarkup, data, "reply_markup");
return result;
}
@@ -194,6 +197,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const {
appendToJson(result, "forward_from_chat", parseChat(object->forwardFromChat));
appendToJson(result, "forward_from_message_id", object->forwardFromMessageId);
appendToJson(result, "forward_signature", object->forwardSignature);
+ appendToJson(result, "forward_sender_name", object->forwardSenderName);
appendToJson(result, "forward_date", object->forwardDate);
appendToJson(result, "reply_to_message", parseMessage(object->replyToMessage));
appendToJson(result, "edit_date", object->editDate);
@@ -208,6 +212,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const {
appendToJson(result, "voice", parseVoice(object->voice));
appendToJson(result, "contact", parseContact(object->contact));
appendToJson(result, "location", parseLocation(object->location));
+ appendToJson(result, "poll", parsePoll(object->poll));
appendToJson(result, "new_chat_member", parseUser(object->newChatMember));
appendToJson(result, "new_chat_members", parseArray(&TgTypeParser::parseUser, object->newChatMembers));
appendToJson(result, "left_chat_member", parseUser(object->leftChatMember));
@@ -224,6 +229,7 @@ string TgTypeParser::parseMessage(const Message::Ptr& object) const {
appendToJson(result, "connected_website", object->connectedWebsite);
appendToJson(result, "invoice", parseInvoice(object->invoice));
appendToJson(result, "successful_payment", parseSuccessfulPayment(object->successfulPayment));
+ appendToJson(result, "reply_markup", parseInlineKeyboardMarkup(object->replyMarkup));
removeLastComma(result);
result += '}';
return result;
@@ -312,6 +318,7 @@ Sticker::Ptr TgTypeParser::parseJsonAndGetSticker(const ptree& data) const {
result->fileId = data.get<string>("file_id");
result->width = data.get<int32_t>("width");
result->height = data.get<int32_t>("height");
+ result->isAnimated = data.get<bool>("is_animated", false);
result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb");
result->emoji = data.get("emoji", "");
result->setName = data.get("set_name", "");
@@ -329,6 +336,7 @@ string TgTypeParser::parseSticker(const Sticker::Ptr& object) const {
appendToJson(result, "file_id", object->fileId);
appendToJson(result, "width", object->width);
appendToJson(result, "height", object->height);
+ appendToJson(result, "is_animated", object->isAnimated);
appendToJson(result, "thumb", parsePhotoSize(object->thumb));
appendToJson(result, "emoji", object->emoji);
appendToJson(result, "file_size", object->fileSize);
@@ -342,6 +350,7 @@ StickerSet::Ptr TgTypeParser::parseJsonAndGetStickerSet(const ptree& data) const
result->name = data.get("name", "");
result->title = data.get("title", "");
result->containsMasks = data.get<bool>("contains_masks", false);
+ result->isAnimated = data.get<bool>("is_animated", false);
result->stickers = parseJsonAndGetArray<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "stickers");
return result;
}
@@ -354,6 +363,7 @@ string TgTypeParser::parseStickerSet(const StickerSet::Ptr& object) const {
result += '{';
appendToJson(result, "name", object->name);
appendToJson(result, "title", object->title);
+ appendToJson(result, "is_animated", object->isAnimated);
appendToJson(result, "contains_masks", object->containsMasks);
appendToJson(result, "stickers", parseArray(&TgTypeParser::parseSticker, object->stickers));
removeLastComma(result);
@@ -385,6 +395,82 @@ string TgTypeParser::parseMaskPosition(const MaskPosition::Ptr& object) const {
return result;
}
+Poll::Ptr TgTypeParser::parseJsonAndGetPoll(const ptree& data) const {
+ auto result(make_shared<Poll>());
+ result->id = data.get("id", 0);
+ result->question = data.get("question", "");
+ result->options = tryParseJson<PollOption>(&TgTypeParser::parseJsonAndGetPollOption, data, "options");
+ result->is_closed = data.get<bool>("is_closed");
+ return result;
+}
+
+string TgTypeParser::parsePoll(const Poll::Ptr& object) const {
+ if (!object) {
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "id", object->id);
+ appendToJson(result, "question", object->question);
+ appendToJson(result, "options", parsePollOption(object->options));
+ appendToJson(result, "is_closed", object->is_closed);
+ removeLastComma(result);
+ result += '}';
+ return result;
+}
+
+PollOption::Ptr TgTypeParser::parseJsonAndGetPollOption(const ptree& data) const {
+ auto result(make_shared<PollOption>());
+ result->text = data.get("text", "");
+ result->voter_count = data.get("voter_count", 0);
+ return result;
+}
+
+string TgTypeParser::parsePollOption(const PollOption::Ptr& object) const {
+ if (!object) {
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "text", object->text);
+ appendToJson(result, "voter_count", object->voter_count);
+ removeLastComma(result);
+ result += '}';
+ return result;
+}
+
+ChatPermissions::Ptr TgTypeParser::parseJsonAndGetChatPermissions(const ptree& data) const {
+ auto result(make_shared<ChatPermissions>());
+ result->canSendMessages = data.get<bool>("can_send_messages");
+ result->canSendMediaMessages = data.get<bool>("can_send_media_messages");
+ result->canSendPolls = data.get<bool>("can_send_polls");
+ result->canSendOtherMessages = data.get<bool>("can_send_other_messages");
+ result->canAddWebPagePreviews = data.get<bool>("can_add_web_page_previews");
+ result->canChangeInfo = data.get<bool>("can_change_info");
+ result->canInviteUsers = data.get<bool>("can_invite_users");
+ result->canPinMessages = data.get<bool>("can_pin_messages");
+ return result;
+}
+
+string TgTypeParser::parseChatPermissions(const ChatPermissions::Ptr& object) const {
+ if (!object) {
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "can_send_messages", object->canSendMessages);
+ appendToJson(result, "can_send_media_messages", object->canSendMediaMessages);
+ appendToJson(result, "can_send_polls", object->canSendPolls);
+ appendToJson(result, "can_send_other_messages", object->canSendOtherMessages);
+ appendToJson(result, "can_add_web_page_previews", object->canAddWebPagePreviews);
+ appendToJson(result, "can_change_info", object->canChangeInfo);
+ appendToJson(result, "can_invite_users", object->canInviteUsers);
+ appendToJson(result, "can_pin_messages", object->canPinMessages);
+ removeLastComma(result);
+ result += '}';
+ return result;
+}
+
Video::Ptr TgTypeParser::parseJsonAndGetVideo(const ptree& data) const {
auto result(make_shared<Video>());
result->fileId = data.get<string>("file_id");
@@ -877,6 +963,7 @@ ChatMember::Ptr TgTypeParser::parseJsonAndGetChatMember(const boost::property_tr
result->canInviteUsers = data.get<bool>("can_invite_users", false);
result->canRestrictMembers = data.get<bool>("can_restrict_members", false);
result->canPinMessages = data.get<bool>("can_pin_messages", false);
+ result->isMember = data.get<bool>("is_member", false);
result->canPromoteMembers = data.get<bool>("can_promote_messages", false);
result->canSendMessages = data.get<bool>("can_send_messages", false);
result->canSendMediaMessages = data.get<bool>("can_send_media_messages", false);
@@ -1710,6 +1797,7 @@ InlineKeyboardButton::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardButton(cons
auto result(make_shared<InlineKeyboardButton>());
result->text = data.get<string>("text");
result->url = data.get<string>("url", "");
+ result->loginUrl = make_shared<LoginUrl>();
result->callbackData = data.get<string>("callback_data", "");
result->switchInlineQuery = data.get<string>("switch_inline_query", "");
result->switchInlineQueryCurrentChat = data.get<string>("switch_inline_query_current_chat", "");
@@ -2128,5 +2216,4 @@ void TgTypeParser::appendToJson(string& json, const string& varName, const strin
}
json += ',';
}
-
}