Is it possible to get multiple telegram bots?

Is there any way use two different bots in telegram?

1 Like

Sure, you can have it.

1 Like

Great. How?

Try this:

telegram_bot: 
  - platform: polling
    api_key: 1111111111333:asdfasdfasdfasdfasdfasdfasdfsaf
    allowed_chat_ids:
      - 11111111111
      - AAAAAAAAA

  - platform: polling 
    api_key: 222222222333:asdfasdfasdfasdfasdfasdfasdfasf 
    allowed_chat_ids:
      - BBBBBBBBB
      - 2222222222

Thanks for the support but once I’ve done that I’m not sure how to select the right bot from which to send the message.

For example, I have a notify component set up for my original bot:

Example configuration.yaml entry for the notifier

notify:

  • name: NOTIFIER_NAME
    platform: telegram
    chat_id: CHAT_ID_2

I want to use two different bots in the same chat (one for health reminders, the other for problems in the house/alerts).

1 Like

Now I understand your need. In fact I use 2 bots, but one is just for inputs (managed by node-red). It may be possible to achieve what you want with node-red, but I don’t know if you can using just HA.

Unfortunately I’m using hassbian so nodered is difficult to install and use

What?

You just install nodered as indicated on their website. It’s no different than installing any other applications

I have two separate bots created, 1 for general info and 1 for security alerts. Funny thing is that this used to work quite a few revisions ago. I believe it worked back then because you could specify an API Key for the notifier like so

notify:
  - platform: telegram
    name: telegram_HA
    api_key: 123456AABBCCDDEEFFF
    chat_id: 123456789ZZ

This appears to be ignored now and will only deliver to the first configured Telegram Bot. Anyone have any ideas?

I have the same question. Anyone know?

Same here. would like a separate bot for alarm notifications and on with general info…

Hi,

Same here too. I couldn’t get it working with only HA, but I got it working using the MQTTWARN. You will have another app/docker running but there’s some advantages in doing so, because you decouple the notifications from HA, which means that if you want change how/who gets a notification you can do that without restarting HA. The basic idea is that you publish to a topic what message you want to send, and on MQTTWARN you create the rules on how and to who this message should be sent. Since HA can talk directly to the MQTT instead of using a component you should create a script, like this:

homealert:
  sequence:
    - service: mqtt.publish
      data_template:
          payload: "{{ message }}"
          topic: mqttwarn/alert/msg

you may call the script like this:

- service: script.homealert
        data_template:
            message: "Danger Will Robinson"

The configuration of the mqttwarn is simple, the relevant part of mine (i.e. not including the mqtt part) looks like this:

[config:telegram]
timeout = 30
parse_mode = 'Markdown'
token = '<your_bot_token>'
targets = {
      'j01' : [ '#-33xxxxxxxx' ],
      'j02' : [ '#-2xxxxxxxxx' ]
}

[mqttwarn/info/+]
targets = telegram:j01

[mqttwarn/alert/+]
targets = telegram:j02

As you can see, you don’t need two bots! Just create different groups and include people/bots you want.

1 Like

I actually solved my idea in a different way than having 2 bots.
I have 1 bot but two telegram groups.
1 group for generic notifications and the other for urgent notifications.
I added the chat id’s of those 2 groups to the allowed chat ids:

telegram_bot:
  - platform: broadcast
    api_key: !secret telegram_api
    allowed_chat_ids:
      - !secret telegram_chatid
      - !secret telegram_chatid2

Then under notify I created 2 items called Bot and BotUrgent with those same chatid’s:

notify:
  - name: TelegramBot 
    platform: telegram
    chat_id: !secret telegram_chatid
  - name: TelegramBotUrgent   
    platform: telegram
    chat_id: !secret telegram_chatid2

So now in an automation I can send it to either of the 2.
If you also add (both under telegram_bot as under notify) for instance the chatid of your own telegram account (so not the groups but your private one), you can also send notifications only to yourself instead of the groups. I did that because my wife is in the groups but some notifications really only need to go to myself.

10 Likes

Having groups you will see messages arriving from different “users” in telegram android/iphone app correct?
Will investigate how to do these different groups, and use also @clyra idea of mqtt, I like way more of doing changes without having all the time to restart HASS

Also interested how many groups you guys use, and for each group which type of messages /notifications you use (for inspiration)

I’ve tried to get this working but I keep hitting an error and well… it doesn’t work… Any ideas please?

ERROR (SyncWorker_19) [homeassistant.components.telegram_bot] Error sending message: Chat not found. Args: (xxxxx, 'Home Assistant restarted'),
1 Like

answer my own question… group IDs are a negative… so use -123456678 etc

I had the same problem. I want a “standard bot” for info and a small chat app I created (turn lights on/off, set cover position, get gas prices…), but I wanted another bot for urgent alarm messages (water leakage…). It didn’t work with groups for me, because I want my fitness tracker (Mi Band) to vibrate aggressively in an urgent case, and I didn’t manage to set up “Tools & Mi Band” (an app) to distinguish between the groups, as both messages come from the same contact/bot.

That’s why I extended my appdaemon app, that I wrote a long time ago when I had an unstable internet connection (that’s why all the ping and check stuff is in the app. I could remove that one day…).
It listenes for an event called “custom_notify”. Any automation (or appdaemon app) can send it. If you provide a target that is defined in configuration.yaml, it will send the message to that target. If traget == “special_bot”, you have to provide the token of that bot (I accidently called it api_key) and the chat_id it should be sent to.
By that, I can send a message via every bot I want, and I do not disturb the “standard bot” configuration of home assistant.

Maybe this helps if someone has a similar issue…

3 Likes

You are just a GENIUS! Thank you so much!
I do not understand why this is not a very popular topic.
I think this is a very important issue.
You need a channel for regular notifications, for history, and you need a separate channel for the most important notifications. Type of alarm.
Just awesome! Thanks again!

1 Like

Did anyone solve this without any extra config like appdaemon, etc?