Set up Telegram using Nabu Casa

I’m trying to set up Home Assistant using Nabu Casa to send and receive Telegram messages.

So far I’m trying to use a simple ping pong script.

Configuration.yaml:

telegram_bot:
  - platform: webhooks
    api_key: 111111111:AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    url: https://abcdefghijklmnop.ui.nabu.casa:8443
    allowed_chat_ids:
      - 11111111111

notify:
  - name: Telegram_Bot
    platform: telegram
    chat_id: 11111111111

Then I set up a automation using the automation wizard. I have translated the following YAML

- id: 'A'
  alias: PingPong
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/ping'
  action:
    - service: telegram_bot.send_message
      data:
        message: 'pong'

Unfortunately, this doesn’t work upon receiving the messages (in fact, I cannot even see the event). Sending the messages does work on the other hand (via a service or by triggering the automation).

How come I can send messages but I cannot receive them?

I’ve given up on Nabu Casa since I cannot do port forwarding. I am now trying to do the same with DuckDNS but I also cannot get it working. The reaction is the same. I can send messages, but not receive. I’ve tried multiple things:

  • Monitored the event stream by watching for telegram_message and telegram_text, but nothing happens when I send a text to the bot.
  • Monitored the full event stream with node-red debugging, but nothing happens when I send a text to the bot.
  • I checked the open ports and the one I specify for Telegram (8443) is open (from the internet). This means the port forwarding works correctly and something is happening on this port.
  • I have no active Telegram related errors. The platform is set up correctly.
  • Besides, I’m using duckdns for accessing my Home assistant instance and that is working correctly, so it also isn’t the connection.

Latest config:

http:
  base_url: https://xxxxxxx.duckdns.org:8123
  ssl_certificate: /ssl/fullchain.pem
  ssl_key: /ssl/privkey.pem
  server_port: 8123

telegram_bot:
  - platform: webhooks
    api_key: 111111111:AAAAAAAAAAAAAAAAAAAAAAAAAAAAA
    url: https://xxxxxxx.duckdns.org:8443
    allowed_chat_ids:
      - 11111111111

notify:
  - name: Telegram_Bot
    platform: telegram
    chat_id: 11111111111

I have also triple checked the chat_id and it is the same in both config parts. Therefore the chat id shouldn’t be the issue.

Where are you getting url from? Telegram doesn’t require a URL to work, so you don’t even need Nabu Casa.

Check out: https://www.home-assistant.io/components/polling/

Hmm, I’ve been trying the webhooks way since it was recommended as ‘the preferred way’. Switched to polling and now it works indeed without url (tried it also without url with webhooks, but without any effect).

Thanks a lot!

Have anybody godt webhooks for Telegram working when using Nabu Casa? I currently use polling which works fine, but I sometimes get repeated messages, which might be due to polling. In the docs webooks need a base_url, but is that just the remote URL for nabu casa?

I am also waiting to see this working so I can use Telegram app to communicate to HA.
By the polling mode makes in thousands of request to telegram server and PiHole lists HA top client in the list :frowning:

I have Telegram working using webhooks via Nabu Casa. I also switched from polling to avoid all those requests (and I think webhooks is a bit quicker as well).

There is no need to configure an url in the Telegram config when you have defined the external url in the basic information setup.

You have to add 127.0.0.1 to trusted_networks as otherwise the messages aren’t received by the bot.

This is my config:

telegram_bot:
  - platform: webhooks 
    api_key: !secret api_telegram
    trusted_networks:
      - 149.154.160.0/20
      - 91.108.4.0/22
      - 127.0.0.1
    allowed_chat_ids:
      - !secret chatid_telegram
      - !secret alarm_chatid_telegram
      - !secret info_chatid_telegram
      - !secret user_chatid_telegram
4 Likes

Appreciate it.
I assume that external_url in basic config should be the url of my Nabu Casa instance and it would be something like shown below right?

homeassistant:
  external_url: https://*****.ui.nabu.casa

Thank you so much!

That’s correct. I have configured the Nabu Casa url via the GUI, but in config.yaml should work of course as well.

1 Like

All good now and working as expected thank you so much!

thanks. this has solved my problems. also please check ip_bans.yaml and make sure 127.0.0.1 is not listed inside there. It was listed in mine and I have wasted many hours trying to figure out what went wrong.

TL;DR; Try forwarding external port 8443 to 8123

Oke, I’ve been at this for a few days now… and finally managed to get it working with webhooks. I have HomeAssistant running on a Raspberry Pi, not directly exposed to the internet.

I have a Synology NAS running a Reverse Proxy, thus handeling the ssl certificates.



image
I’ve forwarded port 8443 from my router to my nas. Note that in the NAS, I have hooked up external port 8443 to internal port 8123. Also note that https is forwarded as http traffic, this is because there is no valid ssl certificate between the NAS and the Pi.

My http config looks like this:

http:
  use_x_forwarded_for: true  # Required for reverse proxy to work
  trusted_proxies: 192.168.1.2  # IP of the synology

My Telegram config looks like this:

telegram_bot:
  - platform: webhooks
    api_key: !secret telegram_api_key
    allowed_chat_ids: !secret telegram_allowed_chat_ids
    trusted_networks:
      - 149.154.160.0/20
      - 91.108.4.0/22
      - 127.0.0.1  # Not sure if this is actually required
      - 192.168.1.2  # The nas
    url: !secret telegram_url  # https://example.org:8443

The AHA! moment was when I realised that Home Assistant was blocking traffic on 8443 because it is not configured as the server port, that’s when I routed external traffic on 8443 to 8123 on the pi.