Skip to content

Commit 762d6e8

Browse files
committed
added a working example for update (toggle a LED)
1 parent 726c112 commit 762d6e8

File tree

2 files changed

+196
-0
lines changed

2 files changed

+196
-0
lines changed
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
/*******************************************************************
2+
An example of how to use message_id to update an inline keyboard
3+
message and buttons to toggle a LED
4+
5+
written by Frits Jan van Kempen
6+
*******************************************************************/
7+
#include <Arduino.h>
8+
#include <WiFi.h>
9+
#include <WiFiClientSecure.h>
10+
#include <UniversalTelegramBot.h>
11+
12+
// Initialize Wifi connection to the router
13+
const char *ssid = "mySSID";
14+
const char *password = "myPASSWORD";
15+
16+
// Initialize Telegram BOT
17+
#define BOTtoken "xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxxxxxx" // your Bot Token (Get from Botfather)
18+
WiFiClientSecure client;
19+
UniversalTelegramBot bot(BOTtoken2, client);
20+
21+
int Bot_mtbs = 1000; //mean time between scan messages
22+
long Bot_lasttime; //last time messages' scan has been done
23+
int last_message_id = 0;
24+
25+
// LED parameters
26+
const int ledPin = 2; // Internal LED on DevKit ESP32-WROOM (GPIO2)
27+
int ledState = LOW;
28+
29+
void handleNewMessages(int numNewMessages)
30+
{
31+
32+
for (int i = 0; i < numNewMessages; i++)
33+
{
34+
35+
// Get all the important data from the message
36+
int message_id = bot.messages[i].message_id;
37+
String chat_id = String(bot.messages[i].chat_id);
38+
String text = bot.messages[i].text;
39+
String from_name = bot.messages[i].from_name;
40+
if (from_name == "")
41+
from_name = "Guest";
42+
String msg = ""; // init a message string to use
43+
44+
// Output the message_id to give you feeling on how this example works
45+
Serial.print("Message id: ");
46+
Serial.println(message_id);
47+
48+
// Inline buttons with callbacks when pressed will raise a callback_query message
49+
if (bot.messages[i].type == "callback_query")
50+
{
51+
Serial.print("Call back button pressed by: ");
52+
Serial.println(bot.messages[i].from_id);
53+
Serial.print("Data on the button: ");
54+
Serial.println(bot.messages[i].text);
55+
56+
if (text == "/toggleLED")
57+
{
58+
59+
// Toggle the ledState and update the LED itself
60+
ledState = !ledState;
61+
digitalWrite(ledPin, ledState);
62+
63+
// Now we can UPDATE the message, lets prepare it for sending:
64+
msg = "Hi " + from_name + "!\n";
65+
msg += "Notice how the LED state has changed!\n\n";
66+
msg += "Try it again, see the button has updated as well:\n\n";
67+
68+
// Prepare the buttons
69+
String keyboardJson = "["; // start Json
70+
keyboardJson += "[{ \"text\" : \"The LED is ";
71+
if (ledState)
72+
{
73+
keyboardJson += "ON";
74+
}
75+
else
76+
{
77+
keyboardJson += "OFF";
78+
}
79+
keyboardJson += "\", \"callback_data\" : \"/toggleLED\" }]";
80+
keyboardJson += ", [{ \"text\" : \"Send text\", \"callback_data\" : \"This text was sent by inline button\" }]"; // add another button
81+
//keyboardJson += ", [{ \"text\" : \"Go to Google\", \"url\" : \"https://www.google.com\" }]"; // add another button, this one appears after first Update
82+
keyboardJson += "]"; // end Json
83+
84+
// Now send this message including the current message_id as the 5th input to UPDATE that message
85+
bot.sendMessageWithInlineKeyboard(chat_id, msg, "Markdown", keyboardJson, message_id);
86+
}
87+
88+
else
89+
{
90+
// echo back callback_query which is not handled above
91+
bot.sendMessage(chat_id, text, "Markdown");
92+
}
93+
}
94+
95+
// 'Normal' messages are handled here
96+
else
97+
{
98+
if (text == "/start")
99+
{
100+
// lets create a friendly welcome message
101+
msg = "Hi " + from_name + "!\n";
102+
msg += "I am your Telegram Bot running on ESP32.\n\n";
103+
msg += "Lets test this updating LED button below:\n\n";
104+
105+
// lets create a button depending on the current ledState
106+
String keyboardJson = "["; // start of keyboard json
107+
keyboardJson += "[{ \"text\" : \"The LED is ";
108+
if (ledState)
109+
{
110+
keyboardJson += "ON";
111+
}
112+
else
113+
{
114+
keyboardJson += "OFF";
115+
}
116+
keyboardJson += "\", \"callback_data\" : \"/toggleLED\" }]"; //callback is /toggleLED
117+
keyboardJson += ", [{ \"text\" : \"Send text\", \"callback_data\" : \"This text was sent by inline button\" }]"; // add another button
118+
keyboardJson += "]"; // end of keyboard json
119+
120+
//first time, send this message as a normal inline keyboard message:
121+
bot.sendMessageWithInlineKeyboard(chat_id, msg, "Markdown", keyboardJson);
122+
}
123+
if (text == "/options")
124+
{
125+
String keyboardJson = "[[{ \"text\" : \"Go to Google\", \"url\" : \"https://www.google.com\" }], [{ \"text\" : \"Send\", \"callback_data\" : \"This was sent by inline\" }]]";
126+
bot.sendMessageWithInlineKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson);
127+
}
128+
}
129+
}
130+
}
131+
132+
void setup()
133+
{
134+
Serial.begin(115200);
135+
136+
// Attempt to connect to Wifi network:
137+
Serial.print("Connecting Wifi: ");
138+
Serial.println(ssid);
139+
140+
// Set WiFi to station mode and disconnect from an AP if it was Previously
141+
// connected
142+
WiFi.mode(WIFI_STA);
143+
WiFi.begin(ssid, password);
144+
145+
while (WiFi.status() != WL_CONNECTED)
146+
{
147+
Serial.print(".");
148+
delay(500);
149+
}
150+
151+
Serial.println("");
152+
Serial.println("WiFi connected");
153+
Serial.print("IP address: ");
154+
Serial.println(WiFi.localIP());
155+
156+
pinMode(ledPin, OUTPUT); // initialize ledPin as an output.
157+
digitalWrite(ledPin, ledState); // initialize pin as low (LED Off)
158+
}
159+
160+
void loop()
161+
{
162+
// run this in loop to poll new messages
163+
if (millis() > Bot_lasttime + Bot_mtbs)
164+
{
165+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
166+
167+
while (numNewMessages)
168+
{
169+
Serial.println("got response");
170+
handleNewMessages(numNewMessages);
171+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
172+
}
173+
174+
Bot_lasttime = millis();
175+
}
176+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ESP32 - LED button update
2+
3+
This is an example of how one can update inline keyboard messages (buttons).
4+
The message_id of the specific message is sent when a message is received.
5+
This message_id can be used to UPDATE that message.
6+
One can update text inside a message, but also buttons can be updated.
7+
This way one can build menu's, like the menu the botfather uses.
8+
9+
In this simple example we use a inlinekeyboard button to toggle (and update) the state of a LED.
10+
11+
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
12+
13+
Example and update to Universal-Arduino-Telegram-Bot originally written by
14+
[Frits Jan van Kempen] (https://github.com/fritsjan) with inspiration from [RomeHein] (https://github.com/RomeHein)
15+
16+
Adapted by [Brian Lough](https://github.com/witnessmenow)
17+
18+
## License
19+
20+
You may copy, distribute and modify the software provided that modifications are described and licensed for free under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html). Derivatives works (including modifications or anything statically linked to the library) can only be redistributed under [LGPL-3](http://www.gnu.org/licenses/lgpl-3.0.html), but applications that use the library don't have to be.

0 commit comments

Comments
 (0)