Configuring telegram bot for HA

Hi,

I want to create a telegram bot for notifications like alarm system, covers, etc.

First i created bot in telegram and i have de api, but for HA configuration ask for chat_id. How i can obtain this value?

I tried to in http put:

https://api.telegram.org/botTokenAPI/getUpdates?offset=0

result: {“ok”:false,“error_code”:401,“description”:“Unauthorized”}

https://api.telegram.org/botTokenAPI/getUpdates

result: {“ok”:false,“error_code”:401,“description”:“Unauthorized”}

https://api.telegram.org/botTokenAPI/sendMessage?chat_id=@channelName&text=123

result: {“ok”:false,“error_code”:401,“description”:“Unauthorized”}

https://api.telegram.org/botTokenAPI/getUpdates

result: {“ok”:true,“result”:[]}

I dunno what else to do to obtain chat_id.

please can someone explain me?

thanks

Make your channel public, then call sendMessage and in json response look for the field “id”, this is the chat_id.

After get the chat_id you can make your channel private again.

Hi,

Thanks for reply.
Well i created channel and made it public. What does it mean “call sendMessage” ? I need computer for this or maybe with my iphone is enought?

Well sorry but i don’t have much idea about json, i need some application in my computer to use json? and see the id chat?

thanks and sorry about this questions.

Hi,

When i say call “sendMessage” i refer to: https://api.telegram.org/botTokenAPI/sendMessage… service.

Don’t worry about json, you’ll see the id that your looking for in response of sendMessage (field id).

The platform documentation shows two ways to retrieve the chat_id.

Hi,

Before create this topic, i red platform documentation as you can see in my first post with examples that i tried.

About second option in platform documentation, where i need to write this code? In HA? In pc?

Thanks

Well, in this case I will update the docs. It seems not clear that YOUR_API_TOKEN should be the token.

Command-line after the Python interpreter is loaded.

Hi jgon,

I tried to so as you told me, about message, but appear me a message that bot is not in channel:

https://api.telegram.org/botTokenAPI/sendMessage?chat_id=@casarg&text=123

result: {“ok”:false,“error_code”:403,“description”:“Forbidden: bot is not a member of the channel chat”}

Then i tried i add bot in the channel, but i can’t appears me this error:

Method: channels.inviteToChannel
Url: N/A
Result: {“_”:“rpc_error”,“error_code”:400,“error_message”:“USER_BOT”}
Stack: h@https://web.telegram.org/js/app.js:23:25735
ee@https://web.telegram.org/js/app.js:29:12302
e.inviteToGroup/<@https://web.telegram.org/js/app.js:27:21477
s@https://web.telegram.org/js/app.js:13:4947
r/<@https://web.telegram.org/js/app.js:13:5119
Cn/this.$get</g.prototype.$eval@https://web.telegram.org/js/app.js:13:12662
Cn/this.$get</g.prototype.$digest@https://web.telegram.org/js/app.js:13:11144
Cn/this.$get</g.prototype.$apply@https://web.telegram.org/js/app.js:13:12947
us[t]</<.compile/</<@https://web.telegram.org/js/app.js:15:17814
ae.event.dispatch@https://web.telegram.org/js/app.js:4:2735
ae.event.add/f.handle@https://web.telegram.org/js/app.js:3:31539

thanks

Hi,
the chat_id is not a nick name like “@casarg”.
It is a 9-digit number.

Hi to every one.

First of all thanks for replies.

At the end i obtained the chat_id.

How? Well via https i was in my telegram web. Then i wrote to my bot something like:

  • “/start”
  • “/test”
  • “Test”

then via https i use the code:

https://api.telegram.org/botTokenAPI/getUpdates?offset=0

and i obtained 3 lines with messages sent it before, and i each line i have de chat_id.

thanks!

Now i want send info from temperature sensor or status of mobile by telegram.

someone can tell me here to find this info or some examples?

thanks again!

I setup the bot and got a API key, however if I visit the link with my own api key I got the following reply:

{"ok":true,"result":[]}

Can someone tell my how to obtain my chat ID?

The Python version is not working for me:


>>> import telegram
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'telegram'

UPDATE: got my id after talking to my bot through Telegram web and then revisiting the URL :slight_smile: