Telegram for Developers
I've heard about Telegram and its bots for a while but never understood exactly why it'd be useful. Yet another chat? I finally looked into and discovered how it's useful to me, someone who hacks on side projects for fun and maybe profit. 🤞
Push Notifications
It's usefulness really comes down to me being able to easily send myself push notifications and a simple interface to view them.
I'm hacking on a side project and I wanted to be notified if/when users do certain actions:
- sign up
- perform an onboarding action
- reach an api limit
- etc
This gives me an opportunity to each out to the user if I feel like it may be appropriate to do so. Yes, this won't scale but this is for my hobby projects. I'm not worried about scale.
I could use email but then I need to figure out email credentials to be able to actually send them and have to use an email client to read the messages which are only going to be a sentence long. I find a dedicated chat interface to be much nicer for this use case. I could use Slack but creating my own slack workspace for myself seems like overkill. Not to mention the Slack api takes some time to familiarize yourself with.
Telegram Speed Run
There's an extensive tutorial from Telegram that you can follow but it's pretty long and uses java. So here's a very condensed version that uses node.
First, install the app on your phone and computer.
Bots
There are two "bots" we're going to use to help set things up. A bot is just a chat application within Telegram.
Go here to start using the "BotFather" bot. This bot helps you create bots. Enter /newbot
in a chat with the BotFather and follow the prompts to create new bot.
We'll be using the bot's username and the access token shortly.
Next, we need to get the id
of our own account so we can send messages from the bot account to ourselves. We'll use another bot for this. Type userinfobot
in the Contacts search bar, select the bot and click "Start". It will immediately respond with a message containing your Id
.
Sending a message
You can use a super simple http request with your bot's access token and your chat id.
POST https://api.telegram.org/bot<telegram_bot_token>/sendMessage
Content-Type: application/json
{
"chat_id": <chat_id>,
"text": "Is this thing on??"
}
Note that the token is prefixed with bot
and there's no space between it and the actual token.
If you've installed the mobile client you should see a push notification.
Sending messages in Node
In my project sending these notifications isn't critical to the business logic so I like to fire and forget the sending of these messages.
import { env } from "~/env.server";
export async function sendTelegram(message: string) {
const url = `https://api.telegram.org/bot${env.TELEGRAM_BOT_TOKEN}/sendMessage`;
return fetch(url, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
chat_id: env.TELEGRAM_MY_ID,
text: message,
}),
});
}
And then somewhere else in the application:
const user = await createUser(data);
await sendConfirmationEmail(user)
sendTelegram(`New user signup: ${data.email}`).catch(console.error)
return redirect('/confirm-user')
Notice the sendTelegram
function is not being await
ed and there's a catch
to ensure any error doesn't bubble up and crash the application. This means the user isn't waiting on the process of interacting with the telegram api to finish. I want the app to be as snappy as possible with the least amount of effort.
Categories: Remix