Skip to content
This repository was archived by the owner on Jan 16, 2022. It is now read-only.

Commit 0016abb

Browse files
authored
Merge pull request #3 from cotestatnt/dev
Dev
2 parents 752e698 + 807fbb1 commit 0016abb

23 files changed

+1467
-672
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ Don't you know Telegram bots and how to setup one? Check [this](https://core.tel
2222

2323
### Features
2424
+ Send and receive non-blocking messages to Telegram bot
25+
+ Send photo both from url and from local filesystem (SPIFFS, LittleFS, FFAT, SD etc etc )
2526
+ Inline keyboards
2627
+ Reply keyboards
2728
+ Receive localization messages
2829
+ Receive contacts messages
2930
+ Http communication on ESP32 work on own task pinned to Core0
3031

3132
### To do
32-
+ Send and receive images
33+
+ Send documents
3334

3435
### Supported boards
3536
The library works with the ESP8266 and ESP32 chipset.
@@ -40,6 +41,7 @@ Take a look at the examples provided in the [examples folder](https://github.com
4041
### Reference
4142
[Here how to use the library](https://github.com/cotestatnt/AsyncTelegram/blob/master/REFERENCE.md).
4243

44+
+ 1.0.6 AsyncTelegram now can send also pictures
4345
+ 1.0.5 Added possibility to forward a messege to a puclic channel (bot must be one of admins) or to a specific user
4446
+ 1.0.4 Fixed ArduinoJson ARDUINOJSON_DECODE_UNICODE define
4547
+ 1.0.3 Bug fixes
60.6 KB
Loading
9.47 KB
Loading
19.9 KB
Loading
3.05 KB
Loading
20.9 KB
Loading
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*
2+
https://github.com/cotestatnt/AsyncTelegram
3+
Name: sendPhoto.ino
4+
Created: 20/06/2020
5+
Author: Tolentino Cotesta <cotestatnt@yahoo.com>
6+
Description: an example to show how send a picture from bot.
7+
8+
Note:
9+
Sending image to Telegram take some time (as longer as bigger are picture files)
10+
In this example image files will be sent in tree ways and for two of them, FFat filesystem is required
11+
12+
- with command /photofs, bot will send an example image stored in filesystem (or in an external SD)
13+
- with command /photohost:<host>/path/to/image, bot will send a sendPhoto command
14+
uploading the image file that first was downloaded from a LAN network address.
15+
If the file is small enough, could be stored only in memory, but for more reliability we save it before on flash.
16+
N.B. This can be useful in order to send images stored in local webservers, wich is not accessible from internet.
17+
With images hosted on public webservers, this is not necessary because Telegram can handle links and parse it properly.
18+
19+
- with command /photoweb:<url>, bot will send a sendPhoto command passing the url provided
20+
*/
21+
22+
#include <Arduino.h>
23+
24+
#include <FFat.h>
25+
#include <AsyncTelegram.h>
26+
27+
AsyncTelegram myBot;
28+
const char* ssid = "XXXXXXXXX"; // REPLACE mySSID WITH YOUR WIFI SSID
29+
const char* pass = "XXXXXXXXX"; // REPLACE myPassword YOUR WIFI PASSWORD, IF ANY
30+
const char* token = "XXXXXXXXX:XXXXXXXXXXXXXXXXXXXXXXX"; // REPLACE myToken WITH YOUR TELEGRAM BOT TOKEN
31+
32+
/* For upload filesystem (folder /data)
33+
https://github.com/lorol/arduino-esp32fs-plugin
34+
*/
35+
36+
// You only need to format FFat the first time you run a test
37+
#define FORMAT_FS_IF_FAILED true
38+
#define LED_BUILTIN 2
39+
40+
41+
//Example url == "http://192.168.2.81/telegram.png"
42+
void downloadFile(fs::FS &fs, String url, String fileName){
43+
HTTPClient http;
44+
WiFiClient client;
45+
Serial.println(url);
46+
File file = fs.open("/" + fileName, "w");
47+
if (file) {
48+
http.begin(client, url);
49+
int httpCode = http.GET();
50+
if (httpCode > 0) {
51+
if (httpCode == HTTP_CODE_OK) {
52+
http.writeToStream(&file);
53+
}
54+
} else {
55+
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
56+
}
57+
file.close();
58+
}
59+
http.end();
60+
}
61+
62+
63+
#if defined(ESP32)
64+
// WiFi event handler
65+
void WiFiEvent(WiFiEvent_t event) {
66+
switch (event) {
67+
case SYSTEM_EVENT_STA_GOT_IP:
68+
Serial.print("\nWiFi connected! IP address: ");
69+
Serial.println(WiFi.localIP());
70+
break;
71+
case SYSTEM_EVENT_STA_DISCONNECTED:
72+
Serial.println("\nWiFi lost connection");
73+
WiFi.setAutoReconnect(true);
74+
myBot.reset();
75+
break;
76+
default: break;
77+
}
78+
}
79+
#endif
80+
81+
82+
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
83+
Serial.printf("Listing directory: %s\r\n", dirname);
84+
85+
File root = fs.open(dirname);
86+
if(!root){
87+
Serial.println("- failed to open directory");
88+
return;
89+
}
90+
if(!root.isDirectory()){
91+
Serial.println(" - not a directory");
92+
return;
93+
}
94+
95+
File file = root.openNextFile();
96+
while(file){
97+
if(file.isDirectory()){
98+
Serial.print(" DIR : ");
99+
Serial.println(file.name());
100+
if(levels){
101+
listDir(fs, file.name(), levels -1);
102+
}
103+
} else {
104+
Serial.print(" FILE: ");
105+
Serial.print(file.name());
106+
Serial.print("\tSIZE: ");
107+
Serial.println(file.size());
108+
}
109+
file = root.openNextFile();
110+
}
111+
}
112+
113+
114+
void setup() {
115+
pinMode(LED_BUILTIN, OUTPUT);
116+
// initialize the Serial
117+
Serial.begin(115200);
118+
119+
WiFi.setAutoConnect(true);
120+
WiFi.mode(WIFI_STA);
121+
122+
#if defined(ESP32)
123+
Serial.printf("setup() running on core %d\n", xPortGetCoreID());
124+
WiFi.onEvent(WiFiEvent);
125+
#endif
126+
Serial.printf("\n\nFree heap: %d\n", ESP.getFreeHeap());
127+
Serial.print("\nStart connection to WiFi...");
128+
delay(100);
129+
// connects to access point
130+
WiFi.begin(ssid, pass);
131+
delay(500);
132+
while (WiFi.status() != WL_CONNECTED) {
133+
Serial.print('.');
134+
delay(500);
135+
}
136+
137+
if(!FFat.begin(FORMAT_FS_IF_FAILED)){
138+
Serial.println("SPIFFS Mount Failed.\nFilesystem will be formatted, please wait.");
139+
}
140+
141+
Serial.printf("Total space: %10u\n", FFat.totalBytes());
142+
Serial.printf("Free space: %10u\n", FFat.freeBytes());
143+
listDir(FFat, "/", 0);
144+
145+
// Set the Telegram bot properies
146+
myBot.setUpdateTime(1000);
147+
myBot.setTelegramToken(token);
148+
149+
// Check if all things are ok
150+
Serial.print("\nTest Telegram connection... ");
151+
myBot.begin() ? Serial.println("OK") : Serial.println("NOK");
152+
153+
TBMessage msg;
154+
msg.sender.id = 436865110;
155+
myBot.sendMessage(msg, "myBot ready");
156+
157+
}
158+
159+
160+
161+
void loop() {
162+
163+
// In the meantime LED_BUILTIN will blink with a fixed frequency
164+
// to evaluate async and non-blocking working of library
165+
static uint32_t ledTime = millis();
166+
if (millis() - ledTime > 200) {
167+
ledTime = millis();
168+
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
169+
}
170+
171+
// a variable to store telegram message data
172+
TBMessage msg;
173+
174+
// if there is an incoming message...
175+
if (myBot.getNewMessage(msg)) {
176+
Serial.print("New message from chat_id: ");
177+
Serial.println(msg.sender.id);
178+
MessageType msgType = msg.messageType;
179+
180+
if (msgType == MessageText){
181+
// Received a text message
182+
Serial.print("\nText message received: ");
183+
Serial.println(msg.text);
184+
185+
if (msg.text.equalsIgnoreCase("/photofs1")) {
186+
Serial.println("\nSending Photo from filesystem");
187+
String myFile = "telegram-bot1.jpg";
188+
myBot.sendPhotoByFile(msg.sender.id, myFile, FFat);
189+
}
190+
191+
else if (msg.text.equalsIgnoreCase("/photofs2")) {
192+
Serial.println("\nSending Photo from filesystem");
193+
String myFile = "telegram-bot2.jpg";
194+
myBot.sendPhotoByFile(msg.sender.id, myFile, FFat);
195+
}
196+
197+
else if (msg.text.indexOf("/photohost>") > -1 ) {
198+
String url = msg.text.substring(msg.text.indexOf("/photohost>") + sizeof("/photohost"));
199+
String fileName = url.substring(url.lastIndexOf('/')+1);
200+
downloadFile(FFat, url, fileName);
201+
listDir(FFat, "/", 0);
202+
Serial.println("\nSending Photo from LAN: ");
203+
Serial.println(url);
204+
myBot.sendPhotoByFile(msg.sender.id, fileName, FFat);
205+
//FFat.remove("/" + fileName);
206+
listDir(FFat, "/", 0);
207+
}
208+
209+
else if (msg.text.indexOf("/photoweb>") > -1 ) {
210+
String url = msg.text.substring(msg.text.indexOf("/photoweb>") + sizeof("/photoweb"));
211+
Serial.println("\nSending Photo from web: ");
212+
Serial.println(url);
213+
myBot.sendPhotoByUrl(msg.sender.id, url, url);
214+
}
215+
216+
else {
217+
String replyMsg = "Welcome to the Async Telegram bot.\n\n";
218+
replyMsg += "/photofs1 or /photofs2 will send an example photo from fylesystem\n";
219+
replyMsg += "/photohost><host>/path/to/image will send a photo from your LAN\n";
220+
replyMsg += "/photoweb><url> will send a photo from internet\n";
221+
myBot.sendMessage(msg, replyMsg);
222+
}
223+
224+
}
225+
}
226+
}
60.6 KB
Loading
9.47 KB
Loading
19.9 KB
Loading

0 commit comments

Comments
 (0)