From e2470071996bf7ed8936c16e5aee433b38483be1 Mon Sep 17 00:00:00 2001 From: Jannik Date: Wed, 18 Nov 2020 07:04:03 -0800 Subject: [PATCH 1/2] Added ping command --- telegram-bot-api/Client.cpp | 32 ++++++++++++++++++++++++++++++++ telegram-bot-api/Client.h | 5 +++++ 2 files changed, 37 insertions(+) diff --git a/telegram-bot-api/Client.cpp b/telegram-bot-api/Client.cpp index 23008f7..fb19f64 100644 --- a/telegram-bot-api/Client.cpp +++ b/telegram-bot-api/Client.cpp @@ -256,6 +256,8 @@ bool Client::init_methods() { methods_.emplace("getparticipants", &Client::process_get_participants_query); methods_.emplace("deletemessages", &Client::process_delete_messages_query); methods_.emplace("togglegroupinvites", &Client::process_toggle_group_invites_query); + methods_.emplace("ping", &Client::process_ping_query); + return true; } @@ -3213,6 +3215,30 @@ class Client::TdOnSendCustomRequestCallback : public TdQueryCallback { PromisedQueryPtr query_; }; +//start custom callbacks impl + +class Client::TdOnPingCallback : public TdQueryCallback { + public: + TdOnPingCallback(PromisedQueryPtr query) + : query_(std::move(query)) { + } + + void on_result(object_ptr result) override { + if (result->get_id() == td_api::error::ID) { + return fail_query_with_error(std::move(query_), move_object_as(result), "Server not available"); + } + CHECK(result->get_id() == 959899022); // id for return type `seconds` + + auto seconds_ = move_object_as(result); + answer_query(td::VirtuallyJsonableString(std::to_string(seconds_->seconds_)), std::move(query_)); + } + + private: + PromisedQueryPtr query_; +}; + +//end custom callbacks impl + void Client::close() { need_close_ = true; if (td_client_.empty()) { @@ -7500,6 +7526,12 @@ td::Status Client::process_toggle_group_invites_query(PromisedQueryPtr &query) { return Status::OK(); } +td::Status Client::process_ping_query(PromisedQueryPtr &query) { + send_request(make_object(), + std::make_unique(std::move(query))); + return Status::OK(); +} + //end custom methods impl void Client::do_get_file(object_ptr file, PromisedQueryPtr query) { diff --git a/telegram-bot-api/Client.h b/telegram-bot-api/Client.h index cc6a7a9..4622244 100644 --- a/telegram-bot-api/Client.h +++ b/telegram-bot-api/Client.h @@ -178,6 +178,10 @@ class Client : public WebhookActor::Callback { class TdOnCancelDownloadFileCallback; class TdOnSendCustomRequestCallback; + //start custom callbacks + class TdOnPingCallback; + //end custom callbacks + void on_get_reply_message(int64 chat_id, object_ptr reply_to_message); void on_get_edited_message(object_ptr edited_message); @@ -492,6 +496,7 @@ class Client : public WebhookActor::Callback { Status process_get_participants_query(PromisedQueryPtr &query); Status process_delete_messages_query(PromisedQueryPtr &query); Status process_toggle_group_invites_query(PromisedQueryPtr &query); + Status process_ping_query(PromisedQueryPtr &query); void webhook_verified(td::string cached_ip_address) override; From f2daedea706b485504ef0ca99c206681d1df7022 Mon Sep 17 00:00:00 2001 From: Jannik <32801117+code1mountain@users.noreply.github.com> Date: Wed, 18 Nov 2020 16:10:55 +0100 Subject: [PATCH 2/2] Update README.md --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index bd43e36..6f032ab 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,15 @@ The maximum number of messages to be deleted in a single batch is determined by - `end` Last message id to delete ##### Returns `true` +#### Command `ping` +Send an MTProto ping message to the telegram servers. +Useful to detect the delay of the bot api server. + +##### Parameters +No parameters +##### Returns `string` +Ping delay in seconds represented as string. +