-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
156 lines (122 loc) · 4.76 KB
/
main.cpp
File metadata and controls
156 lines (122 loc) · 4.76 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
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
#include <fstream>
#include <nlohmann/json.hpp>
#include <tgbot/tgbot.h>
#include <curl/curl.h>
#include <iostream>
#include <sstream>
#include <iomanip>
class Config {
public:
// Метод для получения единственного экземпляра класса
static Config& getInstance() {
static Config instance;
return instance;
}
// Метод для получения значения по ключу
template <typename T>
T get(const std::string& key) {
if(config.contains(key)) {
return config[key].get<T>();
}
else {
throw std::runtime_error("Key not found: " + key);
}
}
private:
nlohmann::json config;
// Приватный конструктор
Config() {
std::ifstream file("config.json");
if (file.is_open()) {
file >> config;
file.close();
} else {
std::cerr << "Не удалось открыть файл config.json" << std::endl;
}
}
// Приватный деструктор
~Config() {}
// Запрещаем копирование
Config(const Config&) = delete;
Config& operator=(const Config&) = delete;
};
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
std::string getWeather(double latitude, double longitude) {
Config& config = Config::getInstance();
std::string weather_api_key = config.get<std::string>("weather_api_key");
std::string url = "http://api.weatherapi.com/v1/current.json?key=" + weather_api_key + "&q=" + std::to_string(latitude) + "," + std::to_string(longitude);
CURL* curl;
CURLcode res;
std::string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
if (res != CURLE_OK) {
return "Failed to get weather data.";
}
nlohmann::json jsonData;
try {
jsonData = nlohmann::json::parse(readBuffer);
std::string location = jsonData["location"]["name"];
double temp = jsonData["current"]["temp_c"].get<double>();
std::stringstream stream;
stream << std::fixed << std::setprecision(1) << temp;
std::string temperature = stream.str();
std::string condition = jsonData["current"]["condition"]["text"];
return "Location: " + location + "\nTemperature: " + temperature + "°C\nCondition: " + condition;
} catch (nlohmann::json::exception& e) {
std::cerr << "JSON parse error: " << e.what() << std::endl;
return "Failed to parse weather data.";
}
}
int main() {
Config& config = Config::getInstance();
std::string tg_api_key = config.get<std::string>("tg_api_key_wthr");
TgBot::Bot bot(tg_api_key);
bot.getEvents().onCommand("start", [&bot](TgBot::Message::Ptr message) {
TgBot::ReplyKeyboardMarkup::Ptr keyboardMarkup(new TgBot::ReplyKeyboardMarkup);
TgBot::KeyboardButton::Ptr button(new TgBot::KeyboardButton);
button->text = "Send Location 📍";
button->requestLocation = true;
std::vector<TgBot::KeyboardButton::Ptr> row;
row.push_back(button);
keyboardMarkup->keyboard.push_back(row);
keyboardMarkup->oneTimeKeyboard = true;
keyboardMarkup->resizeKeyboard = true;
bot.getApi().sendMessage(message->chat->id, "Hello! I am your weather bot. Please share your location ⬇", nullptr, nullptr, keyboardMarkup, "", false);
});
bot.getEvents().onCommand("bye", [&bot](TgBot::Message::Ptr message) {
bot.getApi().sendMessage(message->chat->id, "Bye, bye! Wish to see you again.");
});
bot.getEvents().onAnyMessage([&bot](TgBot::Message::Ptr message) {
if (message->location) {
double latitude = message->location->latitude;
double longitude = message->location->longitude;
std::string weatherInfo = getWeather(latitude, longitude);
bot.getApi().sendMessage(message->chat->id, weatherInfo);
return;
}
else if (StringTools::startsWith(message->text, "/start") || StringTools::startsWith(message->text, "/bye")) {
return;
}
});
try {
std::cout << "Bot is running..." << std::endl;
TgBot::TgLongPoll longPoll(bot);
while (true) {
longPoll.start();
}
} catch (std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
return 0;
}