Skip to content

Commit 52b19a6

Browse files
authored
Merge pull request witnessmenow#24 from rusboard/master
Created simple version of inline keyboard message and many changes and improves of exists code.
2 parents a28f164 + 0370fce commit 52b19a6

File tree

10 files changed

+261
-130
lines changed

10 files changed

+261
-130
lines changed

.travis.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ cache:
99
- "~/.platformio"
1010

1111
env:
12-
- SCRIPT=platformioSingle EXAMPLE=EchoBot BOARDTYPE=ESP8266 BOARD=d1_mini
13-
- SCRIPT=platformioSingle EXAMPLE=CustomKeyboard BOARDTYPE=ESP8266 BOARD=d1_mini
14-
- SCRIPT=platformioSingle EXAMPLE=FlashledBot BOARDTYPE=ESP8266 BOARD=d1_mini
15-
- SCRIPT=platformioSingle EXAMPLE=UsingWiFiManager BOARDTYPE=ESP8266 BOARD=d1_mini
16-
- SCRIPT=platformioSingle EXAMPLE=BulkMessages BOARDTYPE=ESP8266 BOARD=d1_mini
12+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=EchoBot EXAMPLE_FILE=EchoBot BOARDTYPE=ESP8266 BOARD=d1_mini
13+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=CustomKeyboard/ReplyKeyboardMarkup EXAMPLE_FILE=ReplyKeyboardMarkup BOARDTYPE=ESP8266 BOARD=d1_mini
14+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=CustomKeyboard/InlineKeyboardMarkup EXAMPLE_FILE=InlineKeyboardMarkup BOARDTYPE=ESP8266 BOARD=d1_mini
15+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=FlashledBot EXAMPLE_FILE=FlashledBot BOARDTYPE=ESP8266 BOARD=d1_mini
16+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=UsingWiFiManager EXAMPLE_FILE=UsingWiFiManager BOARDTYPE=ESP8266 BOARD=d1_mini
17+
- SCRIPT=platformioSingle EXAMPLE_FOLDER=BulkMessages EXAMPLE_FILE=BulkMessages BOARDTYPE=ESP8266 BOARD=d1_mini
1718

1819
# This will run all the ESP8266 examples
1920
#- SCRIPT=platformioEsp8266 BOARD=d1_mini

examples/ESP8266/BulkMessages/BulkMessages.ino

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
#include <ArduinoJson.h>
1212

1313
// Initialize Wifi connection to the router
14-
char ssid[] = "xxxxxxx"; // your network SSID (name)
15-
char password[] = "yyyyyyyy"; // your network key
14+
char ssid[] = "XXXXXX"; // your network SSID (name)
15+
char password[] = "YYYYYY"; // your network key
1616

1717
// Initialize Telegram BOT
1818
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
@@ -43,7 +43,13 @@ void handleNewMessages(int numNewMessages) {
4343

4444
if (text == "/start") {
4545
if (addSubscribedUser(chat_id, from_name)) {
46-
bot.sendMessage(chat_id, "Welcome to BulkMessages example, " + from_name, "");
46+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
47+
welcome += "This is Bulk Messages example.\n\n";
48+
welcome += "/showallusers : show all subscribed users\n";
49+
welcome += "/testbulkmessage : send test message to subscribed users\n";
50+
welcome += "/removeallusers : remove all subscribed users\n";
51+
welcome += "/stop : unsubscribe from bot\n";
52+
bot.sendMessage(chat_id, welcome, "Markdown");
4753
} else {
4854
bot.sendMessage(chat_id, "Something wrong, please try again (later?)", "");
4955
}
@@ -61,7 +67,7 @@ void handleNewMessages(int numNewMessages) {
6167
sendMessageToAllSubscribedUsers("ATTENTION, this is bulk message for all subscribed users!");
6268
}
6369

64-
if (text == "\/showallusers") {
70+
if (text == "/showallusers") {
6571
File subscribedUsersFile = SPIFFS.open("/"+subscribed_users_filename, "r");
6672

6773
if (!subscribedUsersFile) {
@@ -78,7 +84,7 @@ void handleNewMessages(int numNewMessages) {
7884
}
7985
}
8086

81-
if (text == "\/removeallusers") {
87+
if (text == "/removeallusers") {
8288
if (SPIFFS.remove("/"+subscribed_users_filename)) {
8389
bot.sendMessage(chat_id, "All users removed", "");
8490
} else {
@@ -180,7 +186,7 @@ void sendMessageToAllSubscribedUsers(String message) {
180186
}
181187

182188
void setup() {
183-
Serial.begin(9600);
189+
Serial.begin(115200);
184190

185191
if (!SPIFFS.begin()) {
186192
Serial.println("Failed to mount file system");
@@ -215,4 +221,4 @@ void loop() {
215221

216222
Bot_lasttime = millis();
217223
}
218-
}
224+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*******************************************************************
2+
* An example of how to use a custom reply keyboard markup. *
3+
* *
4+
* *
5+
* written by Vadim Sinitski *
6+
*******************************************************************/
7+
#include <ESP8266WiFi.h>
8+
#include <WiFiClientSecure.h>
9+
#include <UniversalTelegramBot.h>
10+
11+
// Initialize Wifi connection to the router
12+
char ssid[] = "XXXXXX"; // your network SSID (name)
13+
char password[] = "YYYYYY"; // your network key
14+
15+
// Initialize Telegram BOT
16+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
17+
18+
WiFiClientSecure client;
19+
UniversalTelegramBot bot(BOTtoken, client);
20+
21+
int Bot_mtbs = 1000; //mean time between scan messages
22+
long Bot_lasttime; //last time messages' scan has been done
23+
24+
void handleNewMessages(int numNewMessages) {
25+
Serial.println("handleNewMessages");
26+
Serial.println(String(numNewMessages));
27+
28+
for (int i=0; i<numNewMessages; i++) {
29+
String chat_id = String(bot.messages[i].chat_id);
30+
String text = bot.messages[i].text;
31+
32+
String from_name = bot.messages[i].from_name;
33+
if (from_name == "") from_name = "Guest";
34+
35+
if (text == "/options") {
36+
String keyboardJson = "[[\{ \"text\" : \"Go to Google\", \"url\" : \"https://www.google.com\" \} ]]";
37+
bot.sendMessageWithInlineKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson);
38+
}
39+
40+
if (text == "/start") {
41+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
42+
welcome += "This is Inline Keyboard Markup example.\n\n";
43+
welcome += "/options : returns the inline keyboard\n";
44+
45+
bot.sendMessage(chat_id, welcome, "Markdown");
46+
}
47+
}
48+
}
49+
50+
void setup() {
51+
Serial.begin(115200);
52+
53+
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
54+
WiFi.mode(WIFI_STA);
55+
WiFi.disconnect();
56+
delay(100);
57+
58+
// attempt to connect to Wifi network:
59+
Serial.print("Connecting Wifi: ");
60+
Serial.println(ssid);
61+
WiFi.begin(ssid, password);
62+
63+
while (WiFi.status() != WL_CONNECTED) {
64+
Serial.print(".");
65+
delay(500);
66+
}
67+
68+
Serial.println("");
69+
Serial.println("WiFi connected");
70+
Serial.print("IP address: ");
71+
Serial.println(WiFi.localIP());
72+
}
73+
74+
void loop() {
75+
if (millis() > Bot_lasttime + Bot_mtbs) {
76+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
77+
78+
while(numNewMessages) {
79+
Serial.println("got response");
80+
handleNewMessages(numNewMessages);
81+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
82+
}
83+
84+
Bot_lasttime = millis();
85+
}
86+
}

examples/ESP8266/CustomKeyboard/README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
1-
#ESP8266 - Custom Keyboard bot
1+
#Reply Keyboard Markup
22

3-
This is an example of how to use custom reply keyboards on a ESP8266 based board.
3+
This is an example of how to use reply keyboard markup on a ESP8266 based board.
4+
5+
![alt text](https://core.telegram.org/file/811140184/1/5YJxx-rostA/ad3f74094485fb97bd "Reply Keyboard example")
46

57
The application will turn on and off an LED based on commands received via telegram.
68

9+
#Inline Keyboard Markup
10+
11+
This is an example of how to use reply keyboard markup on a ESP8266 based board.
12+
13+
14+
![alt text](https://core.telegram.org/file/811140999/1/2JSoUVlWKa0/4fad2e2743dc8eda04 "Inline Keyboard example")
15+
16+
Right now working only URL redirection button. Other features will be added later.
17+
18+
-----------------
19+
720
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
821

922
Application written by [Brian Lough](https://github.com/witnessmenow)
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[] = "XXXXXX"; // your network SSID (name)
13+
char password[] = "YYYYYY"; // your network key
2014

2115
// Initialize Telegram BOT
2216
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
@@ -26,87 +20,97 @@ 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+
35+
String from_name = bot.messages[i].from_name;
36+
if (from_name == "") from_name = "Guest";
37+
3738
if (text == "/ledon") {
3839
digitalWrite(ledPin, HIGH); // turn the LED on (HIGH is the voltage level)
3940
ledStatus = 1;
4041
bot.sendMessage(chat_id, "Led is ON", "");
4142
}
43+
4244
if (text == "/ledoff") {
4345
ledStatus = 0;
4446
digitalWrite(ledPin, LOW); // turn the LED off (LOW is the voltage level)
4547
bot.sendMessage(chat_id, "Led is OFF", "");
4648
}
49+
4750
if (text == "/status") {
4851
if(ledStatus){
4952
bot.sendMessage(chat_id, "Led is ON", "");
5053
} else {
5154
bot.sendMessage(chat_id, "Led is OFF", "");
5255
}
5356
}
57+
5458
if (text == "/options") {
5559
String keyboardJson = "[[\"/ledon\", \"/ledoff\"],[\"/status\"]]";
5660
bot.sendMessageWithReplyKeyboard(chat_id, "Choose from one of the following options", "", keyboardJson, true);
5761
}
5862

5963
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";
64+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
65+
welcome += "This is Reply Keyboard Markup example.\n\n";
66+
welcome += "/ledon : to switch the Led ON\n";
67+
welcome += "/ledon : to switch the Led ON\n";
68+
welcome += "/ledoff : to switch the Led OFF\n";
69+
welcome += "/status : Returns current status of LED\n";
70+
welcome += "/options : returns the reply keyboard\n";
6571
bot.sendMessage(chat_id, welcome, "Markdown");
6672
}
6773
}
6874
}
6975

70-
7176
void setup() {
7277
Serial.begin(115200);
7378

74-
// Set WiFi to station mode and disconnect from an AP if it was Previously
75-
// connected
79+
// Set WiFi to station mode and disconnect from an AP if it was Previously connected
7680
WiFi.mode(WIFI_STA);
7781
WiFi.disconnect();
7882
delay(100);
83+
7984
// attempt to connect to Wifi network:
8085
Serial.print("Connecting Wifi: ");
8186
Serial.println(ssid);
8287
WiFi.begin(ssid, password);
88+
8389
while (WiFi.status() != WL_CONNECTED) {
8490
Serial.print(".");
8591
delay(500);
8692
}
93+
8794
Serial.println("");
8895
Serial.println("WiFi connected");
89-
Serial.println("IP address: ");
90-
IPAddress ip = WiFi.localIP();
91-
Serial.println(ip);
96+
Serial.print("IP address: ");
97+
Serial.println(WiFi.localIP());
9298

9399
pinMode(ledPin, OUTPUT); // initialize digital ledPin as an output.
94100
delay(10);
95101
digitalWrite(ledPin, HIGH); // initialize pin as off
96-
97102
}
98103

99-
100-
101104
void loop() {
102-
103105
if (millis() > Bot_lasttime + Bot_mtbs) {
104106
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
107+
105108
while(numNewMessages) {
106109
Serial.println("got response");
107110
handleNewMessages(numNewMessages);
108111
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
109112
}
113+
110114
Bot_lasttime = millis();
111115
}
112116
}

0 commit comments

Comments
 (0)