home-assistant / core

:house_with_garden: Open source home automation that puts local control and privacy first.
https://www.home-assistant.io
Apache License 2.0
71.63k stars 29.93k forks source link

telegram_bot commands not working anymore #54812

Closed SquaredCircleHunter closed 2 years ago

SquaredCircleHunter commented 3 years ago

The problem

Since some time, telegram command processing is not working anymore. Sending messages by the bot is still working fine.

The last succesful command was executed on August 5th. Since I usually follow releases quite closely, quite likely it is related to 2021.8 release -> https://github.com/home-assistant/core/pull/53690 and https://github.com/home-assistant/core/issues/53587

My config did not change.

I double checked the group ID, it is still the same.

See further for the logged error.

Setting telegram.ext.dispatcher and homeassistant.components.telegram_bot to debug did not result in extra logs.

No HA event is fired.

What is version of Home Assistant Core has the issue?

2021.8.7

What was the last working version of Home Assistant Core?

No response

What type of installation are you running?

Home Assistant OS

Integration causing the issue

telegram_bot

Link to integration documentation on our website

https://www.home-assistant.io/integrations/telegram_bot

Example YAML snippet

- alias: 'Telegram command - Open gate'
  trigger:
    - platform: event
      event_type: telegram_command
      event_data:
        command: '/p'
  condition:
      - condition: template
        value_template: "{{ trigger.event.data.args[0] == states.sensor.poort_pin.state }}"
  action:
    - service: telegram_bot.delete_message
      data_template:
        message_id: "{{ trigger.event.data.id }}"
        chat_id: "{{ trigger.event.data.chat_id }}"
    - service: script.knx_gate_pulse
    - service: telegram_bot.send_message
      data_template:
        message: "{{ trigger.event.data.from_first }} opened the gate"
        target: "{{ trigger.event.data.chat_id }}"
        disable_notification: True

Anything in the logs that might be useful for us?

Logger: telegram.ext.dispatcher
Source: components/telegram_bot/__init__.py:851
First occurred: 1:08:56 PM (1 occurrences)
Last logged: 1:08:56 PM

An uncaught error was raised while handling the error.
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 448, in process_update
    self.dispatch_error(update, exc)
  File "/usr/local/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 658, in dispatch_error
    callback(update, context)
  File "/usr/src/homeassistant/homeassistant/components/telegram_bot/polling.py", line 38, in process_error
    raise context.error
  File "/usr/local/lib/python3.9/site-packages/telegram/ext/dispatcher.py", line 432, in process_update
    handler.handle_update(update, self, check, context)
  File "/usr/src/homeassistant/homeassistant/components/telegram_bot/polling.py", line 70, in handle_update
    return self.callback(update, context)
  File "/usr/src/homeassistant/homeassistant/components/telegram_bot/polling.py", line 99, in process_update
    self.process_message(update.to_dict())
  File "/usr/src/homeassistant/homeassistant/components/telegram_bot/__init__.py", line 906, in process_message
    message_ok, event_data = self._get_message_data(data)
  File "/usr/src/homeassistant/homeassistant/components/telegram_bot/__init__.py", line 851, in _get_message_data
    and msg_data["message"]["chat"].get("id") not in self.allowed_chat_ids
KeyError: 'message'

Additional information

No response

probot-home-assistant[bot] commented 3 years ago

telegram_bot documentation telegram_bot source (message by IssueLinks)

SquaredCircleHunter commented 3 years ago

it looks like it's not safe to assume msg_data["message"] exists

maybe the code changed by #53690 needs to be kept backward compatible. In pseudocode:

if ( msg_data["from"].get("id") not in self.allowed_chat_ids and msg_data["chat"].get("id") not in self.allowed_chat_ids and ( msg_data["message"] is not defined OR msg_data["message"]["chat"].get("id") not in self.allowed_chat_ids ) )

SquaredCircleHunter commented 3 years ago

Please let me know if I can somehow get the telegram response in the logs. Wireshark is not an easy option in my network.

bessarabov commented 3 years ago

I think that we should revert the change that was done in https://github.com/home-assistant/core/pull/53690/files

That change broke how telegram works with in group chats.

Before that change everything woked as expected — when the group chat id is in allowed_chat_ids then all persons from that chat can send commands to telegram bot. But with that change this is not working anymore.

SquaredCircleHunter commented 3 years ago

Makes sense.

Will this be picked up by somebody 'automatically' or do I need to contact somebody directly?

frenck commented 3 years ago

@SquaredCircleHunter Neither of those. Someone has to pick it up. This is an open-source project, powered by volunteers. If you want to pick it up, go ahead. Else it will be sitting here until someone else does.

SquaredCircleHunter commented 3 years ago

Ok, will see what I can do, will need to dive into HA change & release mgmt first.