Telegram media group seems to work now

This commit is contained in:
2026-01-31 02:39:12 +03:00
parent 97a6ade0a4
commit d30722498e
2 changed files with 173 additions and 81 deletions

View File

@@ -83,8 +83,23 @@
# Behavior: # Behavior:
# - First sends a text notification message to each Telegram chat # - First sends a text notification message to each Telegram chat
# - Then sends photos/videos as replies to that notification message # - Then sends photos/videos as replies to that notification message
# - Media is sent as individual messages (Home Assistant doesn't support # - By default, media is sent as individual messages
# Telegram media groups / albums) # - Optional: Send as media group (album) using Telegram API directly
#
# Media Group Mode (Album):
# - Sends multiple photos/videos grouped together as a Telegram album
# - Requires additional setup: add rest_command to configuration.yaml:
#
# rest_command:
# immich_album_watcher_telegram_send_media_group:
# url: "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMediaGroup"
# method: POST
# content_type: "application/json"
# payload: '{"chat_id": {{ chat_id }}, "media": {{ media | to_json }}, "reply_to_message_id": {{ reply_to_message_id }}}'
#
# - Replace <YOUR_BOT_TOKEN> with your actual bot token from BotFather
# - TIP: Use secrets.yaml for the URL: url: !secret telegram_bot_send_media_group_url
# - Media groups support 2-10 items (Telegram limit)
# #
# Limitations: # Limitations:
# - Only assets with valid public URLs will be sent # - Only assets with valid public URLs will be sent
@@ -418,6 +433,26 @@ blueprint:
selector: selector:
boolean: boolean:
telegram_send_as_media_group:
name: Send as Media Group (Album)
description: >
Send multiple photos/videos as a grouped album instead of individual messages.
REQUIRES additional setup - add this to your configuration.yaml:
rest_command:
immich_album_watcher_telegram_send_media_group:
url: "https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMediaGroup"
method: POST
content_type: "application/json"
payload: '{"chat_id": {{ chat_id }}, "media": {{ media | to_json }}, "reply_to_message_id": {{ reply_to_message_id }}}'
Replace <YOUR_BOT_TOKEN> with your actual bot token from BotFather.
TIP: Store the full URL in secrets.yaml and use: url: !secret telegram_bot_send_media_group_url
Telegram limits media groups to 2-10 items.
default: false
selector:
boolean:
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
# Periodic Summary # Periodic Summary
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@@ -566,6 +601,7 @@ variables:
telegram_media_delay: !input telegram_media_delay telegram_media_delay: !input telegram_media_delay
telegram_video_warning_template: !input telegram_video_warning telegram_video_warning_template: !input telegram_video_warning
telegram_disable_url_preview: !input telegram_disable_url_preview telegram_disable_url_preview: !input telegram_disable_url_preview
telegram_send_as_media_group: !input telegram_send_as_media_group
# Periodic Summary Settings # Periodic Summary Settings
enable_periodic_summary: !input enable_periodic_summary enable_periodic_summary: !input enable_periodic_summary
@@ -953,8 +989,10 @@ action:
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Send Media to Telegram (if enabled) # Send Media to Telegram (if enabled)
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Sends photos/videos as individual Telegram messages using telegram_bot integration. # Sends photos/videos to Telegram using telegram_bot integration.
# Note: Media is sent individually (HA doesn't support Telegram media groups). # Two modes available:
# 1. Individual messages (default) - uses telegram_bot.send_photo/send_video
# 2. Media group (album) - uses rest_command to call Telegram API directly
- choose: - choose:
- conditions: - conditions:
- condition: template - condition: template
@@ -1009,6 +1047,10 @@ action:
For images, download URLs (asset_download_url) are preferred. For images, download URLs (asset_download_url) are preferred.
View URLs may return 404. View URLs may return 404.
**Media Group Mode:**
- Enabled: {{ telegram_send_as_media_group }}
- Will use media group: {{ telegram_send_as_media_group and media_to_send | length >= 2 }}
# Build the notification message for Telegram # Build the notification message for Telegram
# Uses {video_warning} placeholder which is populated when videos are present # Uses {video_warning} placeholder which is populated when videos are present
- variables: - variables:
@@ -1042,82 +1084,132 @@ action:
- variables: - variables:
reply_to_message_id: "{{ telegram_response.chats[0].message_id | default(0) | int }}" reply_to_message_id: "{{ telegram_response.chats[0].message_id | default(0) | int }}"
# Send each media item to this chat as reply to the notification # Choose between media group (album) or individual messages
- repeat: - choose:
count: "{{ media_to_send | length }}" # ---------------------------------------------------------
sequence: # OPTION 1: Send as Media Group (Album) via REST API
# Delay before each media item (acts as delay after notification for first item) # ---------------------------------------------------------
- choose: # Requires: rest_command.immich_album_watcher_telegram_send_media_group
- conditions: # Telegram limits: 2-10 media items per group
- condition: template - conditions:
value_template: "{{ telegram_media_delay > 0 }}" - condition: template
sequence: value_template: >
- delay: {{ telegram_send_as_media_group and
milliseconds: "{{ telegram_media_delay }}" media_to_send | length >= 2 }}
sequence:
# Build the media array for Telegram API
- variables:
# Build JSON array for Telegram sendMediaGroup API
# Limit to 10 items (Telegram max for media groups)
media_group_json: >
{% set ns = namespace(items = []) %}
{% set items_to_send = media_to_send[:10] %}
{% for asset in items_to_send %}
{% set asset_type = asset.asset_type | default('IMAGE') %}
{% set playback_url = asset.asset_playback_url | default('') %}
{% set download_url = asset.asset_download_url | default('') %}
{% set view_url = asset.asset_url | default('') %}
{% if asset_type == 'VIDEO' and playback_url | length > 0 %}
{% set media_url = playback_url %}
{% elif download_url | length > 0 %}
{% set media_url = download_url %}
{% else %}
{% set media_url = view_url %}
{% endif %}
{% set media_type = 'video' if asset_type == 'VIDEO' else 'photo' %}
{% set item = {'type': media_type, 'media': media_url} %}
{% set ns.items = ns.items + [item] %}
{% endfor %}
{{ ns.items | to_json }}
- variables: # Call rest_command to send media group
current_asset: "{{ media_to_send[repeat.index - 1] }}" - service: rest_command.immich_album_watcher_telegram_send_media_group
asset_type: "{{ current_asset.asset_type | default('IMAGE') }}" continue_on_error: true
# URL preference: playback_url (videos) > download_url > asset_url data:
asset_url: > chat_id: "{{ current_chat_id }}"
{% set playback_url = current_asset.asset_playback_url | default('') %} media: "{{ media_group_json }}"
{% set download_url = current_asset.asset_download_url | default('') %} reply_to_message_id: "{{ reply_to_message_id if reply_to_message_id > 0 else 0 }}"
{% set view_url = current_asset.asset_url | default('') %}
{% if asset_type == 'VIDEO' and playback_url | length > 0 %}
{{ playback_url }}
{% elif download_url | length > 0 %}
{{ download_url }}
{% else %}
{{ view_url }}
{% endif %}
# Use image/video asset templates for captions
caption: >
{% set tpl = message_asset_video_template if asset_type == 'VIDEO' else message_asset_image_template %}
{% set raw_date = current_asset.asset_created | default('') %}
{% set dt = raw_date | as_datetime(none) if raw_date | length > 0 else none %}
{% set formatted_date = dt.strftime(date_format) if dt else '' %}
{{ tpl | replace('{filename}', current_asset.asset_filename | default(''))
| replace('{description}', current_asset.asset_description | default(''))
| replace('{type}', current_asset.asset_type | default(''))
| replace('{created}', formatted_date)
| replace('{owner}', current_asset.asset_owner | default(''))
| replace('{url}', current_asset.asset_url | default(''))
| replace('{people}', (current_asset.people | default([])) | join(', '))
| replace('{album_name}', event_album_name) }}
# Send photo or video based on asset type # ---------------------------------------------------------
- choose: # OPTION 2: Send as Individual Messages (Default)
# Send as photo for IMAGE assets # ---------------------------------------------------------
- conditions: default:
- condition: template # Send each media item to this chat as reply to the notification
value_template: "{{ asset_type == 'IMAGE' }}" - repeat:
sequence: count: "{{ media_to_send | length }}"
- service: telegram_bot.send_photo sequence:
# Continue on error so one failed photo doesn't stop other media # Delay before each media item (acts as delay after notification for first item)
continue_on_error: true - choose:
data: - conditions:
target: "{{ current_chat_id }}" - condition: template
url: "{{ asset_url }}" value_template: "{{ telegram_media_delay > 0 }}"
caption: "{{ caption }}" sequence:
reply_to_message_id: > - delay:
{{ reply_to_message_id if reply_to_message_id > 0 else omit }} milliseconds: "{{ telegram_media_delay }}"
config_entry_id: >
{{ telegram_config_entry_id if telegram_config_entry_id | length > 0 else omit }}
# Send as video for VIDEO assets - variables:
- conditions: current_asset: "{{ media_to_send[repeat.index - 1] }}"
- condition: template asset_type: "{{ current_asset.asset_type | default('IMAGE') }}"
value_template: "{{ asset_type == 'VIDEO' }}" # URL preference: playback_url (videos) > download_url > asset_url
sequence: asset_url: >
- service: telegram_bot.send_video {% set playback_url = current_asset.asset_playback_url | default('') %}
# Continue on error so one large video doesn't stop other media {% set download_url = current_asset.asset_download_url | default('') %}
# Note: Telegram has 50 MB limit for videos sent via URL {% set view_url = current_asset.asset_url | default('') %}
continue_on_error: true {% if asset_type == 'VIDEO' and playback_url | length > 0 %}
data: {{ playback_url }}
target: "{{ current_chat_id }}" {% elif download_url | length > 0 %}
url: "{{ asset_url }}" {{ download_url }}
caption: "{{ caption }}" {% else %}
reply_to_message_id: > {{ view_url }}
{{ reply_to_message_id if reply_to_message_id > 0 else omit }} {% endif %}
config_entry_id: > # Use image/video asset templates for captions
{{ telegram_config_entry_id if telegram_config_entry_id | length > 0 else omit }} caption: >
{% set tpl = message_asset_video_template if asset_type == 'VIDEO' else message_asset_image_template %}
{% set raw_date = current_asset.asset_created | default('') %}
{% set dt = raw_date | as_datetime(none) if raw_date | length > 0 else none %}
{% set formatted_date = dt.strftime(date_format) if dt else '' %}
{{ tpl | replace('{filename}', current_asset.asset_filename | default(''))
| replace('{description}', current_asset.asset_description | default(''))
| replace('{type}', current_asset.asset_type | default(''))
| replace('{created}', formatted_date)
| replace('{owner}', current_asset.asset_owner | default(''))
| replace('{url}', current_asset.asset_url | default(''))
| replace('{people}', (current_asset.people | default([])) | join(', '))
| replace('{album_name}', event_album_name) }}
# Send photo or video based on asset type
- choose:
# Send as photo for IMAGE assets
- conditions:
- condition: template
value_template: "{{ asset_type == 'IMAGE' }}"
sequence:
- service: telegram_bot.send_photo
# Continue on error so one failed photo doesn't stop other media
continue_on_error: true
data:
target: "{{ current_chat_id }}"
url: "{{ asset_url }}"
caption: "{{ caption }}"
reply_to_message_id: >
{{ reply_to_message_id if reply_to_message_id > 0 else omit }}
config_entry_id: >
{{ telegram_config_entry_id if telegram_config_entry_id | length > 0 else omit }}
# Send as video for VIDEO assets
- conditions:
- condition: template
value_template: "{{ asset_type == 'VIDEO' }}"
sequence:
- service: telegram_bot.send_video
# Continue on error so one large video doesn't stop other media
# Note: Telegram has 50 MB limit for videos sent via URL
continue_on_error: true
data:
target: "{{ current_chat_id }}"
url: "{{ asset_url }}"
caption: "{{ caption }}"
reply_to_message_id: >
{{ reply_to_message_id if reply_to_message_id > 0 else omit }}
config_entry_id: >
{{ telegram_config_entry_id if telegram_config_entry_id | length > 0 else omit }}

View File

@@ -1,3 +1,3 @@
{ {
"version": "1.8.0" "version": "1.9.3"
} }