Can Bots be useful in blockchain?
Bots are becoming relevant nowadays due to their usefulness. You may have noticed people using bots to gain visibility, send messages to thousands of people, and gather information. They mostly run on messaging platforms.
\ So, the answer to the question is YES. These bots are useful. They help get first-hand information about blockchain networks like Ethereum and Bitcoin, without visiting block explorers or wallets to check for information.
\ In this article, you will learn how to build a Telegram bot that fetches data from Rootstock using RootStock API.
Tool and TechnologiesYou will be making use of BotFather; Botfather is a Bot creator.
Click on BotFather, and add it to your telegram.
Click on start, and it’s going to give different commands (study that later).
To create a new bot, input /newbot.
4. Enter the name of your bot.
\ 5. Enter your username for your bot, and end it with ”Bot.”
![](https://cdn.hackernoon.com/images/h9zh5laZcuVhjnaK4lBLPxYH14f2-2024-10-01T00:30:08.275Z-q8y27bwwq09vrb4prp9y49b9)After that, you will get a response showing that you’ve created your bot. This response will carry your bot URL and access token.
Brief Explanation:
\
node-telegram-bot-api: This is a library that interacts with the Telegram Bot API. It allows you to create and manage Telegram bots, handle messages, and perform various bot-related tasks.
\
web3: This is a JavaScript library for interacting with the Ethereum virtual machine (EVM). It provides an interface to interact with smart contracts, send transactions, and query blockchain data e.g., Rootstock.
\
dotenv: This library loads environment variables from a .env file into process.env. It helps manage configuration settings and secrets securely.
\
Body-parser: This middleware parses incoming request bodies in a middleware before your handlers, available under the req.body property. It supports parsing JSON, URL-encoded, and raw bodies.
\
express: This is a fast, unopinionated, minimalist web framework for Node.js. It provides a robust set of features for web and mobile applications.
\
nodemon: This is a utility that monitors for any changes in your source and automatically restarts your server. It helps in development by reducing the need to manually restart the server after changes.
\
Create a new folder in the root directory and a new file index.js
Your folder should look like this:
Writing the Bot Command ScriptIn this section, you will program the bot by stating the commands. All this action will be executed inside the index.jsfile.
\ Before you start, you need to define how to run the app, and specify the kind of module you are using. In this case, you will be using the [ECMAScript modules (ESM) ]()and npm start to run the app.
DefinitionsendHelpMessage constructs and sends a help message listing available commands to a specified chat. It takes chatId as a parameter to specify the chat where the message will be sent.
\
The /help command tells the bot to respond when it receives a message matching /help. It extracts the chatId from the message and calls sendHelpMessage to send the help message to that chat.
This provides a friendly welcome when the bot is being launched. In this case, if the user clicks on START, they will get “Hello! Welcome to the RootstockBot.”
![](https://cdn.hackernoon.com/images/h9zh5laZcuVhjnaK4lBLPxYH14f2-2024-10-01T00:30:08.112Z-i369mfvjzjvqlpu6abdo9zqz)await web3.eth.getBalance(walletAddress) : This method fetches the wallet address from the Rootstock network. It takes the inputted wallet address as a parameter.
\
web3.utils.fromWei(balance, “ether”): This method converts the balance from wei to ether e.g., if the balance is 195881312000000, it converts it to 0.000195881312.
await web3.eth.getGasprice() method fetches the current gas price and converts the price to ether using the web3.utils.fromWei(gasprice, “ether”) method.
\
This command defines the number of transactions performed by an account. It uses the await web3.eth.getTransactionCount method taking the inputted wallet address and returning the number of transactions.
![](https://cdn.hackernoon.com/images/h9zh5laZcuVhjnaK4lBLPxYH14f2-2024-10-01T00:30:08.047Z-kbykw6c4mcc5zbzk60l644np)This command uses the await web3.eth.getBlockNumber() to fetch the latest number on Rootstock network.
\
This event listener checks incoming messages to see if they match any available commands in the commands object and sends an error message with available commands.
\
To ensure that the server starts and listens for incoming requests.
\ Add the following:
\
:::info All the command code are to be written inside the main() function.
:::
\ Add the following to handle errors that might occur during the execution of the main function.
main().catch(console.error);\ So, your script should look like this:
import dotenv from "dotenv"; import express from "express"; import bodyParser from "body-parser"; import TelegramBot from "node-telegram-bot-api"; import Web3 from "web3"; // Load environment variables from .env file dotenv.config(); async function main() { const app = express(); const port = 3000; // Parse the request body as JSON app.use(bodyParser.json()); // Create a TelegramBot instance with your bot token const botToken = process.env.BOT_TOKEN; const bot = new TelegramBot(botToken, { polling: true }); const web3 = new Web3( `https://rpc.testnet.rootstock.io/${process.env.RSK_API_KEY}` ); const commands = { start: '/start - Start the bot', balance: '/balance - Check the balance of a wallet', transactions: '/transactions - Get recent transactions of a wallet', latestblock: '/latestblock - Get the latest block number', help: '/help - Show this help message', gasprice: '/gasprice - Get the current gas price', }; const sendHelpMessage = (chatId) => { const helpMessage = `You can use the following commands:\n` + Object.values(commands).join('\n'); bot.sendMessage(chatId, helpMessage); }; bot.onText(/\/help/, (msg) => { const chatId = msg.chat.id; sendHelpMessage(chatId); }); // Handle /start bot.onText(/\/start/, (msg) => { const chatId = msg.chat.id; bot.sendMessage(chatId, `Hello! Welcome to the RootstockBot.`); }); // Handle /balance command bot.onText(/\/balance (.+)/, async (msg, match) => { const chatId = msg.chat.id; const walletAddress = match[1]; // Extract the address from the command try { const balance = await web3.eth.getBalance(walletAddress); bot.sendMessage(chatId, `The balance of the address ${walletAddress} is ${web3.utils.fromWei(balance, 'ether')} ETH.`); } catch (error) { bot.sendMessage(chatId, 'Failed to fetch the balance. Please try again later.'); console.error(error); } }); // Handle /gasprice command bot.onText(/\/gasprice/, async (msg) => { const chatId = msg.chat.id; try { const gasPrice = await web3.eth.getGasPrice(); const gasInEth = web3.utils.fromWei(gasPrice, "ether"); bot.sendMessage(chatId, `The current gas price is ${gasInEth} eth.`); } catch (error) { bot.sendMessage(chatId, `Failed to fetch gas price. Please try again later.`); console.error(error); } }); // Handle /transactions command bot.onText(/\/transactions (.+)/, async (msg, match) => { const chatId = msg.chat.id; const walletAddress = match[1]; if (!walletAddress) { bot.sendMessage(chatId, 'Please provide a valid address.'); return; } try { const transactionCount = await web3.eth.getTransactionCount(walletAddress); bot.sendMessage(chatId, `${walletAddress} has made ${transactionCount} transactions.`); } catch (error) { bot.sendMessage(chatId, `Failed to fetch transactions for ${walletAddress}. Please try again later.`); console.error(error); } }); // Handle /latestblock command bot.onText(/\/latestblock/, async (msg) => { const chatId = msg.chat.id; try { const latestBlock = await web3.eth.getBlockNumber(); bot.sendMessage(chatId, `The latest block number is ${latestBlock}.`); } catch (error) { bot.sendMessage(chatId, 'Failed to fetch the latest block number. Please try again later.'); console.error(error); } }); // Handle unrecognized commands bot.on('message', (msg) => { const chatId = msg.chat.id; const text = msg.text; if (!Object.keys(commands).some(cmd => text.startsWith(`/${cmd}`))) { bot.sendMessage(chatId, `Sorry, I didn't recognize that command. Please use the following commands:\n` + Object.values(commands).join('\n')); } }); // Start the Express server app.listen(port, () => { console.log(`Server is running on port ${port}`); }); } main().catch(console.error);\ Push your folder to GitHub, and [deploy your application using Render.]()
\ You can add more commands using the avaliable RPC API Methods.
ConclusionThis article has guided you in creating a telegram bot that Query data from the RootStock network.
Demo below:
https://youtu.be/ve9QO9G44as?embedable=true
Referencehttps://youtu.be/Ubg-rwcl3no?si=sfUj6J0Nt9hSPXLx&embedable=true
\ \
All Rights Reserved. Copyright , Central Coast Communications, Inc.