Skip to content

Commit bae9efa

Browse files
committed
Merge branch 'issue_1_simplify_setMyCommands'
2 parents a5a0fa1 + 03330cc commit bae9efa

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ Here is a list of features that this library covers. (Note: The examples link to
5959
|*Location*|Your bot can receive location data, either from a single location data point or live location data. |Check the example.| [Location](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/Location/Location.ino)|
6060
|*Channel Post*|Reads posts from channels. |Check the example.| [ChannelPost](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/ChannelPost/ChannelPost.ino)|
6161
|*Long Poll*|Set how long the bot will wait checking for a new message before returning now messages. <br><br> This will decrease the amount of requests and data used by the bot, but it will tie up the arduino while it waits for messages |`bot.longPoll = 60;` <br><br> Where 60 is the amount of seconds it should wait | [LongPoll](https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/master/examples/ESP8266/LongPoll/LongPoll.ino)|
62-
|*Update Firmware and SPIFFS*|You can update firmware and spiffs area through send files as a normal file with a specific caption. |`update firmware` <br>or<br>`update spiffs`<br> These are captions for example. | [telegramOTA](https://github.com/solcer/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP32/telegramOTA/telegramOTA.ino)|
62+
|*Update Firmware and SPIFFS*|You can update firmware and spiffs area through send files as a normal file with a specific caption. |`update firmware` <br>or<br>`update spiffs`<br> These are captions for example. | [telegramOTA](https://github.com/solcer/Universal-Arduino-Telegram-Bot/blob/master/examples/ESP32/telegramOTA/telegramOTA.ino)|```
63+
|*Set bot's commands*|You can set bot commands programmatically from your code. The commands will be shown in a special place in the text input area| ```bot.setMyCommands("[{\"command\":\"help\", \"description\":\"get help\"},{\"command\":\"start\",\"description\":\"start conversation\"}]");```. See examples| [SetMyCommands](examples/ESP8266/SetMyCommands/SetMyCommands.ino)|
6364

6465
The full Telegram Bot API documentation can be read [here](https://core.telegram.org/bots/api). If there is a feature you would like added to the library please either raise a Github issue or please feel free to raise a Pull Request.
6566

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <ESP8266WiFi.h>
2+
#include <ArduinoOTA.h>
3+
#include <UniversalTelegramBot.h>
4+
5+
#define BOTtoken "XXXXXXXXXX:YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY" // Change it to your bot token
6+
7+
WiFiClientSecure client;
8+
UniversalTelegramBot bot(BOTtoken, client);
9+
10+
int Bot_mtbs = 1000; //mean time between scan messages
11+
long Bot_lasttime; //last time messages' scan has been done
12+
13+
void bot_setup() {
14+
client.setInsecure(); // Required for ESP8266
15+
const String commands = F("["
16+
"{\"command\":\"help\", \"description\":\"Get bot usage help\"},"
17+
"{\"command\":\"start\", \"description\":\"Message sent when you open a chat with a bot\"},"
18+
"{\"command\":\"status\",\"description\":\"Answer device current status\"}" // no comma on last command
19+
"]");
20+
bot.setMyCommands(commands);
21+
bot.sendMessage("25235518", "Hola amigo!", "Markdown");
22+
}
23+
24+
void setup() {
25+
Serial.begin(115200);
26+
Serial.print("Connecting Wifi: ");
27+
WiFi.begin(); // Using stored credentials
28+
while (WiFi.status() != WL_CONNECTED) {
29+
Serial.print(".");
30+
delay(500);
31+
}
32+
Serial.println("\r\nWiFi connected");
33+
34+
ArduinoOTA.begin(); // You should set a password for OTA. Ideally using MD5 hashes
35+
bot_setup();
36+
}
37+
38+
void loop() {
39+
ArduinoOTA.handle(); // Only program on the serial port the first time. Then on WiFi! :-)
40+
41+
if (millis() > Bot_lasttime + Bot_mtbs) {
42+
String answer;
43+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
44+
45+
for (int i = 0; i < numNewMessages; i++) {
46+
telegramMessage &msg = bot.messages[i];
47+
Serial.println("Received " + msg.text);
48+
if (msg.text == "/help") answer = "So you need _help_, uh? me too! use /start or /status";
49+
else if (msg.text == "/start") answer = "Welcome my new friend! You are the first *" + msg.from_name + "* I've ever met";
50+
else if (msg.text == "/status") answer = "All is good here, thanks for asking!";
51+
else answer = "Say what?";
52+
53+
bot.sendMessage(msg.chat_id, answer, "Markdown");
54+
}
55+
56+
Bot_lasttime = millis();
57+
}
58+
}

src/UniversalTelegramBot.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2828
#include <Arduino.h>
2929
#include <ArduinoJson.h>
3030
#include <Client.h>
31-
#include <core_version.h>
3231

3332
#define TELEGRAM_HOST "api.telegram.org"
3433
#define TELEGRAM_SSL_PORT 443

0 commit comments

Comments
 (0)