Telegram bot notifications and communication

If it’s constant 401 error, something is wrong in your token.

May be you miss something. This is step by step to setup telegram bot.

From what I can tell, the letter ‘o’ is lowercase, compared to zero ‘0’

If you want, contact the BotFather and get a new Token.

  1. /start
  2. /token
  3. Follow BotFather response.

You can also browse to “https://api.telegram.org/botYOUR_API_TOKEN/getUpdates” as a test, it should return “{“ok”:true,“result”:[]}”

1 Like

I decided to just get a new token and what do you know… it all works now :grin: thank you!

1 Like

:+1: Cheers, glad you got it working.

Hi everybody,

I’m trying to make a text recognition in automation…

to success, I need to make a loop with a “for”. My algorithm is:

for i = 0 to 10
   if text[i] == 'turn' 
       and 
      text[i+1] == 'on' or text[i+2] == 'on'
   endif
endfor

but I can’t write the “for i = 0 to 10”

How can I do this ?
With this method, I hope to recognise the meaning of a sentence, and the position of words will have no impact !
I tried the method with homeassistant.turn {{onoff}} and it works but it misses possibilities !

Thank you !!

Conversation with Telegram Bot: "turn [Friendly Name] [on/off]" at post #32

1 Like

OK, so the chat_id is a bit misleading, I was confused as well. Chat_id does seem a bit like user_id, although is anyone aware of how to set this up with a group chat? A chat_id for a group chat would then be an actual chat_id rather than a user_id.

@get_id_bot actually responds with:

get id:
Hello Coolie1101
Your Chat ID = xxxxxxxxx

@myidbot responds with:
IDBot:
xxxxxxxxx

That’s correct, but neither of those are a group chat, or at least they didn’t work for me in a group chat. By group chat, I mean multiple Telegram users (family/housemates) with the bot in one conversation.

The only way I could get the correct ID for the Telegram API was following this answer: https://stackoverflow.com/a/45577773

None of the bots/apps could do it. The web app was the only way.

Open the web app, log in, go to the group chat and check the URL. Copy the numbers after the g and put a dash before it. That’s the group chat_id and it can be used to send a message from the bot to multiple users. So in this case, the chat_id is exactly what it’s called.

It makes sense to me now. When messaging a user, the chat_id is just the id of the user. When messaging a group, it’s the id of the group. So the naming seems accurate to me now that I’ve seen that.

Actually if you create a group and add @getidsbot, you would get a response with the group ID.

get id:
Hello Coolie1101
Your Chat ID = xxxxxxxxx
Username is not set

Your group Chat ID : -309375977
Title : Tsst

Response from @getidsbot
GetIDs Bot:
:bust_in_silhouette: You
├ id: xxxxxxxxx
├ first_name: Coolie1101
├ language_code: en-us
└ registered: > 24/02/2017 (?)

:busts_in_silhouette: Group
├ id: -309375977
└ title: Tsst

Ah, you’re right! The bot seems to respond as a private message to me when I ask for the group_id. But ye, that actually works.

My new problem is that when a second user tried to interact with my bot, nothing happens. The second user has the bot on their contacts/conversations. I can message /ping and the group gets pong, but if another user says /ping, nothing happens. All the IDs are correct, otherwise it wouldn’t reply to my own message.

Is groups enabled for your bot?

Yes, and it is an admin of the group.

what’s you automation config for response to /ping?

It’s just pretty simple.

- id: 'telegrampingpong'                                                            
  alias: 'Telegram bot to start pinging and ponging'
  trigger:
    platform: event
    event_type: telegram_command
    event_data:
      command: '/ping'
  action:
  - service: notify.oneplusgroup
    data:
      message: 'Message received!'
      data:
        inline_keyboard:
          - 'Ping:/ping'

My suspicion is that I need to add the second user to the allowed_chat_ids. I can’t test it right now as telegram bots seem to not be working at all for a lot of people.

However, it seems a bit odd that every person in a group chat needs to be approved. Other bots obviously don’t suffer with this. There could be hundreds of people in a telegram group with no way to get everybody’s user_id. Have other people got a group chat working with a home assistant Telegram bot?

Edit: to be clear, that obviously works for user 1, but user 2 wasn’t able to get a response. Can’t test anything else as telegram doesn’t seem to be working for either user.

Edit 2: telegram appears to be working and I got the id of user 2. I added that to the allowed_chats and now the hot will communicate to both users in the group chat. That seems a little cumbersome. I already allowed it to communicated with the entire group chat, it should be able to communicate with everyone in it.

Additionally to the problems with multiple users above, it appears the inline keyboard commands don’t work in a group chat? The same code works fine for a user chat, but clicking an inline keyboard button in a group chat does nothing.

Intended, as described in the DOC’s
.
I’m not sure how this works exactly, but what I did was add the bot id and group id as an allowed in HA, and all users in the group receive notifications from HA, but only the user that sends a message to the bot from the group chat receives a response from the bot independently, it this makes sense.

Group Chat > Bot = Independent response from Bot to initiating ID.
Bot (HA) to Group = Response to All ID’s in Group Chat.

I feel like there needs to be a wildcard accepted in the allowed_chat_ids with that being the case. Or, if allowing it to interact with a group, that should allow it to interact with anyone in the group.

The way I’ve set it up, the bot responds in the group chat if that’s where they get a question. You can ask in private and the bot would reply there as well.

There’s also the issue of inline_keyboard buttons not working in a group chat, not even for user 1.

Hi all

This 3d is what I was looking for to have Telegram make some control on Hass.io

Is there a way to have it managed via nodered instead of automations?
Any example?

Thanks

PS:
I found it using the addon from telgram and the “command” node.
Very easy and powerful