Repository navigation
Expand file tree
/
Copy pathgroup_call_events.cpp
More file actions
62 lines (56 loc) · 2.15 KB
/
Copy pathgroup_call_events.cpp
File metadata and controls
62 lines (56 loc) · 2.15 KB
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
// Prints group-call events until interrupted. Duplicate and stale versions are removed by the Client.
//
// Environment: TG_API_ID, TG_API_HASH, TG_SESSION
// Usage: group_call_events
#include <cstdlib>
#include <exception>
#include <iostream>
#include <stdexcept>
#include "cppgram/client.h"
namespace {
void print_event(cppgram::Client&, const cppgram::GroupCallEvent& e) {
switch (e.kind) {
case cppgram::GroupCallEventKind::CallUpdated:
std::cout << "call " << e.call.id << " v" << e.version;
if (e.info)
std::cout << " title='" << e.info->title << "' participants=" << e.info->participants_count;
std::cout << "\n";
break;
case cppgram::GroupCallEventKind::CallDiscarded:
std::cout << "call " << e.call.id << " ended\n";
break;
case cppgram::GroupCallEventKind::ParticipantsUpdated:
std::cout << "call " << e.call.id << " v" << e.version << " participants changed:";
for (const auto& p : e.participants)
std::cout << " " << p.peer.id << (p.left ? "(left)" : "");
std::cout << "\n";
break;
case cppgram::GroupCallEventKind::ConnectionUpdated:
std::cout << "connection parameters received (" << (e.connection_params ? e.connection_params->size() : 0)
<< " bytes)\n";
break;
}
}
} // namespace
int main() {
try {
const char* id = std::getenv("TG_API_ID");
const char* hash = std::getenv("TG_API_HASH");
const char* session = std::getenv("TG_SESSION");
if (id == nullptr || hash == nullptr || session == nullptr)
throw std::runtime_error("set TG_API_ID, TG_API_HASH and TG_SESSION");
cppgram::ClientOptions options;
options.api_id = std::stoi(id);
options.api_hash = hash;
options.session_path = session;
cppgram::Client client(options);
client.on_group_call_event(print_event);
client.connect();
std::cout << "listening for group call events; Ctrl+C to stop\n";
client.run();
} catch (const std::exception& e) {
std::cerr << "error: " << e.what() << "\n";
return 1;
}
return 0;
}