Telegram actionable message with picture?

Is it possible to send a telegram actionable message with a picture? It seems it should be possible from the docs, but I cannot get it to work. Here’s what I’ve tried:

  action:
- service: telegram_bot.send_message
  data_template:
    title: "Garage Door Open"
    message:  "{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}! Do you want to close it?"
    inline_keyboard:
      - "Yes:/closegarage_double, No:/do_nothing"
    data:
      photo:
        - url: !secret hik_garage_pic

Currently, I’m working around this issue by sending to notifications

  action:
- service: telegram_bot.send_message
  data_template:
    title: "Garage Door Open"
    message:  "{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}! Do you want to close it?"
    inline_keyboard:
      - "Yes:/closegarage_double, No:/do_nothing"
- service: notify.telegram_jon
  data:
    title: "garage door open2"
    message: "2nd message"
    data:
      photo:
        - url: !secret hik_garage_pic

Here is what I do and it works like a charm.

  - service: notify.telegram_argy
    data:
      message: 'This is a message. {{states.sensor.balcony_temperature.state}}'
      data:
        inline_keyboard:
          - 'Yes:/answeryes, No:/answerno'
        photo:
          file: /home/homeassistant/.homeassistant/www/images/snapshot.jpg
          caption: 'This is the caption. {{states.sensor.balcony_temperature.state}}'

Take a note: The message doesnt show up anywhere, but you cannot ommit it… The caption is what you see in the telegram. Also, you dont have to use data_template. It works without it.

Another note: use the correct path and make sure you have whitelisted it. Take a look at the bottom of this page:

EDIT: Or use the url in your case (which I didnt notice :stuck_out_tongue: )

2 Likes

Awesome, thanks for the reply! I’ll try this asap when I get home later!

Slightly off topic but kinda not :slight_smile:, does anyone know how to include emojis in a Telegram message? I’ve tried countless methods based on some Internet searching but nothing I do works.

If you use AppDaemon you can do so using the emoji package. In your imports, include the following:

from emoji import emojize

And then when you are calling the telegram_bot service to send your message, structure it like the following example:

self.call_service("telegram_bot/send_message", message = emojize(":running: Mode set to Away", use_aliases = True), target = target_device)

You can use as many emojis inline as you wish, in this case, I have the :running: emoji in the message. Acceptable emojis are here:

Are you sure that I don’t need data instead of data_template? I get the following error when I try to use variables within the automation.

Error sending file: Can't parse entities: can't find end of the entity starting at byte offset 64. Args: (635327867, <_io.BytesIO object at 0x7f3d16d7eca8>), kwargs: {'caption': '{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}. Close?', 'parse_mode': 'Markdown', 'disable_notification': False, 'disable_web_page_preview': None, 'reply_to_message_id': None, 'reply_markup': <telegram.inline.inlinekeyboardmarkup.InlineKeyboardMarkup object at 0x7f3d13481320>, 'timeout': None}

Here’s the action that triggers that error:

- service: notify.telegram_jon
  data:
    message: 'placeholder'
    data:
      inline_keyboard:
        - "Yes:/closegarage_single, No:/do_nothing"
      photo:
        - url: !secret hik_garage_pic
          caption: "{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}. Close?"

Versus, if I use this, it’ll work as expected:

- service: notify.telegram_jon
  data:
    message: 'placeholder'
    data:
      inline_keyboard:
        - "Yes:/closegarage_single, No:/do_nothing"
      photo:
        - url: !secret hik_garage_pic
          caption: "Single garage open. Close?"

Well, I am not really sure how triggers work in a template, so I wont confuse you more with this, but I will just assume it needs to get the trigger from somewhere, and there is nothing in your action that specifies it… Nevertheless, I am only making assumptions here… Cant say for sure… Maybe someone else can help you here.

BUT, if you just use a state template like my example above, you will see it is working. So, templating works fine without using data_template.

EDIT:
This guy here just made the real question. Follow his thread to get an answer. I think this is what you are looking for.

1 Like

Thanks so much! Here’s what ended up working for me:

- service: notify.telegram_jon
  data_template:
    message: 'placeholder'
    data:
      inline_keyboard:
        - "Yes:/closegarage_single, No:/do_nothing"
      photo:
        - url: !secret hik_garage_pic
          caption: "{{ trigger.to_state.attributes.friendly_name }} is {{ trigger.to_state.state }}. Close?"
1 Like