2018-12-31 20:04:05 +01:00
|
|
|
//
|
2020-01-01 02:23:48 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2020
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include <td/telegram/Client.h>
|
|
|
|
#include <td/telegram/td_api.h>
|
|
|
|
#include <td/telegram/td_api.hpp>
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <functional>
|
|
|
|
#include <iostream>
|
|
|
|
#include <limits>
|
|
|
|
#include <map>
|
2018-09-27 03:19:03 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
// Simple single-threaded example of TDLib usage.
|
|
|
|
// Real world programs should use separate thread for the user input.
|
2018-03-02 01:13:30 +01:00
|
|
|
// Example includes user authentication, receiving updates, getting chat list and sending text messages.
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// overloaded
|
|
|
|
namespace detail {
|
|
|
|
template <class... Fs>
|
|
|
|
struct overload;
|
|
|
|
|
|
|
|
template <class F>
|
|
|
|
struct overload<F> : public F {
|
|
|
|
explicit overload(F f) : F(f) {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
template <class F, class... Fs>
|
|
|
|
struct overload<F, Fs...>
|
|
|
|
: public overload<F>
|
|
|
|
, overload<Fs...> {
|
|
|
|
overload(F f, Fs... fs) : overload<F>(f), overload<Fs...>(fs...) {
|
|
|
|
}
|
|
|
|
using overload<F>::operator();
|
|
|
|
using overload<Fs...>::operator();
|
|
|
|
};
|
|
|
|
} // namespace detail
|
|
|
|
|
|
|
|
template <class... F>
|
|
|
|
auto overloaded(F... f) {
|
|
|
|
return detail::overload<F...>(f...);
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace td_api = td::td_api;
|
|
|
|
|
|
|
|
class TdExample {
|
|
|
|
public:
|
|
|
|
TdExample() {
|
2020-11-13 17:22:57 +01:00
|
|
|
td::ClientManager::execute(td_api::make_object<td_api::setLogVerbosityLevel>(1));
|
|
|
|
client_manager_ = std::make_unique<td::ClientManager>();
|
2020-11-14 23:13:11 +01:00
|
|
|
client_id_ = client_manager_->create_client_id();
|
2020-11-13 17:22:57 +01:00
|
|
|
send_query(td_api::make_object<td_api::getOption>("version"), {});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
while (true) {
|
|
|
|
if (need_restart_) {
|
|
|
|
restart();
|
|
|
|
} else if (!are_authorized_) {
|
2020-11-13 17:22:57 +01:00
|
|
|
process_response(client_manager_->receive(10));
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Enter action [q] quit [u] check for updates and request results [c] show chats [m <id> <text>] "
|
2018-09-14 20:03:36 +02:00
|
|
|
"send message [me] show self [l] logout: "
|
2018-01-22 20:38:55 +01:00
|
|
|
<< std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::string line;
|
|
|
|
std::getline(std::cin, line);
|
|
|
|
std::istringstream ss(line);
|
|
|
|
std::string action;
|
|
|
|
if (!(ss >> action)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (action == "q") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (action == "u") {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Checking for updates..." << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
while (true) {
|
2020-11-13 17:22:57 +01:00
|
|
|
auto response = client_manager_->receive(0);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (response.object) {
|
|
|
|
process_response(std::move(response));
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-09-14 18:35:34 +02:00
|
|
|
} else if (action == "close") {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Closing..." << std::endl;
|
2018-09-14 18:35:34 +02:00
|
|
|
send_query(td_api::make_object<td_api::close>(), {});
|
2018-09-14 20:03:36 +02:00
|
|
|
} else if (action == "me") {
|
|
|
|
send_query(td_api::make_object<td_api::getMe>(),
|
2019-11-13 18:22:25 +01:00
|
|
|
[this](Object object) { std::cout << to_string(object) << std::endl; });
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (action == "l") {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Logging out..." << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
send_query(td_api::make_object<td_api::logOut>(), {});
|
|
|
|
} else if (action == "m") {
|
|
|
|
std::int64_t chat_id;
|
|
|
|
ss >> chat_id;
|
|
|
|
ss.get();
|
|
|
|
std::string text;
|
|
|
|
std::getline(ss, text);
|
|
|
|
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Sending message to chat " << chat_id << "..." << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto send_message = td_api::make_object<td_api::sendMessage>();
|
|
|
|
send_message->chat_id_ = chat_id;
|
|
|
|
auto message_content = td_api::make_object<td_api::inputMessageText>();
|
2018-01-30 18:06:54 +01:00
|
|
|
message_content->text_ = td_api::make_object<td_api::formattedText>();
|
|
|
|
message_content->text_->text_ = std::move(text);
|
2018-12-31 20:04:05 +01:00
|
|
|
send_message->input_message_content_ = std::move(message_content);
|
|
|
|
|
|
|
|
send_query(std::move(send_message), {});
|
|
|
|
} else if (action == "c") {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Loading chat list..." << std::endl;
|
2019-08-22 17:24:02 +02:00
|
|
|
send_query(td_api::make_object<td_api::getChats>(nullptr, std::numeric_limits<std::int64_t>::max(), 0, 20),
|
2018-12-31 20:04:05 +01:00
|
|
|
[this](Object object) {
|
|
|
|
if (object->get_id() == td_api::error::ID) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto chats = td::move_tl_object_as<td_api::chats>(object);
|
|
|
|
for (auto chat_id : chats->chat_ids_) {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "[id:" << chat_id << "] [title:" << chat_title_[chat_id] << "]" << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
using Object = td_api::object_ptr<td_api::Object>;
|
2020-11-13 17:22:57 +01:00
|
|
|
std::unique_ptr<td::ClientManager> client_manager_;
|
|
|
|
std::int32_t client_id_{0};
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
td_api::object_ptr<td_api::AuthorizationState> authorization_state_;
|
|
|
|
bool are_authorized_{false};
|
|
|
|
bool need_restart_{false};
|
|
|
|
std::uint64_t current_query_id_{0};
|
|
|
|
std::uint64_t authentication_query_id_{0};
|
|
|
|
|
|
|
|
std::map<std::uint64_t, std::function<void(Object)>> handlers_;
|
|
|
|
|
|
|
|
std::map<std::int32_t, td_api::object_ptr<td_api::user>> users_;
|
|
|
|
|
|
|
|
std::map<std::int64_t, std::string> chat_title_;
|
|
|
|
|
|
|
|
void restart() {
|
2020-11-13 17:22:57 +01:00
|
|
|
client_manager_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
*this = TdExample();
|
|
|
|
}
|
|
|
|
|
|
|
|
void send_query(td_api::object_ptr<td_api::Function> f, std::function<void(Object)> handler) {
|
|
|
|
auto query_id = next_query_id();
|
|
|
|
if (handler) {
|
|
|
|
handlers_.emplace(query_id, std::move(handler));
|
|
|
|
}
|
2020-11-13 17:22:57 +01:00
|
|
|
client_manager_->send(client_id_, query_id, std::move(f));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-11-13 17:22:57 +01:00
|
|
|
void process_response(td::ClientManager::Response response) {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!response.object) {
|
|
|
|
return;
|
|
|
|
}
|
2020-11-13 17:22:57 +01:00
|
|
|
//std::cout << response.request_id << " " << to_string(response.object) << std::endl;
|
|
|
|
if (response.request_id == 0) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return process_update(std::move(response.object));
|
|
|
|
}
|
2020-11-13 17:22:57 +01:00
|
|
|
auto it = handlers_.find(response.request_id);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (it != handlers_.end()) {
|
|
|
|
it->second(std::move(response.object));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:40:22 +02:00
|
|
|
std::string get_user_name(std::int32_t user_id) const {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = users_.find(user_id);
|
|
|
|
if (it == users_.end()) {
|
|
|
|
return "unknown user";
|
|
|
|
}
|
|
|
|
return it->second->first_name_ + " " + it->second->last_name_;
|
|
|
|
}
|
|
|
|
|
2020-10-13 18:40:22 +02:00
|
|
|
std::string get_chat_title(std::int64_t chat_id) const {
|
|
|
|
auto it = chat_title_.find(chat_id);
|
|
|
|
if (it == chat_title_.end()) {
|
|
|
|
return "unknown chat";
|
|
|
|
}
|
|
|
|
return it->second;
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void process_update(td_api::object_ptr<td_api::Object> update) {
|
|
|
|
td_api::downcast_call(
|
|
|
|
*update, overloaded(
|
|
|
|
[this](td_api::updateAuthorizationState &update_authorization_state) {
|
|
|
|
authorization_state_ = std::move(update_authorization_state.authorization_state_);
|
|
|
|
on_authorization_state_update();
|
|
|
|
},
|
|
|
|
[this](td_api::updateNewChat &update_new_chat) {
|
|
|
|
chat_title_[update_new_chat.chat_->id_] = update_new_chat.chat_->title_;
|
|
|
|
},
|
|
|
|
[this](td_api::updateChatTitle &update_chat_title) {
|
|
|
|
chat_title_[update_chat_title.chat_id_] = update_chat_title.title_;
|
|
|
|
},
|
|
|
|
[this](td_api::updateUser &update_user) {
|
|
|
|
auto user_id = update_user.user_->id_;
|
|
|
|
users_[user_id] = std::move(update_user.user_);
|
|
|
|
},
|
|
|
|
[this](td_api::updateNewMessage &update_new_message) {
|
|
|
|
auto chat_id = update_new_message.message_->chat_id_;
|
2020-10-13 18:40:22 +02:00
|
|
|
std::string sender_name;
|
|
|
|
td_api::downcast_call(*update_new_message.message_->sender_,
|
|
|
|
overloaded(
|
|
|
|
[this, &sender_name](td_api::messageSenderUser &user) {
|
|
|
|
sender_name = get_user_name(user.user_id_);
|
|
|
|
},
|
|
|
|
[this, &sender_name](td_api::messageSenderChat &chat) {
|
|
|
|
sender_name = get_chat_title(chat.chat_id_);
|
|
|
|
}));
|
2018-12-31 20:04:05 +01:00
|
|
|
std::string text;
|
|
|
|
if (update_new_message.message_->content_->get_id() == td_api::messageText::ID) {
|
2018-01-30 18:06:54 +01:00
|
|
|
text = static_cast<td_api::messageText &>(*update_new_message.message_->content_).text_->text_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-10-13 18:40:22 +02:00
|
|
|
std::cout << "Got message: [chat_id:" << chat_id << "] [from:" << sender_name << "] [" << text
|
|
|
|
<< "]" << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
},
|
|
|
|
[](auto &update) {}));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto create_authentication_query_handler() {
|
|
|
|
return [this, id = authentication_query_id_](Object object) {
|
|
|
|
if (id == authentication_query_id_) {
|
|
|
|
check_authentication_error(std::move(object));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_authorization_state_update() {
|
|
|
|
authentication_query_id_++;
|
|
|
|
td_api::downcast_call(
|
|
|
|
*authorization_state_,
|
|
|
|
overloaded(
|
|
|
|
[this](td_api::authorizationStateReady &) {
|
|
|
|
are_authorized_ = true;
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Got authorization" << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
},
|
|
|
|
[this](td_api::authorizationStateLoggingOut &) {
|
|
|
|
are_authorized_ = false;
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Logging out" << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
},
|
2019-11-13 18:22:25 +01:00
|
|
|
[this](td_api::authorizationStateClosing &) { std::cout << "Closing" << std::endl; },
|
2018-12-31 20:04:05 +01:00
|
|
|
[this](td_api::authorizationStateClosed &) {
|
|
|
|
are_authorized_ = false;
|
|
|
|
need_restart_ = true;
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Terminated" << std::endl;
|
2018-12-31 20:04:05 +01:00
|
|
|
},
|
2019-07-16 21:08:34 +02:00
|
|
|
[this](td_api::authorizationStateWaitCode &) {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Enter authentication code: " << std::flush;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::string code;
|
|
|
|
std::cin >> code;
|
2019-07-16 21:08:34 +02:00
|
|
|
send_query(td_api::make_object<td_api::checkAuthenticationCode>(code),
|
|
|
|
create_authentication_query_handler());
|
|
|
|
},
|
|
|
|
[this](td_api::authorizationStateWaitRegistration &) {
|
|
|
|
std::string first_name;
|
|
|
|
std::string last_name;
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Enter your first name: " << std::flush;
|
2019-07-16 21:08:34 +02:00
|
|
|
std::cin >> first_name;
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Enter your last name: " << std::flush;
|
2019-07-16 21:08:34 +02:00
|
|
|
std::cin >> last_name;
|
|
|
|
send_query(td_api::make_object<td_api::registerUser>(first_name, last_name),
|
2018-12-31 20:04:05 +01:00
|
|
|
create_authentication_query_handler());
|
|
|
|
},
|
|
|
|
[this](td_api::authorizationStateWaitPassword &) {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Enter authentication password: " << std::flush;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::string password;
|
2020-11-04 19:35:38 +01:00
|
|
|
std::getline(std::cin, password);
|
2018-12-31 20:04:05 +01:00
|
|
|
send_query(td_api::make_object<td_api::checkAuthenticationPassword>(password),
|
|
|
|
create_authentication_query_handler());
|
|
|
|
},
|
2019-12-18 17:23:14 +01:00
|
|
|
[this](td_api::authorizationStateWaitOtherDeviceConfirmation &state) {
|
|
|
|
std::cout << "Confirm this login link on another device: " << state.link_ << std::endl;
|
|
|
|
},
|
2018-12-31 20:04:05 +01:00
|
|
|
[this](td_api::authorizationStateWaitPhoneNumber &) {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Enter phone number: " << std::flush;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::string phone_number;
|
|
|
|
std::cin >> phone_number;
|
2019-05-03 04:44:59 +02:00
|
|
|
send_query(td_api::make_object<td_api::setAuthenticationPhoneNumber>(phone_number, nullptr),
|
2018-12-31 20:04:05 +01:00
|
|
|
create_authentication_query_handler());
|
|
|
|
},
|
|
|
|
[this](td_api::authorizationStateWaitEncryptionKey &) {
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Enter encryption key or DESTROY: " << std::flush;
|
2018-12-31 20:04:05 +01:00
|
|
|
std::string key;
|
|
|
|
std::getline(std::cin, key);
|
|
|
|
if (key == "DESTROY") {
|
|
|
|
send_query(td_api::make_object<td_api::destroy>(), create_authentication_query_handler());
|
|
|
|
} else {
|
|
|
|
send_query(td_api::make_object<td_api::checkDatabaseEncryptionKey>(std::move(key)),
|
|
|
|
create_authentication_query_handler());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[this](td_api::authorizationStateWaitTdlibParameters &) {
|
|
|
|
auto parameters = td_api::make_object<td_api::tdlibParameters>();
|
2018-02-26 10:41:12 +01:00
|
|
|
parameters->database_directory_ = "tdlib";
|
2018-12-31 20:04:05 +01:00
|
|
|
parameters->use_message_database_ = true;
|
|
|
|
parameters->use_secret_chats_ = true;
|
|
|
|
parameters->api_id_ = 94575;
|
|
|
|
parameters->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2";
|
|
|
|
parameters->system_language_code_ = "en";
|
|
|
|
parameters->device_model_ = "Desktop";
|
|
|
|
parameters->application_version_ = "1.0";
|
|
|
|
parameters->enable_storage_optimizer_ = true;
|
|
|
|
send_query(td_api::make_object<td_api::setTdlibParameters>(std::move(parameters)),
|
|
|
|
create_authentication_query_handler());
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
void check_authentication_error(Object object) {
|
|
|
|
if (object->get_id() == td_api::error::ID) {
|
|
|
|
auto error = td::move_tl_object_as<td_api::error>(object);
|
2019-11-13 18:22:25 +01:00
|
|
|
std::cout << "Error: " << to_string(error) << std::flush;
|
2018-12-31 20:04:05 +01:00
|
|
|
on_authorization_state_update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uint64_t next_query_id() {
|
|
|
|
return ++current_query_id_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
TdExample example;
|
|
|
|
example.loop();
|
|
|
|
}
|