Skip to content

stepandra/d

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

How to Create a Telegram Bot using QuickAlerts

Prerequisites


Step 1️⃣ - Creating the Bot on Telegram and adding it to a channel🤖

Open your Telegram app (Web/Desktop/Mobile) and search for @BotFather or go to https://t.me/BotFather. Open BotFather in Telegram and send a command /newbot in the chat, then give your bot a unique name for example, QuickAlerts Bot in the example below, then give it a username for example, myquickalert_bot in the example below. Once the BotFather creates your bot copy the username and the bot token.

Then create a new channel on Telegram, add a bot myidbot as administrator. Send a command /getgroupid, copy the id and then remove the bot. Refer animation below.

Now add your QuickAlerts Bot in this channel using the username.

At this point you should have the Token of your bot and channel ID of your channel.


Step 2️⃣ - Setting up the backend for bot🪛

Clone Example Monorepo

To begin, clone the qn-guide-examples repo, navigate to this project's directory, install dependencies and open the project directory in a code editor (VS code in this case).

git clone https://github.com/quiknode-labs/qn-guide-examples.git
cd qn-guide-examples/QuickAlerts/Telegram_bot
npm install
code .

Open a Terminal window and run ngrok on a port (make sure you have ngrok installed globally on your system).

ngrok http 5001

Copy the URL which you get after running the above command.

Now create a .env file in your /Telegram_bot directory and paste the following in it:

TOKEN=<bot_token>
PORT=5001

Replace <bot_token> with the bot token you got from BotFather in the last step. Make sure the value of PORT is exactly the same as the port number used with the ngrok command.

In the index.js file, replace <TELEGRAM_CHANNEL_ID> on line 24 with the channel ID you got from step 2.


Step 3️⃣ - Setting up QuickAlerts🔔

Setup an alert on the QuickAlerts dashboard, while setting up the WebHook in the dashboard use the URL you got from ngrok with a /webhook suffix. So it should look something like this https://31f5-2401-4900-1c96-acd1-501f-36bc-819c-c5f5.ngrok.io/webhook

Learn how to setup a QuickAlert: Guide, Video

An example QuickAlerts expression to get notified when ever a Bomber Hero NFT on Polygon chain is transfered:

(tx_logs_address == '0xd8a06936506379dbbe6e2d8ab1d8c96426320854') && (tx_logs_topic0 == '0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef')

Step 4️⃣ - Running the bot🏃‍♂️💨

Open a new terminal window and run:

npm run dev

Now your Telegram channel should start recieving messages from the bot, the information in the messages is fed by QuickAlerts.

You can use/create different variables from line 17-20 and edit the message part in index.js file from line 28-29 based on the kind of information you want to display in the Telegram message. With QuickAlerts there is immense possibilities around the kind of alerts one can create.

Above and beyond!

About

d

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published