Incoming notify to telegram bot loses the "image" data

Hello fellow Home Assistant fanatics!

I am setting up Scrypted NVR, which has a homeassistant plugin supporting sending notifications to Home Assistant. This works for Home Assistant Companion notifications. I receive the title, message and the image in the notification on my devices.

I have also setup a telegram bot, however when a notification is sent to the bot, I get the text and no image. It seems the bot doesn’t receive the “image:” data from the notification json.

For example, when I send a test, Home Assistant receives a notification, with an image payload

DEBUG (MainThread) [homeassistant.core] Bus:Handling
  <Event call_service[L]:
    domain=notify,
    service=lazynooblet,
    service_data=title=Scrypted Notification,
      message=This is a message from Scrypted,
      data=priority=high,
      image=https://home.scrypted.app/_punch/web_hi_res_512.png>

But the image is stripped when sent to the telegram bot

DEBUG (MainThread) [homeassistant.components.telegram_bot]
  New telegram message send_message: {
    'target': [1######0],
    'title': 'Scrypted Notification',
    'message': 'This is a message from Scrypted'
  }

Where should I target my efforts to resolve this?

Relevant telegram configurations.yaml section:

telegram_bot:
  - platform: polling
    api_key: "6#########2:A##################################4"
    allowed_chat_ids:
      - 1######0 # lazynooblet

notify:
  - platform: telegram
    name: "lazynooblet"
    chat_id: 1######0

For me this works:

    - service: notify.robert
      data:
        message: 'Message'
        data:
          photo:
            - url: http://192.168.1.200:3000/api/storage/matches/snapshot.jpg
              caption: 'Caption'

That would grab the current snapshot, but the image from Scrypted NVR is a cropped photo of the object that triggered the notification. This is generated and sent as part of the notification.

Works fine on the companion app, but is literally lost in translation when sent to Telegram Bot.

So if you replace my example data with your data it does not work?

    - service: notify.lazynooblet
      data:
        message: 'Message'
        data:
          photo:
            - url: https://home.scrypted.app/_punch/web_hi_res_512.png
              caption: 'Caption'

Sorry maybe I misunderstood. So your Scrypted NVR HA plugin is sending the notification? If so can you change the code so that the notification sent to Telegram has a different format? Because the message format of those services is different and that is why the image does not appear in Telegram.
If you cannot change the plugin you could write a HA automation that is triggered by the HA notification and sends out the telegram notification from HA using code similar to the one I posted above.

I think thats it. I was just writing the same. Having looked at the Companion Api Reference, I can see the format Scrypted NVR is using matches that.

However Telegram needs a different format, so I can’t really use their HA plugin for Telegram, or anything else for that matter.

Its a shame HA doesn’t have a unified notification api.

Am I able to pull the “image” property that was sent and put it into the notify service YAML?

If you really need it, it can be done. The companion app can trigger HA when it receives a certain notification. A HA automation can then send out the Telegram notification. It would be not too complicated but of course some yaml coding needs to be done.

That’s the key question. I’m not perfectly sure. I’ll have a look.

I couldn’t find it yet. I believe its better for you to create a new topic and specifically ask for whether or not it is possible to extract the image name from a received notification that has an image attached. Maybe there is someone who did this and therefore knows how to do it.

What just came to my mind. You have the image url stored in the call_service event above,

So you should be able to create an automation that waits for that trigger and then pulls the url. I have not tried but something like this:

  trigger:
    platform: event
    event_type: call_service
    event_data:
        domain: notify
        service: lazynooblet
        service_data:
          title: Scrypted Notification
  action:
    - service: notify.lazynooblet
      data:
        message: 'Message'
        data:
          photo:
            - url: '{{ trigger.event.data.image }}' # Note that is the key. I'm not sure if this works. You have to try.
              caption: 'Caption'




Holy crap that looks promising, thank you, I’m going to play

I got this to work, thank you for your pointers.

I have a telegram bot

telegram_bot:
  - platform: polling
    api_key: "0000000000:abcdef123456"
    allowed_chat_ids:
      - 000000000

With a telegram notify object, and an extra dummy notify object

notify:
  - platform: group
    name: nvr_notify
    services: []
  - platform: telegram
    name: telegram_lazynooblet
    chat_id: 000000000

Scrypted NVR is configured to notify the nvr_notify object. This allows me to create a trigger from the call_service event like you suggested.

- id: nvr_notify_trigger
  alias: NVR Notify Event
  description: converts incoming notification from Scrypted NVR to suitable Telegram
    notification
  trigger:
  - platform: event
    event_type: call_service
    event_data:
      domain: notify
      service: nvr_notify
  condition: []
  action:
  - service: notify.telegram_lazynooblet
    data_template:
      message: ''
      data:
        photo:
        - url: '{{ trigger.event.data.service_data.data.image }}'
          caption: "\\[{{ trigger.event.data.service_data.title }}] detected \\[{{ trigger.event.data.service_data.message | replace(' Detected. Tap to view.', '') }}] [Tap to view]({{ site_url }}{{ trigger.event.data.service_data.data.url }})"
  trigger_variables:
    site_url: https://ha.example.com

This results in a telegram message with the image
2023-09-21 17_31_15-nooblet_HomeAssistant

I’ll be able to use this method to convert the Scrypted notification to other types of notify, like Notifications for Android TV.

1 Like