Telegram Bot integration fails sending photos

I have a problem sending pictures to a Telegram channel using a Telegram Bot from the core.

My automation, which sends pictures to a telegram channel stopped working with the following error message in logs:

Logger: homeassistant.components.telegram_bot
Source: components/telegram_bot/__init__.py:756
Integration: Telegram bot (documentation, issues)
First occurred: 10:11:33 AM (20 occurrences)
Last logged: 10:48:48 AM

Error sending photo: urllib3 HTTPError ('Connection aborted.', TimeoutError('The write operation timed out')). Args: (), kwargs: {'chat_id': -1001963003364, 'photo': <_io.BytesIO object at 0xffff662c2430>, 'caption': 'car camera1 1685000590.548033', 'disable_notification': False, 'reply_markup': None, 'timeout': None, 'parse_mode': 'Markdown'}

I assume this is an error the telegram_bot integration trying to reach the telegram API api.telegram.org, so I test it from Home Assistance SSH console:

$ curl https://api.telegram.org/bot<token>/getUpdates
{"ok":true,"result":[]}

So I can reach the telegram bot!

I tried to manually send the photo using the service call:

service: notify.camera_people
data:
  data:
    photo:
      - url: >-
          http://localhost:8123/api/frigate/notifications/1685001437.322383-o86h2i/snapshot.jpg
        caption: test
  message: test1

and get the same message in logs:

Logger: homeassistant.components.telegram_bot
Source: components/telegram_bot/__init__.py:756
Integration: Telegram bot (documentation, issues)
First occurred: 11:04:47 AM (3 occurrences)
Last logged: 11:04:56 AM

Error sending photo: urllib3 HTTPError ('Connection aborted.', TimeoutError('The write operation timed out')). Args: (), kwargs: {'chat_id': -1001963003364, 'photo': <_io.BytesIO object at 0xffff66352020>, 'caption': 'person camera1 1685001881.999579', 'disable_notification': False, 'reply_markup': None, 'timeout': None, 'parse_mode': 'Markdown'}

The picture under URL is perfectly reacheable:

curl -v http://localhost:8123/api/frigate/notifications/1685001437.322383-o86h2i/snapshot.jpg
*   Trying 127.0.0.1:8123...
* Connected to localhost (127.0.0.1) port 8123 (#0)
> GET /api/frigate/notifications/1685001437.322383-o86h2i/snapshot.jpg HTTP/1.1
> Host: localhost:8123
> User-Agent: curl/8.0.1
> Accept: */*
> 
< HTTP/1.1 200 OK
< Server: nginx/1.22.1
< Date: Thu, 25 May 2023 08:08:53 GMT
< Content-Length: 1288918
< Connection: keep-alive
< Cache-Control: private, max-age=31536000
< Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
< Content-Type: image/jpeg
< 
Warning: Binary output can mess up your terminal. Use "--output -" to tell 
Warning: curl to output it to your terminal anyway, or consider "--output 
Warning: <FILE>" to save to a file.
* Failure writing output to destination
* Closing connection 0

I use standard Telegram Bot integration from Core Telegram bot - Home Assistant

I can send plain text messages to the chat without problems using the following call:

service: notify.camera_people
data:
  message: The garage door has been open for 10 minutes.
  title: Your Garage Door Friend

Sending over send_photo also does not work, results in the same error log message:

service: telegram_bot.send_photo
data:
  url: >-
    http://localhost:8123/api/frigate/notifications/1685001437.322383-o86h2i/snapshot.jpg

My telegram bot configuration in configuration.yaml:

telegram_bot:
  - platform: broadcast
    api_key: secret_key
    allowed_chat_ids:
        - -1001963003364

notify:
  - platform: telegram
    name: camera_people
    chat_id: -1001963003364

My full automation configuration:

alias: Frigate to Telegram Snapshot
description: Send snapshot to telegram
trigger:
  - platform: mqtt
    topic: frigate/events
condition:
  - condition: template
    value_template: "{{ trigger.payload_json.after.end_time != None }}"
    enabled: true
action:
  - service: notify.camera_people
    data_template:
      message: A{{trigger.payload_json["after"]["label"]}}
      data:
        photo:
          - url: >-
              http://localhost:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              {{trigger.payload_json["after"]["label"]}}
              {{trigger.payload_json["after"]["camera"]}}
              {{trigger.payload_json["after"]["end_time"]}}
mode: queued
max: 3

An empty message is required it can not contain any text. Also you no longer need to use data_template just data will do.

action:
  - service: notify.camera_people
    data:
      message: ''
      data:
        photo:
          - url: >
              http://localhost:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >
              {{trigger.payload_json["after"]["label"]}}
              {{trigger.payload_json["after"]["camera"]}}
              {{trigger.payload_json["after"]["end_time"]}}

Thank you Tom! I’ve set it to the snipped, as you recommended. At next trigger event it will show if it helped. Honestly, it sounds like a magic - set a message to empty string. The old version of the automation (with the non-empty message) did managed to send a 10-20 messages to the Telegram chat. Looks like an instable code. Anyway, it’ll report tomorrow if the fix worked.

@tom_l I’ve observed the new version for a while. From ~30 invocations over the last 12 hours I’ve got the message with picture just 2 times.

Same error repeats:

Logger: homeassistant.components.telegram_bot
Source: components/telegram_bot/__init__.py:756
Integration: Telegram bot (documentation, issues)
First occurred: May 25, 2023 at 11:27:14 AM (1017 occurrences)
Last logged: 9:48:53 AM

Error sending photo: urllib3 HTTPError ('Connection aborted.', TimeoutError('The write operation timed out')). Args: (), kwargs: {'chat_id': -1001963003364, 'photo': <_io.BytesIO object at 0xffff6915d1c0>, 'caption': 'person camera3 1685169478.558071', 'disable_notification': False, 'reply_markup': None, 'timeout': None, 'parse_mode': 'Markdown'}

To narrow the problem further I add action in the same automation, which sends the message w/o photo. I expect it to be executed (message send) normally for every invocation. This is to understand that the general flow of Telegram Bot works. I put second action AFTER the failing one, to understand if there is any post-effects of failure:

action:
  - service: notify.camera_people
    data:
      message: ""
      data:
        photo:
          - url: >-
              http://localhost:8123/api/frigate/notifications/{{trigger.payload_json["after"]["id"]}}/snapshot.jpg
            caption: >-
              {{trigger.payload_json["after"]["label"]}}
              {{trigger.payload_json["after"]["camera"]}}
              {{trigger.payload_json["after"]["end_time"]}}
  - service: notify.camera_people
    data:
      message: >-
        {{trigger.payload_json["after"]["label"]}}
        {{trigger.payload_json["after"]["camera"]}}
        {{trigger.payload_json["after"]["end_time"]}}
      data: {}

I’ll update of the following.

My only rational explanation on that is happing now is sending picture on slow connection (there is a 3G modem with ~2Mbit upload capacity behind) takes same seconds, which is enough for the connection to timeout.

Does anybody knows how to increase sending timeout in Telegram Bot integration for the message send request?

P.S. Found timeout option. Set it to 120. Observing…

P.P.S I think I’ve got it. This manual call does not times out and produces the picture:

service: telegram_bot.send_photo
data:
  timeout: 120
  url: >-
    http://localhost:8123/api/frigate/notifications/1685001437.322383-o86h2i/snapshot.jpg