A lot of times I find myself wanting to notify my phone for something that doesn't have an app. Whether it's something I'm hacking together, or an integration with an existing service, having a flexible mechanism to alert my phone on my terms is powerful.
There's a few ways to go about this, but here I'm going to outline how to create a personal Telegram bot that will send messages to our specific, private chat.
It's alive!
Let's create our bot, and get our bot's API Token.
Start (or continue) a chat with BotFather.
-
Enter
/newbot
into the chat and BotFather will ask you for a name for your bot, then a unique username for your bot, and finally give you a token that will look similar to110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw
:
Nice to meet you.
Now let's say hello and get our chat id.
-
Head back out to your chats, find your bot by its username and say hi--though it won't reply yet :)
-
Now we need the id of the chat we've just started. We can do that from our browser using getUpdates of the Telegram API by heading to the following url, replacing the
<token>
portion with the token we got above.
https://api.telegram.org/bot<token>/getUpdates
-
The response should be JSON with results from the chat we started with our bot from #4. Find the
message.chat.id
and save this in a secure place.
Hello World!
We now have all the information we need to start sending ourselves messages from our bot.
-
Let's reply to our chat by hitting the sendMessage endpoint url below using our token and chat id that we got above.
https://api.telegram.org/bot<token>/sendMessage?chat_id=<chat_id>&text=Oh,+hi!
-
...and we should get a reply back.
That's a wrap!
And there we go. From here we can message ourselves as easily as hitting a url. We can do it from our own python script, nodejs service, chrome extension, bash script, webhooks, really anything that is capable of making an HTTP call can now alert our phone.
Of course, this is just the start of a what a Telegram bot can do. We can then code on top of this bot to listen for commands, issue responses, etc. But, for this post, we're only interested in setting up a one-way communication channel to alert our phones.
Optional
It's worth noting that bots are inherently public. People can find and add our bot. We can stop our bot from being added to groups by heading back to our conversation with BotFather and entering /setjoingroups
, choosing our new bot, and entering in Disable
. BotFather should let us know it was successful. I don't think this disables direct chats; at least not from what I can tell.
Though, it doesn't really matter if people chat up our bot. We'll be sending messages only to our specific chat id. If and when we were to write a bot program that listens to commands, we'd obviously want to only take whatever action when it comes from our chat id (assuming, we want to keep it private).
Top comments (0)