universal_notifier icon indicating copy to clipboard operation
universal_notifier copied to clipboard

telegram_bot dispatch casts chat_id to str instead of int in alt_services loop (Sezione J)

Open jokerigno opened this issue 5 months ago • 4 comments

What version of Universal Notifier are you using?

dev_improvment branch, manifest reports 0.8.0 (issue reproduced and confirmed still present after updating to the v0.8.1 tag — see https://github.com/jumping2000/universal_notifier/issues/24#issuecomment-4723995712 on the now-closed duplicate)

What version of Home Assistant are you using?

Core 2026.5.4

The problem

When a target's channel uses an alt_services override that routes to telegram_bot.send_photo (instead of the per-target notify.xxx wrapper service), the call fails with:

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

Root cause in __init__.py, Sezione J (telegram_bot dispatch loop over notify_targets):

for chat_id in notify_targets:
    p = service_payload.copy()
    p[CONF_CHAT_ID] = str(chat_id)
    ...

chat_id is cast to str instead of int. This contradicts the comment a few lines above in Sezione H, which correctly does:

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]

Sezione H (single-target path) was fixed at some point, but Sezione J (the alt_services/multi-target loop) was not, so the bug still triggers whenever a channel's photo/document alt_service is used.

Proposed fix, with a safe fallback so a bad/unresolved 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: chat_id non numerico ignorato per Telegram: {chat_id!r}")
        continue
    ...

Applied locally on top of v0.8.1 and confirmed it resolves the validation error.

What is your channels configuration for Universal Notifier?

"Marco": {
  "alt_services": {
    "photo": { "service": "telegram_bot.send_photo" }
  },
  "default_media_player": "media_player.jarvis",
  "is_voice": false,
  "service": "notify.telegrammarco",
  "target": ""
}

(Marco's target is empty "" because routing is handled via the per-target notify.telegrammarco service; the photo alt_service routes directly to telegram_bot.send_photo instead, which is the path that triggers this bug.)

What actions did you take in the developer tools?

action: universal_notifier.send
data:
  targets:
    - Marco
  message: "Test foto"
  data:
    type: photo
    url: "https://example.com/test.jpg"

This fans out to the photo alt_service (telegram_bot.send_photo) for the Marco channel, hitting the Sezione J loop described above.

Anything in the logs? Paste it here!

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

(Originally reproduced on a weather-alert automation sending a photo via the Marco channel's photo alt_service; reconstructed here as a minimal manual repro per the template, since the original automation's live log lines have since rotated out.)


Re-filing per the issue template — this was originally reported in #24, which was closed for not following the template. Confirmed still present after testing against the v0.8.1 tag.

jokerigno avatar Jun 17 '26 05:06 jokerigno

@jokerigno you should configure the telegram targets with telegram_bot.send_message not notify, UniNotifier wasn't tested with telegram configuration like this:

action: notify.send_message data: message: "Reminder: Have you considered frogs?" entity_id: notify.telegram_bot_chat

jumping2000 avatar Jun 17 '26 10:06 jumping2000

Grazie per il chiarimento — confermo, il canale Marco era configurato con notify.telegrammarco (wrapper YAML platform: telegram) come servizio principale, invece di telegram_bot.send_message. Riconfigurato il canale per usare direttamente telegram_bot.send_message con target impostato al chat_id numerico (131079340), e l'alt_services.photo (telegram_bot.send_photo) ora eredita correttamente lo stesso target.

Testato dopo la riconfigurazione:

  • messaggio di testo via universal_notifier.send su Marco → nessun errore nei log
  • foto via alt_services.photo con URL reale → nessun errore, chat_id viene castato correttamente a [131079340] (int) nei log

Quindi confermo che il bug originale che avevo riportato era causato dalla mia configurazione non supportata (notify domain), non da Sezione J. Detto questo, il bug di Sezione J che ho descritto sopra (str(chat_id) invece di int(chat_id), in contrasto con Sezione H) resta comunque un problema di codice reale e indipendente — chiunque usi un alt_service telegram_bot.* con target multipli lo incontrerebbe, a prescindere dalla mia configurazione errata. La patch proposta resta valida.

jokerigno avatar Jun 17 '26 12:06 jokerigno

Grazie per il chiarimento — confermo, il canale Marco era configurato con notify.telegrammarco (wrapper YAML platform: telegram) come servizio principale, invece di telegram_bot.send_message. Riconfigurato il canale per usare direttamente telegram_bot.send_message con target impostato al chat_id numerico (131079340), e l'alt_services.photo (telegram_bot.send_photo) ora eredita correttamente lo stesso target.

Testato dopo la riconfigurazione:

  • messaggio di testo via universal_notifier.send su Marco → nessun errore nei log
  • foto via alt_services.photo con URL reale → nessun errore, chat_id viene castato correttamente a [131079340] (int) nei log

Quindi confermo che il bug originale che avevo riportato era causato dalla mia configurazione non supportata (notify domain), non da Sezione J. Detto questo, il bug di Sezione J che ho descritto sopra (str(chat_id) invece di int(chat_id), in contrasto con Sezione H) resta comunque un problema di codice reale e indipendente — chiunque usi un alt_service telegram_bot.* con target multipli lo incontrerebbe, a prescindere dalla mia configurazione errata. La patch proposta resta valida.

nella versione 0.8.2 ho rivisto e semplificato le sezioni H e J .

jumping2000 avatar Jun 17 '26 12:06 jumping2000

SOLVED

jumping2000 avatar Jun 17 '26 23:06 jumping2000