Skip to content

Commit 33ab942

Browse files
committed
Improved example for reply keyboard and changed filename example, there will be more examples in this folder.
1 parent c04b91d commit 33ab942

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
/*******************************************************************
2-
* An example of how to use a custom reply keyboard. *
2+
* An example of how to use a custom reply keyboard markup. *
33
* *
44
* *
55
* written by Brian Lough *
66
*******************************************************************/
7-
87
#include <ESP8266WiFi.h>
98
#include <WiFiClientSecure.h>
109
#include <UniversalTelegramBot.h>
1110

12-
1311
// Initialize Wifi connection to the router
14-
char ssid[] = "xxxxxxxxxxxxxxxxxxxxxx"; // your network SSID (name)
15-
char password[] = "yyyyyyyy"; // your network key
16-
17-
18-
19-
const int ledPin = 13;
12+
char ssid[] = "xxxxxxx"; // your network SSID (name)
13+
char password[] = "yyyyyyyy"; // your network key
2014

2115
// Initialize Telegram BOT
2216
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
@@ -26,87 +20,95 @@ UniversalTelegramBot bot(BOTtoken, client);
2620

2721
int Bot_mtbs = 1000; //mean time between scan messages
2822
long Bot_lasttime; //last time messages' scan has been done
23+
24+
const int ledPin = 13;
2925
int ledStatus = 0;
3026

3127
void handleNewMessages(int numNewMessages) {
3228
Serial.println("handleNewMessages");
3329
Serial.println(String(numNewMessages));
34-
for(int i=0; i<numNewMessages; i++) {
30+
31+
for (int i=0; i<numNewMessages; i++) {
3532
String chat_id = String(bot.messages[i].chat_id);
3633
String text = bot.messages[i].text;
34+
String from_name = bot.messages[i].from_name;
35+
3736
if (text == "/ledon") {
3837
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
3938
ledStatus = 1;
4039
bot.sendMessage(chat_id, "Led is ON", "");
4140
}
41+
4242
if (text == "/ledoff") {
4343
ledStatus = 0;
4444
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
4545
bot.sendMessage(chat_id, "Led is OFF", "");
4646
}
47+
4748
if (text == "/status") {
4849
if(ledStatus){
4950
bot.sendMessage(chat_id, "Led is ON", "");
5051
} else {
5152
bot.sendMessage(chat_id, "Led is OFF", "");
5253
}
5354
}
55+
5456
if (text == "/options") {
5557
String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/status\"]]";
5658
bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
5759
}
5860

5961
if (text == "/start") {
60-
String welcome = "Welcome from FlashLedBot, your personal Bot on ESP8266\n";
61-
welcome = welcome + "/ledon : to switch the Led ON\n";
62-
welcome = welcome + "/ledoff : to switch the Led OFF\n";
63-
welcome = welcome + "/status : Returns current status of LED\n";
64-
welcome = welcome + "/options : returns the custom keyboard\n";
62+
String welcome = "Welcome to Universal Telegram Bot library, " + from_name + ".\n";
63+
welcome += "This is Reply Keyboard Markup example.\n\n";
64+
welcome += "/ledon : to switch the Led ON\n";
65+
welcome += "/ledon : to switch the Led ON\n";
66+
welcome += "/ledoff : to switch the Led OFF\n";
67+
welcome += "/status : Returns current status of LED\n";
68+
welcome += "/options : returns the custom keyboard\n";
6569
bot.sendMessage(chat_id, welcome, "Markdown");
6670
}
6771
}
6872
}
6973

70-
7174
void setup() {
7275
Serial.begin(115200);
7376

74-
// Set WiFi to station mode and disconnect from an AP if it was Previously
75-
// connected
77+
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
7678
WiFi.mode(WIFI_STA);
7779
WiFi.disconnect();
7880
delay(100);
81+
7982
// attempt to connect to Wifi network:
8083
Serial.print("Connecting Wifi: ");
8184
Serial.println(ssid);
8285
WiFi.begin(ssid, password);
86+
8387
while (WiFi.status() != WL_CONNECTED) {
8488
Serial.print(".");
8589
delay(500);
8690
}
91+
8792
Serial.println("");
8893
Serial.println("WiFi connected");
89-
Serial.println("IP address: ");
90-
IPAddress ip = WiFi.localIP();
91-
Serial.println(ip);
94+
Serial.print("IP address: ");
95+
Serial.println(WiFi.localIP());
9296

9397
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
9498
delay(10);
9599
digitalWrite(ledPin, HIGH); // initialize pin as off
96-
97100
}
98101

99-
100-
101102
void loop() {
102-
103103
if (millis() > Bot_lasttime + Bot_mtbs) {
104104
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
105+
105106
while(numNewMessages) {
106107
Serial.println("got response");
107108
handleNewMessages(numNewMessages);
108109
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
109110
}
111+
110112
Bot_lasttime = millis();
111113
}
112114
}

0 commit comments

Comments
 (0)