Telegram Bot Group Chat to control garden lights (with inline_keyboard)

Hi,
i was looking for a solution to allow my neighbour control of some garden lights (“Dr. Zzs Holiday Lights”) which are visible from both our gardens.
Allowing him direct access to my HA was no option. :wink:

The following topics allowed me to set up my solution, thanks for sharing guys!

Now we are using a Telegram Group Chat with several inline_keyboards which allow turning the lights on and off and to change color and brightness.

Since i’ve got no coding skills, my solution is based only on automations - sorry for that. Please feel free to suggest smarter solutions. :wink:

Find the automations here: https://pastebin.com/mi42S4Y4

Telegram keyboard - Control the lights (On, Off, Sub-Menu Color, Sub-Menu Brightness)
led1

Telegram keyboard - Change color to defined sets for red, green, blue
led2

Telegram keyboard - Change brightness
led3

2 Likes

Did you say you had this working for groups? I tried to set inline commands last year when I first set up telegram. Was there anything special you did? Or has the telegram component been updated so it works out of the box?

Hi,
it was my first attempt, so i don’t know if something has been changed.
I had to add the chat id of the group and the id’s of the users to “allowed_chat_ids”, after that it was up and running.
Hope this helps!

#
# Telegram
#
telegram_bot:
  platform: polling
  api_key: !secret telegram_api_key
  allowed_chat_ids: 
  - !secret telegram_chat_id_MW-android
  - !secret telegram_chat_id_MW-ios
  - !secret telegram_chat_id_VR-android
  - !secret telegram_chat_id_group-LED
2 Likes

Does everyone get the bots responses when someone sends a command?

E.g. if you send /start will your neighbour also get the response of the command or does the bot address each person individually?

In my case, everyone receives the response.

I can see how that might be helpful in some cases but I don’t want to get the messages every time someone is using the bot. Instead, replies should only go to the user who triggered the bot (unless of course it’s in a group chat, then its different).

Big thanks goes to @Ciquattro for sharing his template for the inline keyboard for telegram. I find it really interesting with the randomised responses, which makes this more fun :slight_smile:

So the trick is using '{{ trigger.event.data.id }}'. That way the bot only replies to the user that used the command. In case anyone is interested I’ve included a preview from my automations and also here you can find @Ciquattro’s automations.yaml from his GitHub page.

image

#Starting command with randomised reply and inline keyboard with what to do next
- id: telegram-start
  alias: Telegram Start Command
  initial_state: 'on'
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: /start
  action:
  - service: telegram_bot.send_message
    data_template:
      target: '{{ trigger.event.data.user_id }}'
      message: "{{ [ \"Hello!\", \"Welcome Back!\", \"Yes Sir!\", \"There you are\
        \ Again!\" ] | random }}       \n"
      keyboard:
        - /start, /settings
#Automation for settings menu
- id: telegram-settings
  alias: Telegram - Settings
  initial_state: 'on'
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: /settings
  action:
  - service: telegram_bot.send_message
    data_template:
      target: '{{ trigger.event.data.user_id }}'
      message: "{{ [ \"Control Panel\", \"Settings\", \"Here's your options\", \"\
        What would you want me to do?\", \"Here you go\" ] | random }}  \
        \     \n"
      inline_keyboard:
        - Dehumidifier ON:/dehumi_on, Dehumidifier OFF:/dehumi_off
        - All lights OFF:/all_lights_off, All lights ON:/all_lights_on
        - Who is home? :/home_status
#One of the options that show up in the settings menu
- alias: Telegram - All Lights OFF
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: /all_lights_off
  action:
  - service: telegram_bot.answer_callback_query
    data_template:
      callback_query_id: '{{ trigger.event.data.id }}'
      message: All lights are now OFF
  - service: homeassistant.turn_off
    data:
      entity_id: group.all_lights
  - service: switch.turn_off
    data: 
      entity_id: switch.tp_link
4 Likes

Yes it’s useful the callback query id when you have more people on the same telegram group.

callback_query_id: '{{ trigger.event.data.id }}'

thanks for sharing :slight_smile:

1 Like

Very helpful, thanks for sharing!

1 Like