-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
191 lines (174 loc) · 7.28 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <string>
#include <stdio.h>
#include <tgbot/tgbot.h>
#include <curl/curl.h>
#include "json.hpp"
using namespace std;
using json = nlohmann::json; // docs: https://github.com/nlohmann/json
string start_message = "Hello, welcome to the <strong>nm80 dictionary bot</strong>.\nSend a word.";
string lowercase(string text){
for(auto & c : text){
if(c >= 65 && c <= 90){
c += 32;
}
}
return text;
}
// parse merriam-webster text formatting (https://dictionaryapi.com/products/json#sec-2.fmttokens)
string formatter(string text){
string first, second;
size_t pos;
first = "{bc}";
second = "<strong>: </strong>";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{it}";
second = "<i>";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{/it}";
second = "</i>";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{b}";
second = "<strong>";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{/b}";
second = "</strong>";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{phrase}";
second = "<strong>";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{/phrase}";
second = "</strong>";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{ldquo}";
second = "\"";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
first = "{rdquo}";
second = "\"";
while(string::npos != (pos = text.find(first))){
text.replace(pos, first.size(), second);
}
return text;
}
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp){
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
string learner_webster(string search){
try{
CURL * curl;
// CURLcode res;
search = lowercase(search);
std::string readBuffer;
std::string key = getenv("LEARNER_WEBSTER_API");
std::string link = "https://www.dictionaryapi.com/api/v3/references/learners/json/" + search + "?key=" + key;
curl = curl_easy_init();
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, link.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
// res = curl_easy_perform(curl);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
ofstream dic_log("dic_log", ios::app);
try{
json j = json::parse(readBuffer);
stringstream out;
dic_log << "-------------------------------------------------\n";
for(unsigned entry = 0; entry < j.size(); entry++){
try{
if(!j[entry]["meta"]["app-shortdef"].empty()){
string entry_title = j[entry]["meta"]["app-shortdef"]["hw"].get<string>();
if(string::npos != lowercase(entry_title).find(search)){ // check for exact related ones
entry_title = entry_title.substr(0, entry_title.find(":"));
string functional_label;
if (!j[entry]["meta"]["app-shortdef"]["fl"].empty()){
functional_label = " (" + j[entry]["meta"]["app-shortdef"]["fl"].get<string>() + ")";
}
dic_log << entry_title << endl;
out << "<strong>" << formatter(entry_title)
<< functional_label
<< ": \n" << "</strong>";
for(unsigned def = 0; def < j[entry]["meta"]["app-shortdef"]["def"].size(); def++){
string app_def_title;
if("" == j[entry]["meta"]["app-shortdef"]["def"][def]){
out << "<strong> : </strong>" << formatter(j[entry]["shortdef"][def].get<string>()) << "\n";
break;
}
app_def_title = j[entry]["meta"]["app-shortdef"]["def"][def].get<string>();
out << formatter(app_def_title);
dic_log << "\tdebug:\t" << entry << "\t" << app_def_title << endl;
if(!j[entry]["shortdef"][0].empty()){
string def_title = j[entry]["shortdef"][0].get<string>();
if(string("{/it}") == app_def_title.substr(app_def_title.size() - 5)){
out << "<strong> : </strong>" << formatter(def_title);
}
}
out << "\n";
}
}
}
}catch(...){
dic_log << "\nproblem happened here" << endl;
}
}
dic_log.close();
if(out.str().empty()){
return "Exact result not found :(\nTry to search different";
}else{
return out.str();
}
}catch(...){
return "Not found :(";
}
}catch(...){
return "Sorry, Bot couldn't work correctly for this word.\nWe'll work to fix it as soon as possible";
}
}
int main() {
ofstream tel_log("tel_log", ios::app);
std::string BOT_API = getenv("NM80_DICTIONARY_BOT_API");
TgBot::Bot bot(BOT_API);
bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, start_message,false,0,std::make_shared<TgBot::GenericReply>(),"html", false);
});
bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
// printf("User wrote %s\n", message->text.c_str());
if (StringTools::startsWith(message->text, "/start")) {
return;
}
// bypass seeing messages from log channel:
std::string log_channel = getenv("LOG_CHANNEL_ID");
if (std::to_string(message->chat->id) == log_channel){
return;
}
bot.getApi().sendMessage(message->chat->id, "<strong>Meanings of \"" + message->text + "\" from Merriam Webster Learner's Dictionary: \n\n</strong>" + learner_webster(message->text),false,0,std::make_shared<TgBot::GenericReply>(),"html", false);
});
try {
tel_log << "bot name: " << bot.getApi().getMe()->firstName.c_str() << endl;
// printf("Bot name: %s\n", bot.getApi().getMe()->firstName.c_str());
TgBot::TgLongPoll longPoll(bot);
while (true) {
tel_log << "Long poll started\n";
longPoll.start();
}
} catch (TgBot::TgException& e) {
tel_log << "error: " << e.what();
}
}