How to trigger Telegram chat bot automation using args?

I am having some problem triggering Telegram automation using args event data. For example, I have created this but it never triggers…

  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/switch'
      args: 'ground_floor'

Please help. Thanks.

I haven’t tried this yet, it’s on my to-do list, but if I’m reading the docs correctly, you don’t specify the args like that, you would use the args in your logic for the action:

trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/switch'
action:
  - service: homeassistant.turn_off
    data_template:
          [logic goes here...
                      if 'args' = 'ground_floor', entity_id = group.ground_floor_lights
                      if 'args' = first_floor, entity_id = group.first_floor_lights
                      else entity_id = group.all_lights]

Obviously with a compliant data template, not my random pseudo-code :wink:

Thanks. but according to https://home-assistant.io/components/telegram_bot/#event-triggering, args is a valid event_data. I assume I can use all those listed in there, not just command. Am I right?

I read that bit to mean that’s what HA receives from the telegram_bot, sort of like a JSON packet, then we can use the command as a trigger, but the other bits we have to use elsewhere. I may be wrong, but that’s how I understood it.

I have tested with user_id and it was successful. Example…

  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/hello'
      user_id: !secret telegram_chat_id_user1

Above automation will only trigger if the command is sent by user1. If other users send the same command, it won’t trigger.

So I can safely assume those variables listed in event_data can be used in trigger.

1 Like

I don’t have any idea why your original code doesn’t work in that case :thinking:

well, thanks anyway.

i suspect this is a bug. before i open up a new issue for this, i just wanna confirm with the community whether anyone has any luck on this.

Try with a list of args instead of a string. This could be better explained in the docs…

pieces = data['text'].split(' ')
event_data[ATTR_COMMAND] = pieces[0]
event_data[ATTR_ARGS] = pieces[1:]

Thanks. But how to do that in a trigger? Can you show me using the above example?

hi @masterkenobi, it works with:

trigger:
  platform: event
  event_type: telegram_command
  event_data:
    command: '/switch'
    args:
      - 'ground_floor'
3 Likes

ahh… you are right. thank you so much. you are wonderful. i tried to solve this for the past one week.

this should be in the documentation. It would save others some valuable time.

I feel bad about the lost time @masterkenobi, you’re right about the doc. I’ll try to fix it.

If you’re using the telegram_bot to trigger a lot of commands, I suggest you to use AppDaemon: you can make much more with much less code, but in python instead of yaml. Also, if you use inline_keyboards (which send a telegram_callback instead of a telegram_command), you can append buttons to messages to send commands with a press (I love it), like:

action:
  - service: telegram_bot.send_message
    data:
      title: "*Your bold title*"
      message: "The _Telegram message_"
      target: !secret telegram_bot_target
      disable_notification: true
      inline_keyboard:
        - Toggle ground floor:/switch ground_floor

I do generic service calls, show HA logs, run shell commands, create or cancel timers… In most cases, the telegram_bot is my #1 interface for HA. If you need an example of a bot handled with an AppDaemon app, a simple example is included here, and here is my bot handler. Mine is a bit messed up (and the app also handles iOS notification actions and more), but there are a lot of examples there.

4 Likes

Nice idea with adding user_id to the event_data trigger. I tried adding two user ids if the following formats to indicate it can be any of the two users but it did not work. Any suggestions?

user_id: 123456789, 987654321

user_id:
  - 123456789
  - 987654321