Personal Telegram Bot for Alerting Your Phone (w/o code)
Tags: telegram, chatbot, api, notifications
Published: 2020-12-09 23:30:01

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.

1. Start (or continue) a chat with [BotFather](https://t.me/botfather).

2. 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 to `110201543:AAHdqTcvCH1vGWJxfSeofSAs0K5PALDsaw`:

    ![Conversation with BotFather creating our new bot](https://dev-to-uploads.s3.amazonaws.com/i/q0u0162bvwsnw6jfq0s8.jpg)
    <figcaption>Save this token in a secure spot.</figcaption>



## Nice to meet you.
Now let's say hello and get our chat id.

1. Head back out to your chats, find your bot by its username and say hi--though it won't reply yet :)

    ![Conversation saying hello to our new bot.](https://dev-to-uploads.s3.amazonaws.com/i/xquqqmihj5ozbix0mb2t.jpg)
    <figcaption>We'll get them to say hi back soon.</figcaption>

1. Now we need the id of the chat we've just started. We can do that from our browser using [getUpdates](https://core.telegram.org/bots/api#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

    ```

1. 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.

    ![JSON output highlighting the chat id](https://dev-to-uploads.s3.amazonaws.com/i/fflp9xbow7eehndu7se2.jpg)
    <figcaption>JSON.</figcaption>


## Hello World!
We now have all the information we need to start sending ourselves messages from our bot.

1. Let's reply to our chat by hitting the [sendMessage](https://core.telegram.org/bots/api#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!

    ```
2. ...and we should get a reply back.

    ![Conversation with our bot saying hello back.](https://dev-to-uploads.s3.amazonaws.com/i/vcu45y50hiwpuaxebhao.jpg)
    <figcaption>And now we're talking to ourselves.</figcaption>


## 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).