Skip to content

Commit 2f51e2e

Browse files
committed
Init commit of Chat Action message example
1 parent dec13bc commit 2f51e2e

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*******************************************************************
2+
* An example of bot that show bot action message. *
3+
* *
4+
* *
5+
* *
6+
* written by Vadim Sinitski *
7+
*******************************************************************/
8+
#include <ESP8266WiFi.h>
9+
#include <WiFiClientSecure.h>
10+
#include <UniversalTelegramBot.h>
11+
12+
// Initialize Wifi connection to the router
13+
char ssid[] = "XXXXXX"; // your network SSID (name)
14+
char password[] = "YYYYYY"; // your network key
15+
16+
// Initialize Telegram BOT
17+
#define BOTtoken "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" // your Bot Token (Get from Botfather)
18+
19+
WiFiClientSecure client;
20+
UniversalTelegramBot bot(BOTtoken, client);
21+
22+
int Bot_mtbs = 1000; //mean time between scan messages
23+
long Bot_lasttime; //last time messages' scan has been done
24+
bool Start = false;
25+
26+
void handleNewMessages(int numNewMessages) {
27+
Serial.println("handleNewMessages");
28+
Serial.println(String(numNewMessages));
29+
30+
for (int i=0; i<numNewMessages; i++) {
31+
String chat_id = String(bot.messages[i].chat_id);
32+
String text = bot.messages[i].text;
33+
34+
String from_name = bot.messages[i].from_name;
35+
if (from_name == "") from_name = "Guest";
36+
37+
if (text == "/send_test_action") {
38+
bot.sendChatActionMessage(chat_id, "typing");
39+
40+
// You can't use own message, just choose from one of bellow
41+
42+
//typing for text messages
43+
//upload_photo for photos
44+
//record_video or upload_video for videos
45+
//record_audio or upload_audio for audio files
46+
//upload_document for general files
47+
//find_location for location data
48+
49+
//more info here - https://core.telegram.org/bots/api#sendchataction
50+
}
51+
52+
if (text == "/start") {
53+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
54+
welcome += "This is Chat Action Bot example.\n\n";
55+
welcome += "/send_test_action : to send test chat action message\n";
56+
bot.sendMessage(chat_id, welcome);
57+
}
58+
}
59+
}
60+
61+
62+
void setup() {
63+
Serial.begin(115200);
64+
65+
// Set WiFi to station mode and disconnect from an AP if it was Previously
66+
// connected
67+
WiFi.mode(WIFI_STA);
68+
WiFi.disconnect();
69+
delay(100);
70+
71+
// attempt to connect to Wifi network:
72+
Serial.print("Connecting Wifi: ");
73+
Serial.println(ssid);
74+
WiFi.begin(ssid, password);
75+
76+
while (WiFi.status() != WL_CONNECTED) {
77+
Serial.print(".");
78+
delay(500);
79+
}
80+
81+
Serial.println("");
82+
Serial.println("WiFi connected");
83+
Serial.print("IP address: ");
84+
Serial.println(WiFi.localIP());
85+
}
86+
87+
void loop() {
88+
if (millis() > Bot_lasttime + Bot_mtbs) {
89+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
90+
91+
while(numNewMessages) {
92+
Serial.println("got response");
93+
handleNewMessages(numNewMessages);
94+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
95+
}
96+
97+
Bot_lasttime = millis();
98+
}
99+
}

examples/ESP8266/ChatAction/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ESP8266 - Chat Action
2+
3+
This is a basic example of how to use chat action using UniversalTelegramBot for ESP8266 based boards.
4+
5+
Application originally written by [Giancarlo Bacchio]([email protected]) for [ESP8266-TelegramBot library](https://github.com/Gianbacchio/ESP8266-TelegramBot)
6+
7+
Adapted by [Brian Lough](https://github.com/witnessmenow)
8+
9+
NOTE: You will need to enter your SSID, password and bot Token for the example to work.
10+
11+
## License
12+
13+
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.

src/UniversalTelegramBot.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,3 +550,26 @@ bool UniversalTelegramBot::checkForOkResponse(String response) {
550550

551551
return false;
552552
}
553+
554+
bool UniversalTelegramBot::sendChatActionMessage(String chat_id, String text) {
555+
556+
bool sent = false;
557+
if (_debug) Serial.println("SEND Chat Action Message");
558+
long sttime = millis();
559+
560+
if (text != "") {
561+
while (millis() < sttime+8000) { // loop for a while to send the message
562+
String command="bot"+_token+"/sendChatAction?chat_id="+chat_id+"&action="+text;
563+
String response = sendGetToTelegram(command);
564+
565+
if (_debug) Serial.println(response);
566+
sent = checkForOkResponse(response);
567+
568+
if (sent) {
569+
break;
570+
}
571+
}
572+
}
573+
574+
return sent;
575+
}

src/UniversalTelegramBot.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ class UniversalTelegramBot
6464
bool oneTime = false, bool selective = false);
6565
bool sendMessageWithInlineKeyboard(String chat_id, String text,
6666
String parse_mode, String keyboard);
67+
bool sendChatActionMessage(String chat_id, String text);
6768

6869
bool sendPostMessage(JsonObject& payload);
6970
String sendPostPhoto(JsonObject& payload);

0 commit comments

Comments
 (0)