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 (" \n WiFi connected! IP address: " );
69+ Serial.println (WiFi.localIP ());
70+ break ;
71+ case SYSTEM_EVENT_STA_DISCONNECTED:
72+ Serial.println (" \n WiFi 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 (" \t SIZE: " );
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\n Free heap: %d\n " , ESP.getFreeHeap ());
127+ Serial.print (" \n Start 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.\n Filesystem 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 (" \n Test 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 (" \n Text message received: " );
183+ Serial.println (msg.text );
184+
185+ if (msg.text .equalsIgnoreCase (" /photofs1" )) {
186+ Serial.println (" \n Sending 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 (" \n Sending 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 (" \n Sending 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 (" \n Sending 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+ }
0 commit comments