Running a Telegram Photo Bot on the ESP32-CAM

Robot-Zero-One-Telegram-Bot.png

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’

BotFather 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’.

Create BotFather Sequence

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+.

ArduinoJson Arduino IDE

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.

Telegram Send Response Echo

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.

Image Sent to Bot

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.

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

7 Replies to “Running a Telegram Photo Bot on the ESP32-CAM”

  1. Carsten says:

    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”.

    1. WordBot says:

      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

  2. Carsten says:

    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.

  3. John says:

    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.

    1. WordBot says:

      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)

  4. Tobias says:

    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

  5. Tobias says:

    Found it by my own. Needed to disable the privacy for the bot.
    Thanks again for the instructions

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

scroll to top