Wednesday, July 30, 2025
  • Login
SB Crypto Guru News- latest crypto news, NFTs, DEFI, Web3, Metaverse
No Result
View All Result
  • HOME
  • BITCOIN
  • CRYPTO UPDATES
    • GENERAL
    • ALTCOINS
    • ETHEREUM
    • CRYPTO EXCHANGES
    • CRYPTO MINING
  • BLOCKCHAIN
  • NFT
  • DEFI
  • WEB3
  • METAVERSE
  • REGULATIONS
  • SCAM ALERT
  • ANALYSIS
CRYPTO MARKETCAP
  • HOME
  • BITCOIN
  • CRYPTO UPDATES
    • GENERAL
    • ALTCOINS
    • ETHEREUM
    • CRYPTO EXCHANGES
    • CRYPTO MINING
  • BLOCKCHAIN
  • NFT
  • DEFI
  • WEB3
  • METAVERSE
  • REGULATIONS
  • SCAM ALERT
  • ANALYSIS
No Result
View All Result
SB Crypto Guru News- latest crypto news, NFTs, DEFI, Web3, Metaverse
No Result
View All Result

NodeJS Telegram Bot Tutorial – Construct a Telegram Bot Utilizing NodeJS

by SB Crypto Guru News
January 5, 2023
in Web3
Reading Time: 14 mins read
0 0
A A
0


Are you in search of a seamless approach to create a NodeJS Telegram bot? If that’s the case, you’re exactly the place you want to be! This NodeJS Telegram bot tutorial will present you learn how to create one for relaying real-time blockchain switch occasions! Because of Moralis, it is possible for you to to construct this NodeJS Telegram bot in solely three steps: 

  1. Making a Backend Server
  2. Setting Up a Moralis Stream
  3. Constructing the NodeJS Telegram Bot

If you want to leap straight into the code, try the NodeJS Telegram bot GitHub repository under: 

Full NodeJS Telegram Bot GitHub Repo – https://github.com/MoralisWeb3/youtube-tutorials/tree/primary/automated-telegram-nft-transfer-bot

Overview

We’ll begin off this text by diving into the intricacies of Telegram bots. From there, we’ll briefly study the instruments and assets required for constructing a NodeJS Telegram bot. As such, we’ll give attention to Moralis and the Web3 Streams API. Subsequent, we bounce straight into the NodeJS Telegram bot tutorial, protecting the method of constructing one from begin to end. So, if you wish to create your very personal NodeJS Telegram bot, observe alongside all through this text! 

If in case you have additional curiosity within the Web3 growth house, we advocate exploring Moralis additional. The Streams API is just one software that may assist your growth endeavors. One other instance is the NodeJS EVM API, which may come in useful each time creating EVM-compatible tasks. Together with its Web3 APIs, Moralis affords extra Web3 assets, similar to wonderful blockchain growth content material. If this pursuits you, try our guides on the Verse programming language or learn the way Web3 get occasion logs to work! 

Earlier than persevering with, enroll with Moralis, as you want an account to observe alongside on this NodeJS Telegram bot tutorial. Creating an account is free, and you may instantly leverage the ability of blockchain know-how! 

Sign Up with Moralis to Complete This NodeJS Telegram Bot Tutorial

What’s a Telegram Bot? 

Earlier than exhibiting you learn how to create a NodeJS Telegram bot, the article will initially discover the ins and outs of those bots. So, what precisely is a Telegram bot? 

Telegram

In brief, a Telegram bot is a program that acts like a standard chat accomplice however comes geared up with extra features. Bots can carry out predefined duties with none consumer involvement. Moreover, a Telegram bot can do every thing a human chat accomplice does both robotically or on explicit requests. This consists of fundamental performance similar to relaying data like textual content messages, movies, pictures, or another recordsdata. 

Consequently, with a Telegram bot, it’s potential to automate real-time alerts to inform customers when one thing of curiosity occurs, which is precisely what we’ll present you learn how to do on this article! 

How Can You Construct a Telegram Bot? 

With a quick introduction to Telegram bots, you would possibly ask your self, ”can I construct a Telegram bot with JavaScript?”. Nicely, the reply to this query is sure! To point out you ways that is completed, we’ll exhibit learn how to create a simple NodeJS Telegram bot that listens to and relays real-time information concerning blockchain occasions utilizing Moralis! 

To be able to get the knowledge referring to the on-chain occasions, we’ll use the Web3 Streams API. With this growth software, you may stream blockchain information immediately into your backend server by way of Web3 webhooks. From there, we’ll educate you learn how to ship notifications about these occasions by means of a Telegram bot! 

Announcement Banner: Moralis Launches Telegram Bot API/Streams API

So, if you wish to discover ways to create a Telegram bot, be part of us within the following NodeJS Web3 tutorial as we break down the method from begin to end! 

Tutorial: Construct a NodeJS Telegram Bot 

With a extra complete understanding of Moralis’ Streams API, this part will dive into the central a part of this text. The NodeJS Telegram bot we’re about to construct makes use of a Web3 stream to watch EVM occasions and autonomously ship messages to your Telegram channel. Furthermore, on this tutorial, we’ll present you learn how to take heed to all transfers of the “Doodles NFT” sensible contract. 

Moralis

Because of the accessibility of Moralis, the Streams API, and the NodeJS SDK for Web3, you’ll be to construct this Telegram bot in three simple steps: 

  1. Making a Backend Server
  2. Setting Up a Moralis Stream
  3. Constructing the NodeJS Telegram Bot

By following alongside on this NodeJS Telegram bot tutorial and finishing the aforementioned steps, you’ll discover ways to monitor blockchain occasions and obtain messages associated to on-chain occurrences. From there, you may observe the identical workflow to watch any sensible contract occasions on all blockchain networks! 

However, when you want watching movies to coach your self, you can too try the clip under from Moralis’ YouTube channel. On this video, one in all Moralis’ proficient software program engineers takes you thru the complete course of from begin to end: 

Now, earlier than leaping into step one of this NodeJS Telegram bot tutorial, the following part briefly covers the top outcomes. Doing so provides you a extra profound understanding of what you’re working in direction of, making it simpler to visualise what the code does! 

Construct a NodeJS Telegram Bot – What Are You Working Towards? 

Earlier than exhibiting you learn how to arrange the backend for the NodeJS Telegram bot, we’ll initially present you the way it works. As such, allow us to instantly take a better have a look at the top outcome: 

Doodle NFT Bot Channel on a smartphone

Because the picture above illustrates, you’ll arrange a brand new Doodle NFT bot channel, to which the bot returns the response from the Web3 stream. You too can see a few transactions despatched to the channel. These responses include from/to addresses, a token merchandise, and a transaction hash. Nevertheless, that is solely a choice of information returned from the Web3 stream. As such, you may select from the response to customise the messages as you see match. 

Nonetheless, that covers the top outcomes, and also you now know what you’re working towards. When you discovered this attention-grabbing and need to create your individual NodeJS Telegram bot, be part of us as we bounce straight into step one of the tutorial! 

Step 1: Making a Backend Server 

To kickstart this NodeJS Telegram bot tutorial, begin by launching your most popular built-in growth atmosphere (IDE), arrange a brand new NodeJS venture, and create a folder referred to as ”backend”. You possibly can then add a brand new file referred to as ”index.js” to the aforementioned folder and enter the next contents: 

require("dotenv").config();
const categorical = require("categorical");
const app = categorical();
const port = 5001;

app.use(categorical.json());

app.put up("/webhook", async (req, res) => {
  const webhook = req.physique;

  console.log(webhook);

  return res.standing(200).json();
});

app.hear(port, () => {
  console.log(`Listening for NFT Transfers`);
});

With this code, we initially require the required dependencies and specify that your Categorical server will run on “localhost 5001“. What’s extra, the code on the far finish additionally ensures that we take heed to this server by calling the ”app.hear()” perform whereas passing the ”port” variable as an argument. The code additionally introduces the ”/webhook” endpoint, which you want to begin in your system. Nevertheless, earlier than doing so, we should guarantee we are able to put up to this webhook. You possibly can accomplish this by utilizing “ngrok” to create a tunnel to your native machine, that means anybody can entry the endpoint over the web. 

To take action, you initially want to put in ngrok, which you are able to do by opening a brand new terminal and working the next command: 

brew set up ngrok/ngrok/ngrok

From there, you should additionally set up the ngrok dependencies in your ”backend” folder. As such, ”cd” into ”backend”, enter this command, and hit enter: 

npm i ngrok

With the dependencies put in, the following factor you want to do is run the next command utilizing port 5001: 

ngrok http 5001

It will return a URL, which you want to copy and save for the following step:

Code editor terminal showing the Telegram bot URL

Nevertheless, earlier than concluding this preliminary step of the NodeJS Telegram bot tutorial, you should run the Categorical server. As such, open a brand new terminal, ”cd” into the backend folder, and begin by putting in the required dependencies utilizing this command: 

npm i dotenv categorical node-telegram-bot-api nodemon

Subsequent, all that continues to be is so that you can launch the server by inputting the next and hitting enter: 

npm run begin

Step 2: Setting Up a Moralis Stream 

With the server up and working, the following step is to create a brand new Moralis stream. As such, if in case you have not already, enroll with Moralis instantly and log in to the admin panel. You possibly can then click on on the ”Streams” tab and choose ”Create a brand new stream”: 

Streams landing page

Subsequent, choose the ”Create it from Admin” possibility and enter the handle of the sensible contract you wish to monitor (in our case, we fetch the Doodles contract handle from Etherscan):

create it from admin button on streams page

From there, progress to the ”Stream Configurations” half and fill within the enter fields. To start with, add the ngrok URL from the earlier step to the ”Webhook URL” area and embody the ”/webhook” endpoint. Subsequent, add an outline and tag of your alternative: 

Telegram Bot NodeJS URL configuration fields

When you end the stream configurations, select the community you want to monitor. On this case, we’ll go for the Ethereum mainnet: 

network selection page

Upon getting chosen the community you have an interest in, transfer on to the fourth step, and examine the ”Contract interactions (logs)” field for the handle exercise: 

contract interaction checkbox

Lastly, for the final step, you should add the ABI of your contract. When you need assistance fetching this data, you may go to Etherscan, navigate to the contract you have an interest in, click on on the ”Contract” tab, and scroll all the way down to the ”Contract ABI” half:

Doodles NFT contract page on Etherscan

When you paste the ABI, Moralis will autonomously present you the matters you may stream. On this case, because you need to monitor the transfers of the Doodles contract, examine the ”Transfers(handle,handle,uint256)” field: 

Moralis admin panel ABI code input field

That’s it for the stream! As quickly as you launch it, you may transfer again your IDE, open a brand new terminal, and it’s best to shortly be receiving responses consisting of assorted switch occasions: 

terminal inside visual studio code

Step 3: Constructing the NodeJS Telegram Bot

With the responses trickling into the backend server by way of the Moralis stream, it’s time to arrange the NodeJS Telegram bot. This third step of this tutorial is split into two sections: 

  1. Creating the Bot 
  2. Connecting it with the Code 

As such, with out additional ado, allow us to bounce straight into the method of organising the bot! 

Creating the Bot

The very first thing you should do is open Telegram, seek for ”Bot Father”, and begin a dialog. As quickly as you begin the dialog, it’s best to discover a begin button on the backside of the interface. When you hit this button, it ought to lead to an interface just like the one proven under: 

BotFather outlining configuration steps for the NodeJS Telegram bot

From there, you may create a brand new bot by hitting ”/newbot” and selecting an applicable title: 

available commands such as /newbot and /mybots

Subsequent, ”Bot Father” will question you for a username, and it wants to finish with ”_bot”: 

success message after using _bot

You’ll instantly discover a token API while you hit enter and create the bot. Copy this worth and put it aside for later: 

Telegram bot NodeJS HTTP API ID

Lastly, create a brand new Telegram channel and add the bot as an administrator. You are able to do so by clicking on the three dots on the prime, hitting ”Data”, choosing ”Administrator”, and lastly, hitting ”Add Admin”: 

Settings page for the Doodle NFT Bot Channel and the Administrators button

That’s it for the bot itself! All that continues to be from right here is connecting it with the code and the Web3 stream! 

Connecting it with the Code

Now that you’ve the bot at your disposal, you should additionally add it to the code. To take action, begin by creating a brand new ”.env” file and including the entry token as an atmosphere variable. It ought to look one thing like this: 

TELEGRAM_BOT_TOKEN = “589017050…”

From there, exchange the contents of ”index.js” with the next code snippet: 

require("dotenv").config();
const categorical = require("categorical");
const TelegramBot = require("node-telegram-bot-api");
const app = categorical();
const port = 5001;

const TELEGRAM_BOT_TOKEN = course of.env.TELEGRAM_BOT_TOKEN;

const bot = new TelegramBot(TELEGRAM_BOT_TOKEN, { polling: true });

app.use(categorical.json());

app.put up("/webhook", async (req, res) => {
  const webhook = req.physique;

  for (const nftTransfer of webhook.nftTransfers) {
    const fromAddress = `From handle: ${nftTransfer.from.slice(
      0,
      4
    )}...${nftTransfer.from.slice(38)}`;
    const toAddress = `To handle: ${nftTransfer.to.slice(
      0,
      4
    )}...${nftTransfer.to.slice(38)}`;
    const tokenItem = `Token Merchandise: ${nftTransfer.tokenName} #${nftTransfer.tokenId}`;
    const transactionHash = `Transaction Hash: ${nftTransfer.transactionHash}`;

    const chatId = “REPLACE_ME”;
    const textual content = `${fromAddress}, ${toAddress}, ${tokenItem}, ${transactionHash}`;

    bot.sendMessage(chatId, textual content);
  }

  return res.standing(200).json();
});

app.hear(port, () => {
  console.log(`Listening for NFT Transfers`);
});

Lastly, the ultimate step is changing ”REPLACE_ME” on line 29 together with your chat ID. To get this ID, go to the endpoint down under: 

https://api.telegram.org/botTOKEN_API/getUpdates

Nevertheless, be sure that to interchange ”TOKEN_API” together with your precise token API. Doing so ought to take you to a web page trying one thing like this: 

URL showing code structure and the ID of the NodeJS Telegram bot

As such, all you want to do from there’s copy the chat ID, enter it into the code, and begin the server as soon as once more with the next terminal enter:

npm run begin 

Congratulations! You now know learn how to arrange a NodeJS Telegram bot from scratch! If every thing works as meant, all switch occasions needs to be relayed and despatched to the Telegram channel.

When you had hassle at any level throughout this information, please try the video from the ”Tutorial: Construct a NodeJS Telegram Bot” part. This video breaks down the code in additional element, hopefully answering any of your queries. You too can discover the whole NodeJS Telegram bot GitHub code down under: 

Full NodeJS Telegram Bot GitHub Repository – https://github.com/MoralisWeb3/youtube-tutorials/tree/primary/automated-telegram-nft-transfer-bot

Abstract – Methods to Construct a Telegram Bot Utilizing NodeJS

On this article, we started by protecting the ins and outs of Telegram bots. From there, we explored Moralis and the Streams API, as this is among the instruments used all through this tutorial. From there, we dove straight into the NodeJS Telegram bot tutorial, the place we demonstrated learn how to create one in solely three steps: 

  1. Making a Backend Server
  2. Setting Up a Moralis Stream
  3. Constructing the NodeJS Telegram Bot

As such, if in case you have adopted alongside this far, you now know learn how to create a NodeJS Telegram bot relaying data concerning blockchain occasions! What’s extra, if you wish to try the complete code, go to the NodeJS Telegram bot GitHub repository!

When you discovered this tutorial instructive, be happy to study Web3 programming additional right here at Moralis’ Web3 weblog. As an example, try our articles on learn how to create a blockchain Discord bot or learn concerning the intricacies of danksharding. 

However, it doesn’t matter if you’re trying to construct a NodeJS Telegram bot or another Web3 tasks; enroll with Moralis now! You possibly can create your account without spending a dime, and it solely takes a few seconds!





Source link

Tags: Bitcoin NewsBotBuildCrypto NewsCrypto UpdatesLatest News on CryptoNodeJSSB Crypto Guru NewsTelegramTutorial
Previous Post

Coinbase Reaches $100M Settlement for AML Failure

Next Post

Low quantity killed 66% of cash in final 10 years

Related Posts

Exploring Moonbeam – Why Build on Moonbeam? – Moralis Web3

Exploring Moonbeam – Why Build on Moonbeam? – Moralis Web3

by SB Crypto Guru News
September 11, 2024
0

In today’s tutorial, we’ll explore Moonbeam and the network’s benefits to explain why you might want to build on the...

Chiliz Chain Deep Dive – Why Build on Chiliz Chain? – Moralis Web3

Chiliz Chain Deep Dive – Why Build on Chiliz Chain? – Moralis Web3

by SB Crypto Guru News
September 10, 2024
0

In today’s article, we’ll explore the benefits of Chiliz to explain why you might want to build on this network....

NFT Rarity API – How to Get an NFT’s Rarity Ranking – Moralis Web3

NFT Rarity API – How to Get an NFT’s Rarity Ranking – Moralis Web3

by SB Crypto Guru News
September 6, 2024
0

Looking for the easiest way to get an NFT’s rarity ranking? If so, you’ve come to the right place. In...

How to Get Token Prices with an RPC Node – Moralis Web3

How to Get Token Prices with an RPC Node – Moralis Web3

by SB Crypto Guru News
September 3, 2024
0

Are you looking for an easy way to get token prices with an RPC node? If so, you’ve come to...

How to Get NFT Balances with One RPC Call – Moralis Web3

How to Get NFT Balances with One RPC Call – Moralis Web3

by SB Crypto Guru News
August 30, 2024
0

Did you know that with Moralis’ next-generation nodes, you can get NFT balances with just one RPC call? Our Extended...

Load More
Next Post
Low quantity killed 66% of cash in final 10 years

Low quantity killed 66% of cash in final 10 years

Core Scientific to Shut Down 37,000 Bitcoin Mining Rigs Belonging to Bankrupt Crypto Lender Celsius – Mining Bitcoin Information

Core Scientific to Shut Down 37,000 Bitcoin Mining Rigs Belonging to Bankrupt Crypto Lender Celsius – Mining Bitcoin Information

Facebook Twitter LinkedIn Tumblr RSS

CATEGORIES

  • Altcoin
  • Analysis
  • Bitcoin
  • Blockchain
  • Crypto Exchanges
  • Crypto Updates
  • DeFi
  • Ethereum
  • Metaverse
  • Mining
  • NFT
  • Regulations
  • Scam Alert
  • Uncategorized
  • Web3

SITE MAP

  • Disclaimer
  • Privacy Policy
  • DMCA
  • Cookie Privacy Policy
  • Terms and Conditions
  • Contact us

Copyright © 2022 - SB Crypto Guru News.
SB Crypto Guru News is not responsible for the content of external sites.

Welcome Back!

Login to your account below

Forgotten Password?

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • HOME
  • BITCOIN
  • CRYPTO UPDATES
    • GENERAL
    • ALTCOINS
    • ETHEREUM
    • CRYPTO EXCHANGES
    • CRYPTO MINING
  • BLOCKCHAIN
  • NFT
  • DEFI
  • WEB3
  • METAVERSE
  • REGULATIONS
  • SCAM ALERT
  • ANALYSIS

Copyright © 2022 - SB Crypto Guru News.
SB Crypto Guru News is not responsible for the content of external sites.