-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpipe_db_api.cpp
More file actions
57 lines (42 loc) · 1.53 KB
/
pipe_db_api.cpp
File metadata and controls
57 lines (42 loc) · 1.53 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
//
// Created by anton on 8/14/19.
//
#include "pipe_db_api.h"
using namespace nlohmann;
void process_update() {
std::string result;
get_updates(&result);
auto j_upd = json::parse(result);
j_upd = j_upd["result"];
int last_update = get_last_update();
int update_id = -1;
for (auto el : j_upd) {
update_id = el["update_id"].get<int>();
// skip old updates
if (last_update >= update_id) {
continue;
}
std::string text = el["message"]["text"];
// 1 beacase the el[0] == '/'
if (text.find("subscribe") == 1) {
json from = el["message"]["from"];
long long int chid = el["message"]["chat"]["id"].get<long long int>();
user new_user(from["username"].get<std::string>(), from["id"].get<unsigned long int>(), chid, true);
unsigned long int found_user = select_user(new_user);
//adding new user
if (found_user == 0) {
subscribe_user(new_user);
} else {
update_subscribe(new_user, true);
}
} else if (text.find("unsubscribe") == 1) {
json from = el["message"]["from"];
long long int chid = el["message"]["chat"]["id"].get<long long int>();
user new_user(from["username"].get<std::string>(), from["id"].get<unsigned long int>(), chid, true);
update_subscribe(new_user, false);
}
}
if (update_id != -1) {
update_last_update(update_id, last_update);
}
}