diff options
| -rw-r--r-- | .clangd | 2 | ||||
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | .gitmodules | 3 | ||||
| -rw-r--r-- | CMakeLists.txt | 7 | ||||
| -rw-r--r-- | README.md | 19 | ||||
| -rw-r--r-- | client.cpp | 163 | ||||
| -rw-r--r-- | main.cpp | 21 | ||||
| m--------- | td | 0 |
8 files changed, 217 insertions, 0 deletions
| @@ -0,0 +1,2 @@ | |||
| 1 | CompileFlags: | ||
| 2 | Remove: [-flto-odr-type-merging] | ||
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..969e7ee --- /dev/null +++ b/.gitignore | |||
| @@ -0,0 +1,2 @@ | |||
| 1 | .cache | ||
| 2 | build/ | ||
diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..f1273f5 --- /dev/null +++ b/.gitmodules | |||
| @@ -0,0 +1,3 @@ | |||
| 1 | [submodule "td"] | ||
| 2 | path = td | ||
| 3 | url = https://github.com/tdlib/td.git | ||
diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e09b51f --- /dev/null +++ b/CMakeLists.txt | |||
| @@ -0,0 +1,7 @@ | |||
| 1 | cmake_minimum_required(VERSION 4.1) | ||
| 2 | |||
| 3 | project(AutoTelegramReaction) | ||
| 4 | add_executable(AutoTelegramReaction main.cpp) | ||
| 5 | |||
| 6 | add_subdirectory(td) | ||
| 7 | target_link_libraries(AutoTelegramReaction PRIVATE Td::TdStatic) | ||
diff --git a/README.md b/README.md new file mode 100644 index 0000000..77f6b2d --- /dev/null +++ b/README.md | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | # Auto telegram reaction | ||
| 2 | |||
| 3 | Automatically sets reactions for the user | ||
| 4 | |||
| 5 | ## Build | ||
| 6 | |||
| 7 | ```sh | ||
| 8 | mkdir build && cd build | ||
| 9 | ``` | ||
| 10 | |||
| 11 | ```sh | ||
| 12 | cmake DCMAKE_BUILD_TYPE=Release .. && cmake --build . | ||
| 13 | ``` | ||
| 14 | |||
| 15 | ## Usage | ||
| 16 | |||
| 17 | ```sh | ||
| 18 | ./AutoTelegramReaction <user_id> <reaction_emoji> | ||
| 19 | ``` | ||
diff --git a/client.cpp b/client.cpp new file mode 100644 index 0000000..a64973e --- /dev/null +++ b/client.cpp | |||
| @@ -0,0 +1,163 @@ | |||
| 1 | #include "td/telegram/Client.h" | ||
| 2 | #include "td/telegram/td_api.h" | ||
| 3 | #include <cstdio> | ||
| 4 | #include <cstdlib> | ||
| 5 | #include <iostream> | ||
| 6 | |||
| 7 | using namespace td::td_api; | ||
| 8 | |||
| 9 | class AutoReactClient { | ||
| 10 | private: | ||
| 11 | td::ClientManager manager; | ||
| 12 | int client_id; | ||
| 13 | bool is_authorized; | ||
| 14 | |||
| 15 | int user_id; | ||
| 16 | string emoji; | ||
| 17 | |||
| 18 | public: | ||
| 19 | AutoReactClient(int user_id, string emoji) { | ||
| 20 | td::ClientManager manager; | ||
| 21 | |||
| 22 | this->manager = std::move(manager); | ||
| 23 | this->client_id = this->manager.create_client_id(); | ||
| 24 | this->is_authorized = false; | ||
| 25 | |||
| 26 | this->manager.send( | ||
| 27 | client_id, 1, | ||
| 28 | td::td_api::make_object<td::td_api::getOption>("version")); | ||
| 29 | |||
| 30 | this->user_id = user_id; | ||
| 31 | this->emoji = emoji; | ||
| 32 | } | ||
| 33 | |||
| 34 | void authorize() { | ||
| 35 | std::cout << "Client authorization" << std::endl; | ||
| 36 | |||
| 37 | while (!this->is_authorized) { | ||
| 38 | auto response = this->manager.receive(10); | ||
| 39 | |||
| 40 | if (response.object == NULL || (response.request_id != 0 && | ||
| 41 | response.object->get_id() != error::ID)) { | ||
| 42 | continue; | ||
| 43 | } | ||
| 44 | |||
| 45 | switch (response.object->get_id()) { | ||
| 46 | case (updateAuthorizationState::ID): | ||
| 47 | on_auth_state( | ||
| 48 | move_object_as<updateAuthorizationState>(response.object)); | ||
| 49 | break; | ||
| 50 | case (error::ID): | ||
| 51 | on_auth_error(move_object_as<error>(response.object)); | ||
| 52 | break; | ||
| 53 | default: | ||
| 54 | continue; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | void loop() { | ||
| 60 | while (this->is_authorized) { | ||
| 61 | auto response = this->manager.receive(10); | ||
| 62 | |||
| 63 | if (response.object == NULL || (response.request_id != 0 && | ||
| 64 | response.object->get_id() != error::ID)) { | ||
| 65 | continue; | ||
| 66 | } | ||
| 67 | |||
| 68 | feed_update(std::move(response.object)); | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | private: | ||
| 73 | void on_auth_error(object_ptr<error> err) { | ||
| 74 | std::cout << "Error while authorizing: " << err->message_ << std::endl; | ||
| 75 | std::exit(1); | ||
| 76 | } | ||
| 77 | |||
| 78 | void on_auth_state(object_ptr<updateAuthorizationState> update) { | ||
| 79 | switch (update->authorization_state_->get_id()) { | ||
| 80 | case (authorizationStateWaitTdlibParameters::ID): | ||
| 81 | set_td_lib_params(); | ||
| 82 | break; | ||
| 83 | case (authorizationStateWaitPhoneNumber::ID): | ||
| 84 | input_phone_number(); | ||
| 85 | break; | ||
| 86 | case (authorizationStateWaitCode::ID): | ||
| 87 | input_auth_code(); | ||
| 88 | break; | ||
| 89 | case (authorizationStateWaitPassword::ID): | ||
| 90 | input_password(); | ||
| 91 | break; | ||
| 92 | case (authorizationStateReady::ID): | ||
| 93 | this->is_authorized = true; | ||
| 94 | break; | ||
| 95 | default: | ||
| 96 | std::cout << "Unhandled auth state: " | ||
| 97 | << to_string(update->authorization_state_) << std::endl; | ||
| 98 | break; | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | void set_td_lib_params() { | ||
| 103 | auto request = make_object<setTdlibParameters>(); | ||
| 104 | request->database_directory_ = "tdlib/db"; | ||
| 105 | request->files_directory_ = "tdlib/files"; | ||
| 106 | request->api_id_ = 2092395; | ||
| 107 | request->api_hash_ = "38e26914cf0fda6356fda8f9d28f3bb9"; | ||
| 108 | request->system_language_code_ = "en"; | ||
| 109 | request->device_model_ = "auto reaction client"; | ||
| 110 | request->system_version_ = "1.0.0"; | ||
| 111 | request->application_version_ = "1.0.0"; | ||
| 112 | this->manager.send(this->client_id, 1, std::move(request)); | ||
| 113 | } | ||
| 114 | |||
| 115 | void input_phone_number() { | ||
| 116 | auto request = make_object<setAuthenticationPhoneNumber>(); | ||
| 117 | std::cout << "Enter the phone number: "; | ||
| 118 | std::cin >> request->phone_number_; | ||
| 119 | this->manager.send(this->client_id, 1, std::move(request)); | ||
| 120 | } | ||
| 121 | |||
| 122 | void input_auth_code() { | ||
| 123 | auto request = make_object<checkAuthenticationCode>(); | ||
| 124 | std::cout << "Enter auth code: "; | ||
| 125 | std::cin >> request->code_; | ||
| 126 | this->manager.send(this->client_id, 1, std::move(request)); | ||
| 127 | } | ||
| 128 | |||
| 129 | void input_password() { | ||
| 130 | auto request = make_object<checkAuthenticationPassword>(); | ||
| 131 | std::cout << "Enter password: "; | ||
| 132 | std::cin >> request->password_; | ||
| 133 | this->manager.send(this->client_id, 1, std::move(request)); | ||
| 134 | } | ||
| 135 | |||
| 136 | void feed_update(object_ptr<Object> object) { | ||
| 137 | switch (object->get_id()) { | ||
| 138 | case (error::ID): | ||
| 139 | on_error(move_object_as<error>(object)); | ||
| 140 | break; | ||
| 141 | case (updateNewMessage::ID): | ||
| 142 | on_new_message(move_object_as<updateNewMessage>(object)); | ||
| 143 | break; | ||
| 144 | default: | ||
| 145 | break; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | void on_error(object_ptr<error> err) {} | ||
| 150 | |||
| 151 | void on_new_message(object_ptr<updateNewMessage> update) { | ||
| 152 | if (update->message_->sender_id_->get_id() == messageSenderUser::ID && | ||
| 153 | move_object_as<messageSenderUser>(update->message_->sender_id_) | ||
| 154 | ->user_id_ == this->user_id) { | ||
| 155 | auto request = make_object<addMessageReaction>(); | ||
| 156 | request->chat_id_ = update->message_->chat_id_; | ||
| 157 | request->message_id_ = update->message_->id_; | ||
| 158 | request->reaction_type_ = make_object<reactionTypeEmoji>(this->emoji); | ||
| 159 | request->is_big_ = true; | ||
| 160 | this->manager.send(this->client_id, 1, std::move(request)); | ||
| 161 | } | ||
| 162 | } | ||
| 163 | }; | ||
diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..f70e6c0 --- /dev/null +++ b/main.cpp | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | #include "client.cpp" | ||
| 2 | #include <cstdlib> | ||
| 3 | #include <iostream> | ||
| 4 | #include <ostream> | ||
| 5 | |||
| 6 | int main(int argc, char *argv[]) { | ||
| 7 | td::ClientManager::execute( | ||
| 8 | td::td_api::make_object<td::td_api::setLogVerbosityLevel>(0)); | ||
| 9 | |||
| 10 | if (argc != 3) { | ||
| 11 | std::cout << "Must be exactly 2 arguments (user_id and reaction_emoji)" | ||
| 12 | << std::endl; | ||
| 13 | return 1; | ||
| 14 | } | ||
| 15 | |||
| 16 | AutoReactClient client(std::atoi(argv[1]), argv[2]); | ||
| 17 | client.authorize(); | ||
| 18 | client.loop(); | ||
| 19 | |||
| 20 | return 0; | ||
| 21 | } | ||
diff --git a/td b/td new file mode 160000 | |||
| Subproject 369ee922b45bfa7e8da357e4d62e93925862d86 | |||
