aboutsummaryrefslogtreecommitdiff
path: root/client.cpp
blob: eaa4c002319445a6ddbf65a4253f50ee0edf5ce0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "td/telegram/Client.h"
#include "td/telegram/td_api.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace td::td_api;

class AutoReactClient {
private:
  td::ClientManager manager;
  int client_id;
  bool is_authorized;

  int user_id;
  string emoji;

public:
  AutoReactClient(int user_id, string emoji) {
    td::ClientManager manager;

    this->manager = std::move(manager);
    this->client_id = this->manager.create_client_id();
    this->is_authorized = false;

    this->manager.send(
        client_id, 1,
        td::td_api::make_object<td::td_api::getOption>("version"));

    this->user_id = user_id;
    this->emoji = emoji;
  }

  void authorize() {
    std::cout << "Client authorization" << std::endl;

    while (!this->is_authorized) {
      auto response = this->manager.receive(10);

      if (response.object == NULL || (response.request_id != 0 &&
                                      response.object->get_id() != error::ID)) {
        continue;
      }

      switch (response.object->get_id()) {
      case (updateAuthorizationState::ID):
        on_auth_state(
            move_object_as<updateAuthorizationState>(response.object));
        break;
      case (error::ID):
        on_auth_error(move_object_as<error>(response.object));
        break;
      default:
        continue;
      }
    }

    std::cout << "Client is authorized" << std::endl;
  }

  void loop() {
    while (this->is_authorized) {
      auto response = this->manager.receive(10);

      if (response.object == NULL || (response.request_id != 0 &&
                                      response.object->get_id() != error::ID)) {
        continue;
      }

      feed_update(std::move(response.object));
    }
  }

private:
  void on_auth_error(object_ptr<error> err) {
    std::cout << "Error while authorizing: " << err->message_ << std::endl;
    std::exit(1);
  }

  void on_auth_state(object_ptr<updateAuthorizationState> update) {
    switch (update->authorization_state_->get_id()) {
    case (authorizationStateWaitTdlibParameters::ID):
      set_td_lib_params();
      break;
    case (authorizationStateWaitPhoneNumber::ID):
      input_phone_number();
      break;
    case (authorizationStateWaitCode::ID):
      input_auth_code();
      break;
    case (authorizationStateWaitPassword::ID):
      input_password();
      break;
    case (authorizationStateReady::ID):
      this->is_authorized = true;
      break;
    default:
      std::cout << "Unhandled auth state: "
                << to_string(update->authorization_state_) << std::endl;
      break;
    }
  }

  void set_td_lib_params() {
    auto request = make_object<setTdlibParameters>();
    request->database_directory_ = "tdlib/db";
    request->files_directory_ = "tdlib/files";
    request->api_id_ = 2092395;
    request->api_hash_ = "38e26914cf0fda6356fda8f9d28f3bb9";
    request->system_language_code_ = "en";
    request->device_model_ = "auto reaction client";
    request->system_version_ = "1.0.0";
    request->application_version_ = "1.0.0";
    this->manager.send(this->client_id, 1, std::move(request));
  }

  void input_phone_number() {
    auto request = make_object<setAuthenticationPhoneNumber>();
    std::cout << "Enter the phone number: ";
    std::cin >> request->phone_number_;
    this->manager.send(this->client_id, 1, std::move(request));
  }

  void input_auth_code() {
    auto request = make_object<checkAuthenticationCode>();
    std::cout << "Enter auth code: ";
    std::cin >> request->code_;
    this->manager.send(this->client_id, 1, std::move(request));
  }

  void input_password() {
    auto request = make_object<checkAuthenticationPassword>();
    std::cout << "Enter password: ";
    std::cin >> request->password_;
    this->manager.send(this->client_id, 1, std::move(request));
  }

  void feed_update(object_ptr<Object> object) {
    switch (object->get_id()) {
    case (error::ID):
      on_error(move_object_as<error>(object));
      break;
    case (updateNewMessage::ID):
      on_new_message(move_object_as<updateNewMessage>(object));
      break;
    default:
      break;
    }
  }

  void on_error(object_ptr<error> err) {}

  void on_new_message(object_ptr<updateNewMessage> update) {
    if (update->message_->sender_id_->get_id() == messageSenderUser::ID &&
        move_object_as<messageSenderUser>(update->message_->sender_id_)
                ->user_id_ == this->user_id) {
      auto request = make_object<addMessageReaction>();
      request->chat_id_ = update->message_->chat_id_;
      request->message_id_ = update->message_->id_;
      request->reaction_type_ = make_object<reactionTypeEmoji>(this->emoji);
      request->is_big_ = true;
      this->manager.send(this->client_id, 1, std::move(request));
    }
  }
};