How To improve Telegram Bot commands by using args?

Hi, I am looking for a way to use args in Telegram Chat Bot in order to simplify my setup. Currently I am trying out a few things to get a feel for how it works, but I keep hitting a dead-end. For example, if I read out the event.data.args, they are surrounded by brackets i.e ‘args’ which makes it difficult to use them as template for an action.
Since I am researching on how I can accieve that, I found the idea of using AppDaemon, but I have no idea on how to code that and I am happy with getting along with homeassistant already. Also I have set up my ecosystem arround automations, so updating them is easier. My issue is that the commands that trigger the actions are quite lenghy in order to be unique and precise in what they do, i.e ‘/name.lights.on’ to switch on specific lights usualy used by that person. However those commands do get quite long sometimes and the keyboard function in Telegram shows them as ‘/name.li…s.on’, which makes it hard to know what I am doing.

I noticed that by having a space whithin the command allowes Telegram to place a second line of text in the same keyboard button. However, since that means the command has ended and the remaining text is an argument, it wouldn’t trigger the wanted automation.

So, what do I want? I am trying to get something like this to run:

/scene.turn_on name_scene

trigger:
  platform: event
  event_type: telegram_command
  event_data:
    command: '/scene.turn_on'
    
action:
  - service_template: scene.turn_on
    entity_id: scene.{{ trigger.event.data["args"] }}

Is something like this even possible? Currently it seems not to work due to those brackets that are added arround the argument. i.e the current entity_id looks like this:
scene.‘name_scene’
which obviously will not work.

I had a look at this topic here, but even tough I learned something valuable, it didn’t help me to accieve my goal:

Bump
I still have no clue and would love some ideas on how to get this to work

I’m not sure about the way to get that working with the YAML automation, but I can talk about a Node-red automation. This is the way I automate everything and I made absolutely the same automation as you are trying to do in a matter of minutes, everything is so simple with Node-red! :wink:
In fact for my whole Telegram automation I never had to configure anything in the configuration / automation files, all the telegram setup is done within Node-RED.

My Home Assistant is a Docker container running on my Raspberry Pi, then Node-red is running on another container.

The nodes I used are:

Let me walk you through how my flow works, I wanted the command to be “light {light} {state}”, for example “light kitchen on”, and then the bot responds with “Turned on the kitchen light”.

Here’s the whole flow:

The first node is activated when a message that starts with light is received, for that I used regular expression, to match every message starting with “light …”:

Then the content of the message is passed to the next node, and can be accessed with “msg.payload.text”.
The next node is a function that will get arguments, simply separating the message in a list of words, and then build the HA service to execute the action:

var text = msg.payload.text;
var splitted = text.split(' ')
var payload = {
    'domain': 'light',
    'service': 'turn_' + splitted[2],
    'data': {
        'entity_id': 'light.' + splitted[1]
    }
}
return {'payload': payload};

So the json service to call is passed to the next node, which sends the service to Home Assistant. The configuration is left empty, as it will be overwritten by the payload built just before.

The next node is a function building the message the bot will send, based on the arguments of the command.

var state = msg.payload.service.split('_')[1]
var light = msg.payload.data.entity_id.split('.')[1]
var text = 'Got it, turned '+ state + ' the '+ light + ' light.';

return {'payload': {'text':text}};

Finally the last node is a Telegram payload, configured with “sendMessage”, to send the previously built message.

Let me know if you need any help, I’d be very glad to help you to get it working! :slight_smile:

EDIT: so frustrating that as a new user I couldn’t use more than one image… I uploaded my flow so you can just copy paste to use it on your Node-red installation : https://flows.nodered.org/flow/a1dd5b6026545a9d5e2d097498480c9d

1 Like

I should have taken a look at my thread earlier, I absolutely did not expect such a helpful answer that soon!

Programming ain’t my strong suit, I sometimes wonder what witchcraft has written those automations I currently run! However this looks like a neat and highly versatile way of dealing with the problem I have. I surely will take a look at this software and how I can integrate it on my htpc (which also runs Homeassistant among other things).
Thank you for the hint and especialy the example for me to copy!

1 Like

Attempting to set up simple flow based on importing previous one described here.

I have debug turned on and after initial issues with setting everything up have gotten to this point and getting error:
3/18/2019, 4:31:21 PMnode: build servicefunction : (error)

“TypeError: Cannot read property ‘split’ of undefined”

Any help is appreciated.