Polling Telegram Bot not working...?

Hey there!

I’ve just tried to configure the new telegram bot component (polling) on two different hass instances, and on both its not working.
I’m using Docker on both systems, the notify component is working fine.
Maybe I do have a configuration issue?

notify:
  - name: telegrambot_matthias
    platform: telegram
    api_key: '12345678901234567890'
    chat_id: '987654321'

telegram_bot:
      platform: polling
      api_key: '12345678901234567890'
      allowed_chat_ids:
        - '987654321'

automation:
    - alias: telegrambot_ping
        hide_entity: true
        trigger:
          - platform: event
            event_type: telegram.command
            event_data:
              command: '/ping'
        action:
          - service: notify.telegrambot_matthias
            data:
              message: 'pong'

As said, the telegram notify component (notify.telegrambot_matthias) works fine as expected.
The HASS logs don’t tell me any error message, even if i set logging for the bot component to the debug level.

Maybe someone here got the clue :slight_smile:

Cheers!
Matthias

I have a same problem, try

trigger:
      - platform: event
        event_type: telegram_command
        event_data:
          command: '/ping'
1 Like

As @Lex512 indicates, the event type is not correct in the config, due to incorrect docs. Fix incoming:

I’ve changed my config now to:

     - alias: telegram_ping
        hide_entity: true
        trigger:
          - platform: event
            event_type: telegram_command
            event_data:
              command: '/ping'
        action:
          - service: notify.telegrambot_matthias
            data:
              message: 'pong'

But its still not working. Log doesn’t say anything helpful.

I made the same changes and also still have no reaction from HASS on my commands. (Notifications are working fine)
What is the polling interval ?
How can I help to debug this further ?

You can debug this running your homeassistant by “hass” command in terminal
And found in log

INFO (MainThread) [homeassistant.core] Bus:Handling <Event telegram_command[L]: command=/status, args=[], user_id=169839967, from_last=, from_first=Alex>

I have this problem after update to version 0.43

Here is my config for control alarm via telegram for ver 0.43

Maybe can be helpful

telegram_bot:
  platform: polling
  api_key: ##
  allowed_chat_ids:
   - #


notify:
  - name: bot
    platform: telegram
    api_key: ##
    chat_id: #



automation:
 - alias: 'telegram bot that reply pong to ping'
   hide_entity: true
   trigger:
     platform: event
     event_type: telegram_command
     event_data:
       command: '/start'
   action:
     service: notify.bot
     data:
       message: 'commands'
       data:
         keyboard:
           - '/ping, /alarm'

 - alias: 'alarm'
   hide_entity: true
   trigger:
     platform: event
     event_type: telegram_command
     event_data:
       command: '/alarm'
   action:
     service: notify.bot
     data:
       message: 'commands'
       data:
          keyboard:
            - '/status'
            - '/arm, /unarm'
            - '/siren'

Thanks for you help. My configuration is very similar. But I see no reaction in the log on the commandline after I post a command like /start in the chat with my bot. (I see a lot of other events. Also no network activity to api.telegram-org after the initial start of HASS.

I see two standig connection to Telegram:
python.exe 1700 TCP z83 55676 149.154.167.199 https ESTABLISHED
python.exe 1700 TCP z83 56748 149.154.167.199 https ESTABLISHED

I see all my messages and the same ID as configured if I use
https://api.telegram.org/bot/getUpdates

So my question remains: If this is polling Telegram, how often that should happen ?
I revoked the API key an use now a new one. Notifications work fine, but I found that in the log during stop of HASS:
e[31m17-04-23 20:23:15 ERROR (Dummy-3) [homeassistant.core] Error doing job: Task exception was never retrievede[0m Traceback (most recent call last): File "C:\Users\ABC\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\components\telegram_bot\polling.py", line 119, in check_incoming yield from self.handle() File "C:\Users\ABC\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\components\telegram_bot\polling.py", line 108, in handle self.process_message(update) File "C:\Users\ABC\AppData\Local\Programs\Python\Python36\lib\site-packages\homeassistant\components\telegram_bot\__init__.py", line 118, in process_message ATTR_FROM_LAST: data['from']['last_name']} KeyError: 'last_name'e[0m Exception ignored in: <bound method _ProactorBasePipeTransport.__del__ of <_ProactorSocketTransport closing fd=-1 read=<_OverlappedFuture cancelled>>>

My Telegram Account that I used to send messages to the bot has NO last name. I now set one and send a new command. But the error persists since in getUpdates are also old messages without a last name set. So I wait 24h and wait I without old messages it works.

Please don’t post your api key, or chat id. That’s sensitive information. You should probably get a new api key now, if that was your real key.

The telegram polling implementation uses long polling with a timeout of 10 seconds. As soon as a message is sent the update method should return with the message, basically. As soon as the 10 second timeout is passed without incoming message or after a message another long polling is done.

Edit: The KeyError is a bug and should be fixed, please create an issue on github, with the stack trace. We should handle cases without last name.

Same here. I can’t see any incoming messages as events logged by hass. The only thing I can see in the logs is the initialization of the telegram_bot polling component.

I can confirm that once receiving a message from a user breaks the polling and HASS must be restarted. No message should be waiting in the queue with that same problem. Wait 24h or delete messages with calling /getUpdates?offset=higher number than last update message ID

Can you fix this issue in next release ? Perhaps check all fields (like missing first name …)

I am having this error in my log…

ERROR (MainThread) [homeassistant.components.telegram_bot.polling] Client connection error

I just updated to 0.43.1 and its working now on both of my HASS instances. Great!

But I also found a way to break it again: at the moment, as CamFlyerCH already said, the meta fields like First Name and Last Name are currently not being checked for content. So if I remove my Last Name in the telegram settings, this breaks HASS.
So a simple “if” should fix this :slight_smile:

https://github.com/home-assistant/home-assistant/pull/7281

1 Like

I’m on 0.43.2 telegram webhook does not work. this is the error I get

17-04-28 22:57:57 ERROR (MainThread) [homeassistant.components.telegram_bot] Incoming message does not have required data.
17-04-28 22:57:57 ERROR (MainThread) [aiohttp.server] Error handling request
Traceback (most recent call last):
  File "C:\Users\mohammed\AppData\Roaming\.homeassistant\deps\aiohttp\web_protocol.py", line 417, in start
    resp = yield from self._request_handler(request)
  File "C:\Users\mohammed\AppData\Roaming\.homeassistant\deps\aiohttp\web.py", line 289, in _handle
    resp = yield from handler(request)
  File "C:\Users\mohammed\AppData\Local\Programs\Python\Python35\lib\asyncio\coroutines.py", line 209, in coro
    res = yield from res
  File "C:\Users\mohammed\AppData\Local\Programs\Python\Python35\lib\asyncio\coroutines.py", line 209, in coro
    res = yield from res
  File "C:\Users\mohammed\AppData\Local\Programs\Python\Python35\lib\site-packages\homeassistant\components\http\ban.py", line 58, in ban_middleware_handler
    return (yield from handler(request))
  File "C:\Users\mohammed\AppData\Local\Programs\Python\Python35\lib\site-packages\homeassistant\components\http\__init__.py", line 425, in handle
    result = yield from result
  File "C:\Users\mohammed\AppData\Local\Programs\Python\Python35\lib\site-packages\homeassistant\components\telegram_bot\webhooks.py", line 95, in post
    if not self.process_message(data):
  File "C:\Users\mohammed\AppData\Local\Programs\Python\Python35\lib\site-packages\homeassistant\components\telegram_bot\__init__.py", line 116, in process_message
    ATTR_FROM_LAST: data['from']['last_name']}
KeyError: 'last_name'

Config

notify:  
  - name: telegram
    platform: telegram
    api_key: xxxx
    chat_id: xxxx
        
telegram_bot:
    platform: webhooks
    api_key : xxx
    allowed_chat_ids:
        -  xxx

Automation

- alias: 'telegram bot reply car LOC'
  hide_entity: true
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/Car'
  action:
    - service: script.car_finder_ashu_notify

The fix is only merged with dev branch and was not released in 0.43.2.

ok thanks, i will wait till next available update… hope it will be merged to next update

Still not working for me. When you did your investigations, did you manage to get anything fruitful out of the logs?

Same here, I manually applied the changes from the fix but polling isnt working after some time. No logs even if i set the telegram_bot component to debug or info. Telegram notification is still working though. My sender has first and last name so Im not sure if this is a different issue.