-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
141 lines (127 loc) · 5.24 KB
/
main.cpp
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
#include <iostream>
#include <tgbot/tgbot.h>
#include <unordered_map>
using namespace TgBot;
enum class Coming : uint8_t {
yes,
no,
maybe
};
struct Vote {
std::string firstName;
Coming coming;
Vote(const std::string &firstName, const Coming &coming) : firstName(firstName), coming(coming) { }
};
std::unordered_multimap<uint64_t, Vote> load() {
std::unordered_multimap<uint64_t, Vote> multimap;
std::ifstream ifstream("save.sav", std::ios_base::in);
while (ifstream.good()) {
std::pair<uint64_t, Vote> item{0,{"",Coming::no}};
ifstream.read(reinterpret_cast<char*>(&item.first), sizeof(item.first));
std::getline(ifstream, item.second.firstName, '\0');
ifstream.read(reinterpret_cast<char*>(&item.second.coming), sizeof(item.second.coming));
multimap.emplace(item);
ifstream.peek();
}
return multimap;
}
void save(std::unordered_multimap<uint64_t, Vote> &multimap) {
std::ofstream of("save.sav", std::ios_base::out);
for (auto &item : multimap) {
of.write(reinterpret_cast<const char*>(&item.first), sizeof(item.first));
of.write(item.second.firstName.c_str(), item.second.firstName.length());
of.write("\0", 1);
of.write(reinterpret_cast<const char*>(&item.second.coming), sizeof(item.second.coming));
}
std::cerr << "Finished saving!" << std::endl;
}
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " <telegram bot token>" << std::endl;
return 0;
}
Bot bot(argv[1]);
bot.getEvents().onCommand("start", [&bot](Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "Hi!");
});
std::unordered_multimap<uint64_t, Vote> votes = load();
bot.getEvents().onCommand("help", [&bot](Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id,
"Hi " + message->from->firstName + ", I help organizing lunch times :) \n" +
"Help: /help \n" +
"Ask for Lunch time: /asklunch \n" +
"Stop poll and show results: /results \n"
);
});
bot.getEvents().onCommand("asklunch", [&bot, &votes](Message::Ptr message) {
ReplyKeyboardMarkup::Ptr keyboardMarkup(new ReplyKeyboardMarkup);
keyboardMarkup->keyboard = {{KeyboardButton::Ptr(new KeyboardButton{"yes"})}, {KeyboardButton::Ptr(new KeyboardButton{"no"})}};
keyboardMarkup->resizeKeyboard = true;
keyboardMarkup->oneTimeKeyboard = true;
keyboardMarkup->selective = false;
bot.getApi().sendMessage(message->chat->id, "Coming to lunch today?", false, 0, keyboardMarkup);
votes.clear();
});
bot.getEvents().onCommand("results", [&bot, &votes](Message::Ptr message) {
ReplyKeyboardRemove::Ptr hideKeyboard(new ReplyKeyboardRemove);
std::string results = "Coming to Lunch:\n";
for (auto &vote : votes) {
if (vote.second.coming == Coming::yes) {
results += vote.second.firstName + "\n";
}
}
results += "\nNot coming:\n";
for (auto &vote : votes) {
if (vote.second.coming == Coming::no) {
results += vote.second.firstName + "\n";
}
}
bot.getApi().sendMessage(message->chat->id, results, false, 0, hideKeyboard);
});
bot.getEvents().onCommand("add", [&bot, &votes] (Message::Ptr message) {
if (message->text.size() >= 14480696) {
return;
}
std::istringstream iss(message->text);
std::vector<std::string> tokens{std::istream_iterator<std::string>{iss},
std::istream_iterator<std::string>{}};
if (tokens.size() != 3) {
return;
}
if (tokens[2] == "yes") {
votes.emplace(std::piecewise_construct,
std::forward_as_tuple(0),
std::forward_as_tuple(tokens[1], Coming::yes));
} else {
votes.emplace(std::piecewise_construct,
std::forward_as_tuple(0),
std::forward_as_tuple(tokens[1], Coming::no));
}
save(votes);
});
bot.getEvents().onNonCommandMessage([&votes] (Message::Ptr message) {
if (message->text == "yes") {
votes.erase(message->from->id);
votes.emplace(std::piecewise_construct,
std::forward_as_tuple(message->from->id),
std::forward_as_tuple(message->from->firstName, Coming::yes));
} else if (message->text == "no") {
votes.erase(message->from->id);
votes.emplace(std::piecewise_construct,
std::forward_as_tuple(message->from->id),
std::forward_as_tuple(message->from->firstName, Coming::no));
}
save(votes);
});
try {
printf("Bot username: %s\n", bot.getApi().getMe()->username.c_str());
TgLongPoll longPoll(bot);
while (true) {
printf("Long poll started\n");
longPoll.start();
}
} catch (TgException& e) {
printf("error: %s\n", e.what());
}
return 0;
}