Telegram bot notifications and communication

Thank you for sharing, just when I thought the sleepless nights had ended :joy:

1 Like

The first paragraph on the telegram page gives a link on how to set up your bot.

The simplest way IMAO was:
FYI: I’m using Android.

  1. Install app on smart phone.
  2. Start a chat (click pencil icon buttom right).
  3. Search for BotFather image
  4. Clicks on START (buttom of screen), you will get a reply with options.
  5. Click on /newbot
  6. Follow replies from BotFather
  • choose a name (xxxxx)
  • choose a username (xxxxx_bot)
  1. Copy your token from BotFather reply.
  2. Start a new chat as in step 2 above.
  3. Search for IDBot image
  4. Clicks on START (buttom of screen), you will get a reply with options.
  5. Click on /getid
  6. Copy your ID from IDBot reply.
  7. Setup / Config Telgram in HASS from >
    "To enable Telegram notifications in your installation, add the following to your "
  8. Done.
2 Likes

I tried that and it seemed all very complicated and not working because it was going on about connecting to MY bot. Its all good, I basically did what @Coolie1101 has since posted

Yeah, the docs make it sound much more complicated than it is, I’ll try and fix that at some point.

‘chat’ id is actually not a good name for it either as it implies it is a unique id of a conversation.

It should really be called ‘user id’, and is the id code of each user you want to interact with via bot.

From the telegram app on your phone, just click the new conversation button, and in the search box type @myidbot and send it the message, it will return a key for your telegram account and this is the number you need. Same for any other user you want to converse with.

Hope this helps.

I had the situation where I had the same problem (see - Telegram config) finding the chat_id (which I agree is a confusing label). I had set everything up except the chat_id for which I had put a dummy placeholder in my config. Obviously it didn’t work but the HA error returned the actual chat_id I was looking for. I don’t know if that is expected behaviour or as a result of something else I had done in the hours I spent trying to work it out, but if it is normal behaviour it might be an alternative to suggest in the docs?

I definitely think that the docs should reference the very easy way to get the chat_ID using @mybotid like you say, and also to explain that it isnt an ID specific to THAT chat, but the user (again as you said) because that was what had me confused. I thought it had to be an ID specific to me chatting with my HA Bot.

All good now though, thanks.

2 Likes

Actually, I think I spoke too soon. I just tried to send my first message from HA via Telegram and got nothing to my phone…I got this in my error log: (chat_ID removed)

-04-28 06:49:31 ERROR (SyncWorker_19) [homeassistant.components.telegram_bot] Error sending message: Unauthorized. Args: (5xxxxxx, ‘The front door light has gone unavailable’), kwargs: {‘parse_mode’: ‘Markdown’, ‘disable_notification’: False, ‘disable_web_page_preview’: None, ‘reply_to_message_id’: None, ‘reply_markup’: None, ‘timeout’: None}
2018-04-28 06:49:36 ERROR (MainThread) [homeassistant.components.telegram_bot.polling] wrong status 401

and now I see that the log is FULL of the wrong status 401 error from when I first coded it in.

‘Unauthorised’ suggests that your api key is incorrect?

its been typed in directly from what I got back from the @botfather

OK, and have you started the conversation with the bot?

What’s your config for telegram_bot and notify, and what’s the automation that is causing the error?

I think i found my issue… just restarting HA after finding a missing space in my telegram yaml

1 Like

unfortunately that didn’t work… I checked my api_key again and it looks fine, just a slight concern that a 0 could be O…

my setup is in a package called telegram.yaml

telegram_bot:
  - platform: polling
    api_key: 528xxxxxx:Axxxxk1Yr92xxxU1txnrxxxxasIqV0oxxxI
      - 536xxxxxx


notify:
  - name: Telegram_DavesS7
    platform: telegram
    chat_id: 536xxxxxx

the automation is an existing one that works with Pushbullet to which I added the Telegram service as well for testing. I’ve just been triggering it manually.

notify_entry_light_unavailable.yaml

alias: Notify if entry light unavailable
initial_state: 'on'
trigger:
  - platform: state
    entity_id: light.front_door_light
    to: 'unavailable'
    for:
      seconds: 22
action:
  - service: notify.Pushbullet
    data:
      message: 'The front door light has gone unavailable'
      
  - service: notify.telegram_davesS7
    data:
      message: 'The front door light has gone unavailable'

I have also tried with the api_key and chat_id being within single quotes but this makes no difference to the constant error:

image

Anyone have any ideas?

telegram_bot:
  - platform: polling
    api_key: ABCDEFGHJKLMNOPQRSTUVXYZ
    allowed_chat_ids:
      - CHAT_ID_1

Docs definitely need some improvements.

It drove me crazy for a week to figure out how to get Telegram setup.

Hmm. not sure what happened with the copy / paste there. I actually do have:

telegram_bot:
  - platform: polling
    api_key: xxxxxxxxx
    allowed_chat_ids:
      - xxxxxxx

Do you have the app setup on your phone?
Could you try sending a message to HA, you would need to setup an automation to test.

automation:
  - alias: Telegram Test
    initial_state: true
    trigger:
      platform: event
      event_type: telegram_command
      event_data:
        command: '/test'
    action:
      - service: telegram_bot.send_message
        data_template:
          target: "{{ trigger.event.data.user_id }}"
          message: 'Test message received.'

From your phone send a message: /test

I suggest on notify part.

notify:
  - name: Telegram_DavesS7  <== try with no capital letter and no underscore
    platform: telegram
    chat_id: 536xxxxxx

Works with Capital’s and underscore.

Could mean that the Bot isn’t activated, I remember reading somewhere, that you need to send your Bot a message to activate it, just don’t remember where this minute.