forked from smartnode/telebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathechobot.c
102 lines (89 loc) · 2.79 KB
/
echobot.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
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include <telebot.h>
#define SIZE_OF_ARRAY(array) (sizeof(array) / sizeof(array[0]))
int main(int argc, char *argv[])
{
printf("Welcome to Echobot\n");
FILE *fp = fopen(".token", "r");
if (fp == NULL)
{
printf("Failed to open .token file\n");
return -1;
}
char token[1024];
if (fscanf(fp, "%s", token) == 0)
{
printf("Failed to read token\n");
fclose(fp);
return -1;
}
printf("Token: %s\n", token);
fclose(fp);
telebot_handler_t handle;
if (telebot_create(&handle, token) != TELEBOT_ERROR_NONE)
{
printf("Telebot create failed\n");
return -1;
}
telebot_user_t me;
if (telebot_get_me(handle, &me) != TELEBOT_ERROR_NONE)
{
printf("Failed to get bot information\n");
telebot_destroy(handle);
return -1;
}
printf("ID: %d\n", me.id);
printf("First Name: %s\n", me.first_name);
printf("User Name: %s\n", me.username);
telebot_put_me(&me);
int index, count, offset = -1;
telebot_error_e ret;
telebot_message_t message;
telebot_update_type_e update_types[] = {TELEBOT_UPDATE_TYPE_MESSAGE};
while (1)
{
telebot_update_t *updates;
ret = telebot_get_updates(handle, offset, 20, 0, update_types, 0, &updates, &count);
if (ret != TELEBOT_ERROR_NONE)
continue;
printf("Number of updates: %d\n", count);
for (index = 0; index < count; index++)
{
message = updates[index].message;
if (message.text)
{
printf("%s: %s \n", message.from->first_name, message.text);
if (strstr(message.text, "/dice"))
{
telebot_send_dice(handle, message.chat->id, false, 0, "");
}
else
{
char str[4096];
if (strstr(message.text, "/start"))
{
snprintf(str, SIZE_OF_ARRAY(str), "Hello %s", message.from->first_name);
}
else
{
snprintf(str, SIZE_OF_ARRAY(str), "<i>%s</i>", message.text);
}
ret = telebot_send_message(handle, message.chat->id, str, "HTML", false, false, updates[index].message.message_id, "");
}
if (ret != TELEBOT_ERROR_NONE)
{
printf("Failed to send message: %d \n", ret);
}
}
offset = updates[index].update_id + 1;
}
telebot_put_updates(updates, count);
sleep(1);
}
telebot_destroy(handle);
return 0;
}