Setting up an ESP32-CAM to send photos on request to Telegram
This tutorial explains how to set up a Telegram Bot and use the Universal Arduino Telegram Bot Library with an ESP32 to take and send photos.
If you don’t have a Telegram account you need to create one first https://telegram.org/ Telegram is similar to WhatsApp but also works in browsers.
After logging in to the web interface go to https://telegram.me/botfather and click ‘OPEN IN WEB’
Click ‘START’ and you will see the available commands. You can also type /start in the Telegram web interface to see these commands again.
Type /newbot to create a new bot. The username has to follow these conventions: Usernames are 5-32 characters long and are case insensitive, but may only include Latin characters, numbers, and underscores. Your bot’s username must end in ‘bot’, e.g. ‘tetris_bot’ or ‘TetrisBot’.
Copy the token highlighted in red somewhere safe.
Click the t.me/xxxxx_bot link in the message to go to your bot page. Click ‘START’.
ESP32 Arduino IDE Setup
You might need to install or update the ArduinoJson library in the IDE Library Manager to version 6+.
The Arduino Telegram Bot library used for this tutorial isn’t the release version so it has to either be downloaded from the v1.2 branch and renamed to not conflict with the release version or you can just download the UniversalTelegramBotRZO .h and .cpp files from here: https://github.com/robotzero1/esp32cam-telegram and save them all to a new directory in your Arduino folder.
Before running a more complicated sketch it’s better to test everything is working. Copy the ‘EchoBot’ sketch into the new directory, change the #include to match the .h and .cpp file names in the folder and upload it. Open the serial monitor in the IDE and then type something into the message box in the chat with your bot on Telegram and click send.
You should see ‘got response’ in the serial monitor and then the message you entered echoed back to your phone by the ESP32.
Above you can see I sent the message Hello World to the bot (RobotZeroOne) and it replied with the same message.
Uploading a Photo to Telegram
I found sending anything larger than an QVGA size file in one go would crash so to send larger images they have to be sent byte by byte.
Copy the TelegramSendPhotoChunked1_2.ino file into a new Sketch, change the Wi-Fi connection details and save it. Copy the .h and .cpp files above to the new folder. Upload the sketch to the ESP32-CAM and reset it.
Now when you send a message to your bot, the ESP32-CAM will take a photo and send it back to your phone.
If you go back to the BotFather and run the following commands: /setabouttext and /setdescription you can add more information. If you want to add your bot to a group. Also if you are stuck you can see all the commands by typing /help when you are talking to the BotFather.
Buy Me A Coffee
If you found something useful above please say thanks by buying me a coffee here...
References
Universal Arduino Telegram Bot Library: https://github.com/witnessmenow/Universal-Arduino-Telegram-Bot/tree/V1.2.0
Telegram Bots Help Page: https://core.telegram.org/bots#botfather
Hello,
I tried your old post “TTGO T-Camera Fridge PIR Security Camera” successful. Now I was trying to implement this camera example with your Telegram post.
The first example in this post is working fine for me. I can send a message to the Telegram Bot and I receive the same message as answer. When I try the second example with uploading a Photo to Telegram, the camera module is not working. Is it possible that the “esp_camera.h” wrong for me? In the old sketch (with TTGO T-Camera Fridge…” it is “OV2640.h”.
Hi,
Try changing the pin definitions to:
#define PWDN_GPIO_NUM 26
#define RESET_GPIO_NUM -1
#define XCLK_GPIO_NUM 32
#define SIOD_GPIO_NUM 13
#define SIOC_GPIO_NUM 12
#define Y9_GPIO_NUM 39
#define Y8_GPIO_NUM 36
#define Y7_GPIO_NUM 23
#define Y6_GPIO_NUM 18
#define Y5_GPIO_NUM 15
#define Y4_GPIO_NUM 4
#define Y3_GPIO_NUM 14
#define Y2_GPIO_NUM 5
#define VSYNC_GPIO_NUM 27
#define HREF_GPIO_NUM 25
#define PCLK_GPIO_NUM 19
Hi,
it works perfect. Thanks a lot.
Now I can send any message and receive a photo as answer. I’ll try to combine that function with PIR Sensor to get a photo if something happens in front of the camera.
Hi
This sketch seems very promising but having tried several ESP32s, several versions of build, several versions of “.h” libraries and several versions of json library …. nothing will compile. Most of the errors relate to json. I have spent a very long time on this and I don’t know what to try next. I have been able to get the simpler “ledON” to work.
HI, Have you tried using the Arduino IDE? It should work if you have ArduinoJson v6 installed and you just put all the files in a single folder (UniversalTelegramBotRZO.h , UniversalTelegramBotRZO.cpp and TelegramSendPhotoChunked1_2.ino)
Hey!
Thank you for your lib and instructions, it is working well!
Could you please give some guidiance for following scenario:
I’d like to join the bot to a group. This is working and just after joining the Bot sends a picture. If now somebody of the group is sending a text message, the bot doesn’t. I was expecting that the bot sends a picture when somebody in group sends a message.
Thank you in advance
Found it by my own. Needed to disable the privacy for the bot.
Thanks again for the instructions
Hi,
I’ ve used your code succesfully and, at last, I got more decent photos from my board.
So, many, many thanks for your time and efforts spent to make the things work.
(This is also a success to me, after I failed with the “as it is” library as seen in a couple of other tutorials.)
Therefore, I do have a question, related the the latest version of the library UniversalTelegramBot (to date 1.3.0):
(Begging your pardon for being a little bit polemical) Why isn’t it integrating your hack ? (Because, comparing your addtions to 1.2 with what was now released, and assuming I was not blind when I’ve done that, your work is not present).
Hi,
From memory, 1.2 worked fine once I’d worked out how to send the images bit by bit. The additions I made were for the other Telegram bot scripts I wrote – https://github.com/robotzero1/esp32cam-telegram I guess Brian didn’t see what I did or didn’t think it was worth adding to his project. Glad it’s working for you.
Yes !
At last, I got the point: sending the photo was the clue.
I used the library ver. 1.3.0 as it is then I’ve modified the example provided by Brian. In there I’ve changed only the parts related to the call to “bot.sendPhotoByBinary(…)”
I’ve also added two extra Telegram commands (“appetite comes with eating”, isn’t it?) to change cam resolution (up to XGA) and all the code works as expected: now I get the photo and with the size I set.
Thank you again and, please, accept my appologises for wasting your time.
Hi,
I am looking for a manner making two ESP32-CAM listening to the same Telegram bot (my tests demonstrated that there is a condition of racing between the two boards). I’ve already tried some simple changes (like reducing polling to less than 2 secs, use different frequency for the updates, split the code sendPhoto into two : take and send) but they didn’t help.
My googling did not produce big deal of results, hence I’m back seeking your advice.
Hacking Universal Telegram Bot Library is not really the problem here. The real problem is that I do not know where from to start…
Hi, So you want them both to respond to a command in the chat and send an image in response?
Hi, I tried it on ESP32-CAM but it does not complie and gives error “Error compiling for board AI Thinker ESP32 Cam”. can you help?
Hi, Is there anything else in the IDE error message?
I am trying the TelegramSendPhotoChunked1_2.ino example and managed, followed the description exactly and managed to compile and upload the sketch. The problem I run into is that the botconnection fails (error”[BOT]Conection error
Received empty string in response!”. I am sure of the Bot token which works well in other sketches…. Any ideas?
Not really. I don’t remember much about this project apart from the it was quite tricky getting all the bits working together. Sorry not much help.
OK, thanks for the reply. Found a working solution using the TelegramAsync library.