1
+ #include < ESP8266WiFi.h>
2
+ #include < WiFiClientSecure.h>
3
+ #include < UniversalTelegramBot.h>
4
+
5
+ // Wifi network station credentials
6
+ #define WIFI_SSID " YOUR_SSID"
7
+ #define WIFI_PASSWORD " YOUR_PASSWORD"
8
+ // Telegram BOT Token (Get from Botfather)
9
+ #define BOT_TOKEN " XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
10
+
11
+ const unsigned long BOT_MTBS = 1000 ; // mean time between scan messages
12
+
13
+ X509List cert (TELEGRAM_CERTIFICATE_ROOT);
14
+ WiFiClientSecure secured_client;
15
+ UniversalTelegramBot bot (BOT_TOKEN, secured_client, 2500 );
16
+ unsigned long bot_lasttime;
17
+
18
+ void setup ()
19
+ {
20
+ Serial.begin (115200 );
21
+
22
+ connectToWifi ();
23
+ synchronizeTime ();
24
+ }
25
+
26
+ void loop ()
27
+ {
28
+ if (millis () - bot_lasttime > BOT_MTBS)
29
+ {
30
+ int numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
31
+
32
+ while (numNewMessages)
33
+ {
34
+ Serial.println (" got response" );
35
+ handleNewMessages (numNewMessages);
36
+ numNewMessages = bot.getUpdates (bot.last_message_received + 1 );
37
+ }
38
+
39
+ bot_lasttime = millis ();
40
+ }
41
+ }
42
+
43
+ void handleNewMessages (int numNewMessages)
44
+ {
45
+ for (int i = 0 ; i < numNewMessages; i++)
46
+ {
47
+ telegramMessage message = bot.messages [i];
48
+
49
+ Serial.println (" Received: " + message.text );
50
+
51
+ String text = message.text ;
52
+ String from_name = message.from_name ;
53
+ int message_id = getMessageId (message);
54
+
55
+ if (from_name == " " )
56
+ {
57
+ from_name = " Guest" ;
58
+ }
59
+
60
+ if (text == " /options" )
61
+ {
62
+ // Memory pool for JSON object tree.
63
+ //
64
+ // Inside the brackets, 400 is the size of the pool in bytes.
65
+ // Don't forget to change this value to match your JSON document.
66
+ // Use https://arduinojson.org/assistant to compute the capacity.
67
+ StaticJsonDocument<400 > keyboard;
68
+
69
+ StaticJsonDocument<100 > menuItem0;
70
+ menuItem0[" text" ] = " Some button" ;
71
+ menuItem0[" callback_data" ] = " /custom_action" ;
72
+ keyboard[0 ].add (menuItem0);
73
+
74
+ StaticJsonDocument<100 > menuItem1;
75
+ menuItem1[" text" ] = " Go to Google" ;
76
+ menuItem1[" url" ] = " https://www.google.com" ;
77
+ keyboard[1 ].add (menuItem1);
78
+
79
+ StaticJsonDocument<100 > menuItem2;
80
+ menuItem2[" text" ] = " Close" ;
81
+ menuItem2[" callback_data" ] = " /cancel" ;
82
+ keyboard[2 ].add (menuItem2);
83
+
84
+ bot.sendMessageWithInlineKeyboard (
85
+ message.chat_id ,
86
+ " Choose from one of the following options" ,
87
+ " " ,
88
+ getKeyboardJson (keyboard),
89
+ message_id
90
+ );
91
+ }
92
+ else if (text == " /custom_action" )
93
+ {
94
+ Serial.println (
95
+ message_id != 0 ? " The keyboard will be replaced with test text." : " Only test text will be displayed."
96
+ );
97
+
98
+ bot.sendMessage (message.chat_id , " Lorem Ipsum" , " " , message_id);
99
+ }
100
+ else if (text == " /cancel" )
101
+ {
102
+ Serial.println (
103
+ message_id != 0 ? " Keyboard will be deleted." : " Action will be ignored."
104
+ );
105
+
106
+ bot.deleteMessage (message.chat_id , message_id);
107
+ }
108
+ else if (text == " /start" )
109
+ {
110
+ String welcome = " Welcome to Universal Arduino Telegram Bot library, " + from_name + " .\n " ;
111
+ welcome += " This is example of Inline Keyboard Markup and deletion of the previous message.\n\n " ;
112
+ welcome += " /options : returns the inline keyboard\n " ;
113
+ welcome += " /custom_action : returns test text\n " ;
114
+
115
+ bot.sendMessage (message.chat_id , welcome, " " );
116
+ }
117
+ }
118
+ }
119
+
120
+ void connectToWifi ()
121
+ {
122
+ WiFi.mode (WIFI_STA);
123
+ WiFi.disconnect ();
124
+ delay (100 );
125
+
126
+ Serial.print (" Connecting to Wifi SSID " );
127
+ Serial.print (WIFI_SSID);
128
+ Serial.print (" " );
129
+
130
+ WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
131
+ secured_client.setTrustAnchors (&cert);
132
+
133
+ while (WiFi.status () != WL_CONNECTED)
134
+ {
135
+ Serial.print (" ." );
136
+ delay (1000 );
137
+ }
138
+ Serial.println ();
139
+
140
+ Serial.print (" WiFi connected. IP address: " );
141
+ Serial.println (WiFi.localIP ());
142
+ }
143
+
144
+ void synchronizeTime ()
145
+ {
146
+ configTime (0 , 0 , " pool.ntp.org" );
147
+
148
+ Serial.print (" Time synchronization " );
149
+
150
+ time_t now = time (nullptr );
151
+ while (now < 24 * 3600 )
152
+ {
153
+ Serial.print (" ." );
154
+ delay (100 );
155
+ now = time (nullptr );
156
+ }
157
+ Serial.println ();
158
+
159
+ Serial.print (" Current time (unixtime): " );
160
+ Serial.println (now);
161
+ }
162
+
163
+ int getMessageId (telegramMessage message)
164
+ {
165
+ if (message.type == " callback_query" )
166
+ {
167
+ Serial.println (" callback_query detected. The message_id will be passed." );
168
+ }
169
+
170
+ return message.type == " callback_query" ? message.message_id : 0 ;
171
+ }
172
+
173
+ String getKeyboardJson (JsonDocument& keyboard)
174
+ {
175
+ String keyboardJson = " " ;
176
+ serializeJson (keyboard, keyboardJson);
177
+ return keyboardJson;
178
+ }
0 commit comments