universal_notifier icon indicating copy to clipboard operation
universal_notifier copied to clipboard

telegram_bot dispatch casts chat_id to str instead of int, breaking schema validation

Open jokerigno opened this issue 5 months ago • 2 comments

Bug

In __init__.py, Sezione J (telegram_bot dispatch loop), the chat_id is cast to str instead of int:

# __init__.py, ~line 503-505
for chat_id in notify_targets:
    p = service_payload.copy()
    p[CONF_CHAT_ID] = str(chat_id)
    _LOGGER.debug(f"UniNotifier: Sezione J, Telegram to chat_id={chat_id} payload {p}")
    tasks.append(hass.services.async_call(srv_domain, srv_name, p))

This directly contradicts the comment a few lines above in Sezione H, which correctly does int(x):

# __init__.py, ~line 459-460
elif srv_domain == "telegram_bot":
    # Telegram requires int chat_id
    if isinstance(conf_target_value, list):
        conf_target_value = [int(x) for x in conf_target_value]

When a target's channel uses an alt_services override that routes to telegram_bot.send_photo/send_message directly (instead of the per-target notify.xxx wrapper service), HA's telegram_bot service schema rejects the payload:

Error executing script. Invalid data for repeat at pos 2: expected int @ data['chat_id'][0]

Reproduced on our instance via a universal_notifier.send call with data: {type: photo, url: ...} targeting a channel with a photo alt_service pointing at telegram_bot.send_photo.

Fix

Cast to int instead of str, with a safe fallback for non-numeric values (e.g. unresolved aliases) so a bad target doesn't abort the whole dispatch loop:

for chat_id in notify_targets:
    p = service_payload.copy()
    try:
        p[CONF_CHAT_ID] = int(chat_id)
    except (TypeError, ValueError):
        _LOGGER.warning(f"UniNotifier: Sezione J, chat_id non numerico ignorato per Telegram: {chat_id!r}")
        continue
    _LOGGER.debug(f"UniNotifier: Sezione J, Telegram to chat_id={chat_id} payload {p}")
    tasks.append(hass.services.async_call(srv_domain, srv_name, p))

We've applied this locally and confirmed it resolves the validation error (tested on dev_improvment).

Context

Found while investigating a recurring automation failure (Invalid data for ... expected int @ data['chat_id'][0]) on a weather-alert automation that sends a photo via a target whose primary service is notify.xxx but whose photo alt_service is telegram_bot.send_photo. One open question we couldn't fully confirm by static reading: for our specific channel the top-level target field is empty (""), which by the Sezione J logic should produce an empty notify_targets list and skip the loop entirely — yet the error fired, so there may be a secondary path or stale value worth double-checking. Happy to provide a debug-log capture if useful.

Tested against dev_improvment branch (v0.8.0).

jokerigno avatar Jun 16 '26 14:06 jokerigno

Still not fixed in v0.8.1 (released 2026-06-14).

In v0.8.1 __init__.py, Sezione J still has:

p[CONF_CHAT_ID] = str(chat_id)

Note: Sezione H (single-target path) was improved in v0.8.1 — it now correctly casts conf_target_value to int before setting CONF_CHAT_ID in service_payload. But the alt_services loop (Sezione J) that handles notify_targets for telegram_bot still casts to str, which breaks telegram_bot.send_photo and similar services that go through that path.

I am running a local patch that replaces str(chat_id) with:

try:
    p[CONF_CHAT_ID] = int(chat_id)
except (TypeError, ValueError):
    _LOGGER.warning(f"UniNotifier: chat_id non numerico ignorato per Telegram: {chat_id!r}")
    continue

jokerigno avatar Jun 16 '26 21:06 jokerigno

Please follow the issue template to help me understand your issue better.

jumping2000 avatar Jun 16 '26 23:06 jumping2000