-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelegram.c
238 lines (213 loc) · 6.11 KB
/
telegram.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <unistd.h>
#include <string.h>
#include "stdatomic.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_http_client.h"
#include "esp_log.h"
#include "telegram.h"
#include "private_include/telegram_parser.h"
#include "telegram_api.h"
atomic_uint polling_time = ATOMIC_VAR_INIT(5);
atomic_bool bot_running = ATOMIC_VAR_INIT(false);
static char telegram_bot_token[TELEGRAM_BOT_TOKEN_SIZE] = {0};
static void (*update_callback)(telegram_parsed_msg_t *msg) = NULL;
void telegram_set_update_callback(void (*callback) (telegram_parsed_msg_t *msg))
{
update_callback = callback;
}
void telegram_set_bot_token(char *bot_token)
{
memset(telegram_bot_token, 0x00, sizeof(telegram_bot_token));
strcpy(telegram_bot_token, bot_token);
}
const char *telegram_get_bot_token(void)
{
return telegram_bot_token;
}
static void free_message(telegram_parsed_msg_t *msg)
{
if (msg == NULL)
{
return;
}
if (msg->message != NULL)
{
free(msg->message->text);
free(msg->message->document);
free(msg->message);
}
free(msg->file);
}
int telegram_send_response(const char *link, char **answer)
{
int ret_code = 0;
int content_lenght = -1;
int content_lenght_read = -1;
int status_code = 0;
esp_err_t err = ESP_OK;
ESP_LOGD(__func__, "send response to %s", link);
esp_http_client_config_t config = { .url = link };
esp_http_client_handle_t client = esp_http_client_init(&config);
if (client == NULL)
{
ESP_LOGE(__func__, "Failed on esp_http_client_init()");
goto err_exit;
}
err = esp_http_client_open(client, 0);
if (err != ESP_OK)
{
ESP_LOGE(__func__, "Failed to open HTTP connection: %s", esp_err_to_name(err));
goto err_exit;
}
content_lenght = esp_http_client_fetch_headers(client);
status_code = esp_http_client_get_status_code(client);
ESP_LOGD(__func__, "HTTP Status = %d, content_length = %d", status_code, content_lenght);
if(status_code != 200)
{
ESP_LOGE(__func__, "Bad status code (%d)", status_code);
goto err_exit;
}
if(content_lenght == -1)
{
//TODO
ESP_LOGE(__func__, "Chanked answer TODO");
goto err_exit;
}
*answer = calloc(content_lenght + 1, 1);
if(*answer == NULL)
{
ESP_LOGE(__func__, "%s:%d Bad, bad, bad... malloc returned NULL", __func__, __LINE__);
goto err_exit;
}
content_lenght_read = esp_http_client_read(client, *answer, content_lenght);
if(content_lenght_read != content_lenght)
{
ESP_LOGE(__func__, "%s:%d content_lenght_read (%d) != content_lenght (%d)", __func__, __LINE__, content_lenght_read, content_lenght);
goto err_exit;
}
goto exit;
err_exit:
free(*answer);
*answer = NULL;
ret_code = -1;
exit:
esp_http_client_close(client);
esp_http_client_cleanup(client);
return ret_code;
}
int telegram_get_update(char **answer, const size_t offset)
{
static char telegram_update_url[128] = {0};
snprintf(telegram_update_url, sizeof(telegram_update_url) - 1, "%s/bot%s/%s?limit=1&offset=%u",
TELEGRAM_API_URL,
telegram_bot_token,
TELEGRAM_METHOD_GET_UPDATES,
offset);
return telegram_send_response(telegram_update_url, answer);
}
int telebot_check_updates(bool parse)
{
char *answer = NULL;
int ret = -1, ret_val;
telegram_parsed_msg_t msg = {0};
static size_t offset = 0;
ret_val = telegram_get_update(&answer, offset);
if(ret_val != 0)
{
ESP_LOGE(__func__, "Got error on telegram_send_request");
goto err_exit; //TODO
}
ESP_LOGI(__func__,"%s\n", answer);
if (parse == true)
{
ret_val = parse_updates_message(answer, &msg);
if (ret_val != 0)
{
ESP_LOGE(__func__, "Parsing error");
goto err_exit; //TODO
}
update_callback(&msg);
}
offset = msg.update_id + 1;
err_exit:
free_message(&msg);
free(answer);
answer = NULL;
return ret;
}
int telegram_increase_offset(void)
{
return telebot_check_updates(false);
}
void telegram_bot_loop(void *param)
{
while (atomic_load(&bot_running))
{
telebot_check_updates(true);
sleep(atomic_load(&polling_time));
}
ESP_LOGI(__func__, "Telegram bot stopped");
vTaskDelete(NULL);
}
void telegram_bot_set_polling_time(unsigned int _poling_time)
{
atomic_exchange(&polling_time, _poling_time);
}
void telegram_bot_start(void)
{
bool compare = false;
if (update_callback == NULL)
{
ESP_LOGE(__func__, "update_callback is NULL, telegram bot stopped");
return;
}
if (atomic_compare_exchange_strong(&bot_running, &compare, true) == false)
{
ESP_LOGE(__func__, "Bot is already running");
return;
}
xTaskCreate(telegram_bot_loop,
"telegram_bot_loop",
1024 * 8,
NULL,
1,
NULL);
}
void telegram_bot_stop(void)
{
ESP_LOGI(__func__, "Got bot stop signal");
atomic_exchange(&bot_running, false);
}
int telegram_set_file_url(char *url, size_t url_lenght, char *file_id)
{
int ret_val = SUCCESS;
telegram_parsed_msg_t msg = {0};
static char telegram_update_url[128] = {0};
char *answer = NULL;
snprintf(telegram_update_url, sizeof(telegram_update_url) - 1, "%s/bot%s/%s?file_id=%s",
TELEGRAM_API_URL,
telegram_bot_token,
TELEGRAM_METHOD_GET_FILE,
file_id);
ret_val = telegram_send_response(telegram_update_url, &answer);
if(ret_val != SUCCESS)
{
ESP_LOGE(__func__, "Got error on telegram_send_request");
goto err_exit; //TODO
}
ESP_LOGD(__func__, "answer %s", answer);
ret_val = parse_updates_message(answer, &msg);
if (ret_val != 0)
{
ESP_LOGE(__func__, "Parsing error");
goto err_exit; //TODO
}
snprintf(url, url_lenght, "%s/file/bot%s/%s",
TELEGRAM_API_URL,
telegram_bot_token,
msg.file->file_path);
err_exit:
free_message(&msg);
return ret_val;
}