Telegram Callback error: Mapping not allowed here

Hello. I’ve been working on my hass setup for a couple of days now and it has been a real struggle so far. Slowly I’m getting there though.

I’m trying to set up a telegram bot that replies in the correct conversation:

- alias: 'Telegram bot start command'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/start'
  action:
    - service: telegram_bot.send_message
      data_template:
        target: '{{ trigger.event.data.chat_id }}'
        title: '*Command control*'
        message: 'What would you like me to do?'
        inline_keyboard:
          - 'Ping:/ping, Chat Info:/chatinfo'
          - 'Exit:/exit'

This is working fine. It’s responding to the command in the correct chat.

Then I tried to answer the ‘Ping’ with 'Pong

- alias: 'Telegram bot that reply pong to ping'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      command: '/ping'
  action:
    - service: telegram_bot.send_message
      data_template:
        message: 'Pong'

This is working. Except that it’s always replying with pong to the default chat-id.

So I tried adding target:'{{ trigger.event.data.chat_id }}'

This is my code now:

- alias: 'Telegram bot that reply pong to ping'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      command: '/ping'
  action:
    - service: telegram_bot.send_message
      data_template:
        target:'{{ trigger.event.data.chat_id }}'
        message: 'Pong'

But now I’m getting this error and I can’t figure out why that is:

Configuration invalid
Testing configuration at /config
ERROR:homeassistant.util.yaml:mapping values are not allowed here
in “/config/automation/telegram_bot.yaml”, line 29, column 16
Failed config
General Errors:
- mapping values are not allowed here
in “/config/automation/telegram_bot.yaml”, line 29, column 16

Successful config (partial)

Any help?

Thanks!

Telegram cant handle uneven number of _ marks.

This telegram ping - pong works for me:

  alias: 'telegram ping pong'                                                                                                                                           
  hide_entity: true                                                                                                                                                     
  trigger:                                                                                                                                                              
    - platform: event                                                                                                                                                   
      event_type: telegram_callback                                                                                                                                     
      event_data:                                                                                                                                                       
        data: '/ping'                                                                                                                                                   
  action:                                                                                                                                                               
    - service: telegram_bot.answer_callback_query                                                                                                                       
      data_template:                                                                                                                                                    
        callback_query_id: '{{ trigger.event.data.id }}'                                                                                                                
        message: >-                                                                                                                                                     
          Callback received from {{ trigger.event.data.from_first }}.                                                                                                   
          Message id: {{ trigger.event.data.message.message_id }}.                                                                                                      
          Data: {{ trigger.event.data.data }}                                                                                                                           
          {{now().strftime("%H:%M:%S %Y-%m-%d")}} Pong, Message from {{ trigger.event.data["user_id"] }}.                                                               
        show_alert: true
1 Like

Your code works. But what i want is for telegram to send a normal message. As if it’s just relying.

This should work.

- service: telegram_bot.answer_callback_query                               
  data_template:                                                            
    callback_query_id: '{{ trigger.event.data.id }}'                        
    message: 'Message'
1 Like

This Works.

However. Doesn’t matter wich button im pressing. I’s seems to execute the ping command even if I’m pressing another command

Lets see your telegram yaml. It’s hard to help without it. :wink:

configuration.yaml

telegram_bot:
  - platform: polling
    api_key: !secret telegram_api
    allowed_chat_ids:
      - !secret telegram_chat_id_glenn
      - !secret telegram_chat_id_group
      - !secret telegram_chat_id_ellen

Automation

- alias: 'Telegram bot start command'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/start'
  action:
    - service: telegram_bot.send_message
      data_template:
        target: '{{ trigger.event.data.chat_id }}'
        title: '*Command control*'
        message: 'What would you like me to do?'
        inline_keyboard:
          - 'Ping:/ping, Chat Info:/chatinfo'
          - 'Exit:/exit'

- alias: 'Telegram bot that reply pong to ping'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      command: '/ping'
  action:
    - service: telegram_bot.answer_callback_query
      data_template:
        callback_query_id: '{{ trigger.event.data.id }}'
        message: 'Pong'

As you can see I have three buttons. every button responds to the /ping command.

I tried adding automation for the other buttons but that resluts in evry automation being executed no matter what button is being pressed.

Here’s the code for those other automations:

- alias: 'Exit'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      command: '/exit'
  action:
    - service: telegram_bot.delete_message
      data_template:
        message_id: '{{ trigger.event.data.message.message_id }}'
        chat_id: '{{ trigger.event.data.chat_id }}'

 - alias: 'Chat info test'
   hide_entity: true
   trigger:
     platform: event
     event_type: telegram_callback
     event_data:
       command: '/chatinfo'
   action:
     - service: telegram_bot.answer_callback_query
       data_template:
         callback_query_id: '{{ trigger.event.data.id }}'
         message: 'Test Chatinfo'

That is strange. It has to be something with your ‘Telegram bot start command’.
This is my telegram /start command.

- id: '4005'
  alias: 'telegram control'
  hide_entity: true
  trigger:
    - platform: event
      event_type: telegram_command
      event_data:
        command: '/start'
  action:
    - service: notify.telegram
      data:
        message: 'Control panel'
        data:
          inline_keyboard:
            - 'Lights:/lights, Status:/status, Security:/security'
            - 'System:/system, Help:/help'

I would also change that chat info command to something like Chatinfo.

I need the telegram_bot.send_message command because in need the bot to reply in the correct chat.

I keep getting the same problem… Every button executes all commands

So if you press ping it also execute /chatinfo and /exit?

Try to replace

  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      command: '/ping' 

with

  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: '/ping'

Unfortunately still the same

I tried to recreate your setup with only two buttons. And this work. But i test it with only one user. Try it and see what happens.

- alias: 'Telegram bot start command'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/start'
  action:
    - service: telegram_bot.send_message
      data_template:
        target: '{{ trigger.event.data.chat_id }}'
        title: '*Command control*'
        message: 'What would you like me to do?'
        inline_keyboard:
          - 'Ping:/ping, Chat Info:/chatinfo'
#          - 'Exit:/exit'

- alias: 'Telegram bot that reply pong to ping'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: '/ping'
  action:
    - service: telegram_bot.send_message
      data_template:
        target: '{{ trigger.event.data.chat_id }}'
        message: 'Pong'

- alias: 'Chat info test'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_callback
    event_data:
      data: '/chatinfo'
  action:
    - service: telegram_bot.answer_callback_query
      data_template:
        callback_query_id: '{{ trigger.event.data.id }}'
        message: 'Test Chatinfo'
        show_alert: true

I have same problem. Platform: polling.

I’m also using telegram with pooling all the time. But that never happened to me. Try this code i posted before.

I found the mistake in my automations. It must be data: '/command' on the event_data section if I use telegram_callback. Thank you!

1 Like

Great. I’m glad you fix it.

1 Like

I’ll give this a try later today.

This also did it for me. I had one instance where i was using command instead of data

Thanks for the help @vladosam and @Yevgeniy

@vladosam i have a doubt… me and my wife using telegram.
so… if i want to make an automation only for her, and other for me…
what i would put on this line??

target: ‘{{ trigger.event.data.chat_id }}’

???