2
0 Comments

Easy notification system for your web app

This is a story of how we implemented notifications at https://floordle.app/

So before this, we were sending notifications on email, but we needed to send notifications on mobile and implement it quick.

Since our app users are mainly NFT collectors, most of them are using Telegram.

So this is a guide on how to send Notifications on Telegram:

Step 1 : Visit https://t.me/BotFather and create your Bot. The Bot will give you a token. Make sure to save that token.

Step 2: You need to somehow differentiate the users, we use uuid for this. Make the user to whom you want to send notifications access your bot in the following format:

https://t.me/BotName?start=userIdExample.
He also needs to press the 'Start' button in Telegram

Now you can visit

https://api.telegram.org/bot<BOT_TOKEN>/getUpdates
where BOT_TOKEN is the token you received from BotFather on Telegram and access all the updates.

Step 3: Webhooks
Telegram has webhooks built it, you can simply pass your server URL and it will make a request to your server on every update.
To set this up you need to make a GET request to this address:

https://api.telegram.org/bot<BOT_TOKEN>/setWebhook?url=YOUR_SERVER_URL

After the user presses Start button, Telegram will send an update to your server.

Inside the request body you should see something like this:

{ "message": { "chat": { "id": randomId }, "text": "/start >userIdExample", }}

You then need to extract userIdExample and chatId.

Step 4. You need to associate your user with the chatId.

Here there are multiple solutions, you can either create a separate table for telegram chats where you can store more than the chat Id, or you can simply associate it on the user object.

For the simplicity we'll go with second option.

So your user will look like this:

User: {
id: userIdExample,
chatId: chatIdFromTelegram
}

Step 5. Telegram has an endpoint for sending messages, each time you want to notify your user you will need to hit this endpoint and that's it.

https://api.telegram.org/bot<BOT_TOKEN>/sendMessage?>chat_id=<chatIdFromTelegram>&text={text}.

Your users should now receive it in the chat with the bot.

This is a basic example, you can find more by going to Telegram docs. > https://core.telegram.org/bots/api#sendmessage

And if you're collecting NFTs or have an interest in the industry check us out https://floordle.app/.

on March 24, 2022