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.
If you liked this tutorial and want me to create more then please say thanks by buying me a coffee here...
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