summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorOleg Morozenkov <omorozenkov@gmail.com>2018-06-05 19:06:23 +0300
committerGitHub <noreply@github.com>2018-06-05 19:06:23 +0300
commit1a90b5022d47bc0059214381b74db6fae9767cef (patch)
treef88e480700cb36f8acdcfdebcbac01328f16c98d /src
parent381a1cd0d949db88abd0b28bc717a8690b93ccc0 (diff)
parent0f4e792a641527aa590a1a9e23bdaad2f16ab6bc (diff)
Merge pull request #63 from JellyBrick/master
Bot API 2.2 (Game) update / Add optimization option
Diffstat (limited to 'src')
-rw-r--r--src/Api.cpp60
-rw-r--r--src/TgTypeParser.cpp104
-rw-r--r--src/net/HttpClient.cpp24
3 files changed, 183 insertions, 5 deletions
diff --git a/src/Api.cpp b/src/Api.cpp
index 269b8a6..11349e9 100644
--- a/src/Api.cpp
+++ b/src/Api.cpp
@@ -497,6 +497,22 @@ Message::Ptr Api::sendVoice(int64_t chatId, const string& voiceId, const string
return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendVoice", args));
}
+Message::Ptr Api::sendGame(int64_t chatId, const std::string& gameShortName, int32_t replyToMessageId, const InlineKeyboardMarkup::Ptr replyMarkup, bool disableNotification) const {
+ vector<HttpReqArg> args;
+ args.push_back(HttpReqArg("chat_id", chatId));
+ args.push_back(HttpReqArg("game_short_name", gameShortName));
+ if (replyToMessageId) {
+ args.push_back(HttpReqArg("reply_to_message_id", replyToMessageId));
+ }
+ if (replyMarkup) {
+ args.push_back(HttpReqArg("reply_markup", TgTypeParser::getInstance().parseGenericReply(replyMarkup)));
+ }
+ if (disableNotification){
+ args.push_back(HttpReqArg("disable_notification", disableNotification));
+ }
+ return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("sendGame", args));
+}
+
Message::Ptr Api::sendLocation(int64_t chatId, float latitude, float longitude, uint32_t livePeriod, int32_t replyToMessageId, const GenericReply::Ptr replyMarkup, bool disableNotification) const {
vector<HttpReqArg> args;
args.push_back(HttpReqArg("chat_id", chatId));
@@ -975,6 +991,50 @@ bool Api::unpinChatMessage(int64_t chatId) const {
return sendRequest("unpinChatMessage", args).get<bool>("", false);
}
+Message::Ptr Api::setGameScore(int32_t userId, int32_t score, bool force, bool disableEditMessage, int64_t chatId, int32_t messageId, const std::string& inlineMessageId) const {
+ vector<HttpReqArg> args;
+ args.push_back(HttpReqArg("user_id", userId));
+ args.push_back(HttpReqArg("score", score));
+ if (force) {
+ args.push_back(HttpReqArg("force", force));
+ }
+ if (disableEditMessage) {
+ args.push_back(HttpReqArg("disable_edit_message", disableEditMessage));
+ }
+ if (chatId){
+ args.push_back(HttpReqArg("chat_id", chatId));
+ }
+ if (messageId){
+ args.push_back(HttpReqArg("message_id", messageId));
+ }
+ if (!inlineMessageId.empty()){
+ args.push_back(HttpReqArg("inline_message_id", inlineMessageId));
+ }
+ return TgTypeParser::getInstance().parseJsonAndGetMessage(sendRequest("setGameScore", args));
+}
+
+vector<GameHighScore::Ptr> Api::getGameHighScores(int32_t userId, int32_t score, bool force, bool disableEditMessage, int64_t chatId, int32_t messageId, const std::string& inlineMessageId) const {
+ vector<HttpReqArg> args;
+ args.push_back(HttpReqArg("user_id", userId));
+ args.push_back(HttpReqArg("score", score));
+ if (force) {
+ args.push_back(HttpReqArg("force", force));
+ }
+ if (disableEditMessage) {
+ args.push_back(HttpReqArg("disable_edit_message", disableEditMessage));
+ }
+ if (chatId) {
+ args.push_back(HttpReqArg("chat_id", chatId));
+ }
+ if (messageId) {
+ args.push_back(HttpReqArg("message_id", messageId));
+ }
+ if (!inlineMessageId.empty()){
+ args.push_back(HttpReqArg("inline_message_id", inlineMessageId));
+ }
+ return TgTypeParser::getInstance().parseJsonAndGetArray<GameHighScore>(&TgTypeParser::parseJsonAndGetGameHighScore, sendRequest("getGameHighScores", args));
+}
+
void Api::deleteMessage(int64_t chatId, int32_t messageId) const {
sendRequest("deleteMessage", { HttpReqArg("chat_id", chatId), HttpReqArg("message_id", messageId) });
}
diff --git a/src/TgTypeParser.cpp b/src/TgTypeParser.cpp
index 4ba571f..ca7547d 100644
--- a/src/TgTypeParser.cpp
+++ b/src/TgTypeParser.cpp
@@ -113,7 +113,7 @@ string TgTypeParser::parseUser(const User::Ptr& object) const {
return result;
}
-MessageEntity::Ptr TgTypeParser::parseJsonAndGetEntity(const ptree& data) const{
+MessageEntity::Ptr TgTypeParser::parseJsonAndGetMessageEntity(const ptree& data) const{
auto result(make_shared<MessageEntity>());
result->type = data.get<string>("type");
result->offset = data.get<int32_t>("offset");
@@ -121,7 +121,23 @@ MessageEntity::Ptr TgTypeParser::parseJsonAndGetEntity(const ptree& data) const{
result->url = data.get<string>("url", "");
result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user");
return result;
-}
+}
+
+string TgTypeParser::parseMessageEntity(const MessageEntity::Ptr& object) const {
+ if (!object) {
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "type", object->type);
+ appendToJson(result, "offset", object->offset);
+ appendToJson(result, "length", object->length);
+ appendToJson(result, "url", object->url);
+ appendToJson(result, "user", parseUser(object->user));
+ result.erase(result.length() - 1);
+ result += '}';
+ return result;
+}
Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const {
auto result(make_shared<Message>());
@@ -138,10 +154,11 @@ Message::Ptr TgTypeParser::parseJsonAndGetMessage(const ptree& data) const {
result->editDate = data.get<int32_t>("edit_date", 0);
result->authorSignature = data.get("author_signature", "");
result->text = data.get("text", "");
- result->entities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetEntity, data, "entities");
- result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetEntity, data, "caption_entities");
+ result->entities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "entities");
+ result->captionEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "caption_entities");
result->audio = tryParseJson<Audio>(&TgTypeParser::parseJsonAndGetAudio, data, "audio");
result->document = tryParseJson<Document>(&TgTypeParser::parseJsonAndGetDocument, data, "document");
+ result->game = tryParseJson<Game>(&TgTypeParser::parseJsonAndGetGame, data, "game");
result->photo = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photo");
result->sticker = tryParseJson<Sticker>(&TgTypeParser::parseJsonAndGetSticker, data, "sticker");
result->video = tryParseJson<Video>(&TgTypeParser::parseJsonAndGetVideo, data, "video");
@@ -394,7 +411,7 @@ string TgTypeParser::parseVideo(const Video::Ptr& object) const {
}
VideoNote::Ptr TgTypeParser::parseJsonAndGetVideoNote(const ptree& data) const {
- VideoNote::Ptr result(new VideoNote);
+ auto result(make_shared<VideoNote>());
result->fileId = data.get<string>("file_id");
result->length = data.get<int32_t>("length");
result->duration = data.get<int32_t>("duration");
@@ -419,6 +436,82 @@ string TgTypeParser::parseVideoNote(const VideoNote::Ptr& object) const {
return result;
}
+Game::Ptr TgTypeParser::parseJsonAndGetGame(const ptree& data) const {
+ auto result(make_shared<Game>());
+ result->title = data.get("title", "");
+ result->description = data.get("description", "");
+ result->photo = parseJsonAndGetArray<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "photo");
+ result->text = data.get("text", "");
+ result->textEntities = parseJsonAndGetArray<MessageEntity>(&TgTypeParser::parseJsonAndGetMessageEntity, data, "text_entities");
+ result->animation = tryParseJson<Animation>(&TgTypeParser::parseJsonAndGetAnimation, data, "animation");
+ return result;
+}
+
+string TgTypeParser::parseGame(const Game::Ptr& object) const {
+ if (!object) {
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "title", object->title);
+ appendToJson(result, "description", object->description);
+ appendToJson(result, "photo", parseArray(&TgTypeParser::parsePhotoSize, object->photo));
+ appendToJson(result, "text", object->text);
+ appendToJson(result, "text_entities", parseArray(&TgTypeParser::parseMessageEntity, object->textEntities));
+ appendToJson(result, "animation", parseAnimation(object->animation));
+ result.erase(result.length() - 1);
+ result += '}';
+ return result;
+}
+
+GameHighScore::Ptr TgTypeParser::parseJsonAndGetGameHighScore(const ptree& data) const {
+ auto result(make_shared<GameHighScore>());
+ result->position = data.get("position", "");
+ result->user = tryParseJson<User>(&TgTypeParser::parseJsonAndGetUser, data, "user");
+ result->score = data.get<int32_t>("score", 0);
+ return result;
+}
+
+string TgTypeParser::parseGameHighScore(const GameHighScore::Ptr& object) const {
+ if (!object) {
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "position", object->position);
+ appendToJson(result, "user", parseUser(object->user));
+ appendToJson(result, "score", object->score);
+ result.erase(result.length() - 1);
+ result += '}';
+ return result;
+}
+
+Animation::Ptr TgTypeParser::parseJsonAndGetAnimation(const ptree& data) const {
+ auto result(make_shared<Animation>());
+ result->fileId = data.get("file_id", "");
+ result->thumb = tryParseJson<PhotoSize>(&TgTypeParser::parseJsonAndGetPhotoSize, data, "thumb");
+ result->fileName = data.get("file_name", "");
+ result->mimeType = data.get("mime_type", "");
+ result->fileSize = data.get<int32_t>("file_size", 0);
+ return result;
+}
+
+string TgTypeParser::parseAnimation(const Animation::Ptr& object) const {
+ if (!object) {
+ return "";
+ }
+ string result;
+ result += '{';
+ appendToJson(result, "file_id", object->fileId);
+ appendToJson(result, "thumb", parsePhotoSize(object->thumb));
+ appendToJson(result, "file_name", object->fileName);
+ appendToJson(result, "mime_type", object->mimeType);
+ appendToJson(result, "file_size", object->fileSize);
+ result.erase(result.length() - 1);
+ result += '}';
+ return result;
+}
+
Contact::Ptr TgTypeParser::parseJsonAndGetContact(const ptree& data) const {
auto result(make_shared<Contact>());
result->phoneNumber = data.get<string>("phone_number");
@@ -1537,6 +1630,7 @@ InlineKeyboardButton::Ptr TgTypeParser::parseJsonAndGetInlineKeyboardButton(cons
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", "");
+ result->callbackGame = make_shared<CallbackGame>();
return result;
}
std::string TgTypeParser::parseInlineKeyboardButton(const InlineKeyboardButton::Ptr& object) const {
diff --git a/src/net/HttpClient.cpp b/src/net/HttpClient.cpp
index f7f4a6b..340eb1e 100644
--- a/src/net/HttpClient.cpp
+++ b/src/net/HttpClient.cpp
@@ -45,6 +45,20 @@ string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) {
connect(socket.lowest_layer(), resolver.resolve(query));
+ #ifdef TGBOT_DISABLE_NAGLES_ALGORITHM
+ socket.lowest_layer().set_option(tcp::no_delay(true));
+ #endif //TGBOT_DISABLE_NAGLES_ALGORITHM
+
+ #ifdef TGBOT_CHANGE_SOCKET_BUFFER_SIZE
+ #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__
+ socket.lowest_layer().set_option(socket_base::send_buffer_size(65536));
+ socket.lowest_layer().set_option(socket_base::receive_buffer_size(65536));
+ #else //for 32-bit
+ socket.lowest_layer().set_option(socket_base::send_buffer_size(32768));
+ socket.lowest_layer().set_option(socket_base::receive_buffer_size(32768));
+ #endif //Processor architecture
+ #endif //TGBOT_CHANGE_SOCKET_BUFFER_SIZE
+
socket.set_verify_mode(ssl::verify_none);
socket.set_verify_callback(ssl::rfc2818_verification(url.host));
socket.handshake(ssl::stream<tcp::socket>::client);
@@ -53,7 +67,17 @@ string HttpClient::makeRequest(const Url& url, const vector<HttpReqArg>& args) {
write(socket, buffer(requestText.c_str(), requestText.length()));
string response;
+
+ #ifdef TGBOT_CHANGE_READ_BUFFER_SIZE
+ #if _WIN64 || __amd64__ || __x86_64__ || __MINGW64__ || __aarch64__ || __powerpc64__
+ char buff[65536];
+ #else //for 32-bit
+ char buff[32768];
+ #endif //Processor architecture
+ #else
char buff[1024];
+ #endif //TGBOT_CHANGE_READ_BUFFER_SIZE
+
boost::system::error_code error;
while (!error) {
size_t bytes = read(socket, buffer(buff), error);