Skip to content

Commit 396bb7f

Browse files
authored
Merge pull request witnessmenow#33 from rusboard/master
send Chat Action code and example
2 parents ef44295 + 34e2d65 commit 396bb7f

File tree

4 files changed

+139
-0
lines changed

4 files changed

+139
-0
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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.sendChatAction(chat_id, "typing");
39+
delay(4000);
40+
bot.sendMessage(chat_id, "Did you see the action message?");
41+
42+
// You can't use own message, just choose from one of bellow
43+
44+
//typing for text messages
45+
//upload_photo for photos
46+
//record_video or upload_video for videos
47+
//record_audio or upload_audio for audio files
48+
//upload_document for general files
49+
//find_location for location data
50+
51+
//more info here - https://core.telegram.org/bots/api#sendchataction
52+
}
53+
54+
if (text == "/start") {
55+
String welcome = "Welcome to Universal Arduino Telegram Bot library, " + from_name + ".\n";
56+
welcome += "This is Chat Action Bot example.\n\n";
57+
welcome += "/send_test_action : to send test chat action message\n";
58+
bot.sendMessage(chat_id, welcome);
59+
}
60+
}
61+
}
62+
63+
64+
void setup() {
65+
Serial.begin(115200);
66+
67+
// Set WiFi to station mode and disconnect from an AP if it was Previously
68+
// connected
69+
WiFi.mode(WIFI_STA);
70+
WiFi.disconnect();
71+
delay(100);
72+
73+
// attempt to connect to Wifi network:
74+
Serial.print("Connecting Wifi: ");
75+
Serial.println(ssid);
76+
WiFi.begin(ssid, password);
77+
78+
while (WiFi.status() != WL_CONNECTED) {
79+
Serial.print(".");
80+
delay(500);
81+
}
82+
83+
Serial.println("");
84+
Serial.println("WiFi connected");
85+
Serial.print("IP address: ");
86+
Serial.println(WiFi.localIP());
87+
}
88+
89+
void loop() {
90+
if (millis() > Bot_lasttime + Bot_mtbs) {
91+
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
92+
93+
while(numNewMessages) {
94+
Serial.println("got response");
95+
handleNewMessages(numNewMessages);
96+
numNewMessages = bot.getUpdates(bot.last_message_received + 1);
97+
}
98+
99+
Bot_lasttime = millis();
100+
}
101+
}

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::sendChatAction(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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class UniversalTelegramBot
6565
bool sendMessageWithInlineKeyboard(String chat_id, String text,
6666
String parse_mode, String keyboard);
6767

68+
bool sendChatAction(String chat_id, String text);
69+
6870
bool sendPostMessage(JsonObject& payload);
6971
String sendPostPhoto(JsonObject& payload);
7072
String sendPhotoByBinary(String chat_id, String contentType, int fileSize,

0 commit comments

Comments
 (0)