Bots are third-party applications that run inside Telegram. Users can interact with bots by sending them messages. RIF Name Service enables the identification of blockchain addresses by human-readable names, just like DNS for websites, but in a decentralized manner. We are going to build a Telegram bot that resolves addresses for requested domains.

First we need to create a new Telegram bot with the @BotFather1. The @BotFather will tell us the API key to use to control the bot using HTTPS requests to the Bot API. Then, in response to messages, we will query RNS service to answer with the resolved addresses2.

So the recipe is:

  1. Create a bot and get the API key
  2. Catch bot messages with a Node application
  3. Response to message with blockchain service information

Find more about RNS in this blogpost

Creating a bot

First of all, you need to have the Telegram application and an account. Then we can just create a new bot for free:

  1. Open the app and search the @BotFather, he will help us to create the bot.

  2. A message will appear explaining what you can do with the @BotFather, go ahead with 'Start'.

  3. Let's now go for /newbot option. Send him the keyword /newbot and follow the steps.

  4. You can now chat with your bot going into its link, but nothing will happen.

Connecting to the Bot API

11fac89

We are going to create a Node application to connect to our bot and receive all messages that arrive to him.

Find the public repo: rns-bot

  1. Create a new Node project
    mkdir rns-bot
    cd rns-bot
    npm init
    
  2. Install the Telegram Bot API3
    npm i node-telegram-bot-api
    
  3. Create a new index.js file with the following:
const TelegramBot = require('node-telegram-bot-api');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });

// Matches "/addr [whatever]"
bot.onText(/\/addr (.+)/, (msg, match) => {
  // 'msg' is the received Message from Telegram
  // 'match' is the result of executing the regexp above on the text content
  // of the message

  const chatId = msg.chat.id;
  const resp = `I received ${match[1]}`; // the captured [whatever]

  // answer with resp message
  bot.sendMessage(chatId, resp);
});

Run it with:

node index.js

Connect to the RIF Name Service

c9956a7

This is really easy to achieve combining RSK public nodes4 with the RNS Javascript library.

Install Web3.js client5 to connect to RSK through public nodes and RNS library5 to make queries to the service.

npm i web3 @rsksmart/rns

Now we are going to:

  • Connect to the RSK network with
    const Web3 = require('web3');
    
    const web3 = new Web3('https://public-node.rsk.co');
    
  • Connect to the RNS service with
    const RNS = require('@rsksmart/rns');
    
    const rns = new RNS(web3);
    
  • Query the address of a domain with
    rns.addr(domain)
        .then(addr => { })
        .catch(error => { })
    

Putting it all together:

const TelegramBot = require('node-telegram-bot-api');
const Web3 = require('web3');
const RNS = require('@rsksmart/rns');

// replace the value below with the Telegram token you receive from @BotFather
const token = 'YOUR_TELEGRAM_BOT_TOKEN';

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });

// Connect to the RSK network
const web3 = new Web3('https://public-node.rsk.co');

// Connect to the RNS service
const rns = new RNS(web3);

// Matches "/addr [whatever]"
bot.onText(/\/addr (.+)/, (msg, match) => {
  // 'msg' is the received Message from Telegram
  // 'match' is the result of executing the regexp above on the text content
  // of the message

  const chatId = msg.chat.id;

  // Query the address of the captured [whatever]
  rns.addr(match[1])
    .then(addr =>  { // answer the address
      const resp = `The address is ${addr}`;
      bot.sendMessage(chatId, resp)
    })
    .catch(error => { // answer error when not found and a reference if existent
      const resp = `Error: ${error.message}. ${error.ref ? `Find more info at ${error.ref}` : ``}`;
      bot.sendMessage(chatId, resp)
    });
});

Save your file and run it again with

node index.js

The result:

This is it! We got it.

Summary

We created a new Bot, and we connected to it via HTTP with a simple Node server. We programmed our bot to answer all the messages received prepended with /addr, with the RSK address of the domain specified or an error message if appropriate.

Thanks!

Want to improve it?

This are some cool things you can do to improve your project

Hide the API key on your repo

3717cc3

You can use dotenv library7 to move the API key out of your code easily:

  1. Install dotenv library
    npm install dotenv
    
  2. Create a .env file
    TELEGRAM_BOT_TOKEN=YOUR_TELEGRAM_BOT_TOKEN
    
  3. Use it with
    require('dotenv').config();
    process.env.TELEGRAM_BOT_TOKEN
    
  4. Add .env to .gitignore file

The project should now look like:

require('dotenv').config()
const TelegramBot = require('node-telegram-bot-api');
const Web3 = require('web3');
const RNS = require('@rsksmart/rns');

// replace the value below with the Telegram token you receive from @BotFather
const token = process.env.TELEGRAM_BOT_TOKEN;

// Create a bot that uses 'polling' to fetch new updates
const bot = new TelegramBot(token, { polling: true });

/* ... */

Change the bot info

You can achieve this talking to @BotFather

Send him a message saying /mybots

Now choose the bot of yours you want to update and go to ‘Edit bot’

Choose the options you want to configure. I configured my bot to look like this setting each of the options available.

More info

  1. Telegram bots
  2. RIF Name Service
  3. Bot API
  4. RSK Public nodes
  5. Web3.js client
  6. RNS Javascript library
  7. dotenv library