[Telegram Bot Tutorial - Part 4] Read BTC Address input and beautify response message

in #programming6 years ago

In this tutorial, we are going to make the bot to read user input. Allow user to check specific BTC address balance. We will also beautify the response message, adding bold or italic...etc.

If you didn't read the previous tutorial:

Part 0 Tutorial
Part 1 Tutorial
Part 2 Tutorial
Part 3 Tutorial


Before getting user's input, let's do a little change of our code.
This is our code from last tutorial:
image.png

Now, let's add this line after the request variable:

const URL = "https://blockchain.info/balance?active=";

Our code will look like this now:
image.png

and then replace the URL in the get balance function like this:

image.png

Do u still remember i said we need to put our string inside " " or ' ' ?
This time is a little bit different, we are using something called: Template Strings
You can put a variable inside your string by typing ${variable name}.

That means ${URL}39uAUwEFDi5bBbdBm5ViD8sxDBBrz7SUP4 is equal to https://blockchain.info/balance?active=39uAUwEFDi5bBbdBm5ViD8sxDBBrz7SUP4 which is the same URL as before.


Now, Let's get user's specific BTC address.

Remember how we identity command ? We are using something called Regular Expression .

To read user input, we can just simply edit the regular expression we are using to /^\/balance (.+)$/

This regular expression meaning that we need a string that is something like this: /balance xxx :

  1. must start with /balance
  2. must have something after /balance
    otherwise, the bot will not recognize the command.

Let's add a new variable into our arrow function called match like this:
image.png

after that, add a new line console.log(match) before the get function from request, like this:
image.png

Now, start the bot with node index.js and try the /balance command again. (Remember to save the file first)

Now, if you type /balance to your bot, your bot will not response, because /balance is not matching the regular expression. (You can try what kind of text will match the regular expression by using this website: Online Regex Tester)

You will see at least 1 match if the text you typed in matching the regular expression, otherwise it will show 0 match:

1 Match:
image.png

0 Match:
image.png

Now, go back to your telegram bot and type /balance x and see the console in the Visual Studio Code, see what is the output. The bot should responded you, and you will see something like this on your console:

image.png

This is the match variable, You can see it is an array with 5 elements. The second element of the array is what we need. When user type /balance address we can simply get the address by getting the second element of the match variable.

Now, we can go back to our code and edit it :)

image.png

Let's save our code and run it, to see the result !

image.png

It works great !

Before we beautify the response message, let's change the response message if the address is incorrect which will show NaN balance.

NaN meaning Not a Number this will appear when we trying to do some calculation on some variable that is not a number.

Let's test what is the json object if the address is not correct by adding this line before the sendMessage function of telegram:

console.log(json);

Your code will look like this now :
image.png

Remember to remove console.log(match) first.

Now, save your code and try run it ;) to see whats the result.

image.png

It turns out, if the address is incorrect, it will return Invalid Bitcoin Address else it will return a json object.

Great, now we know if json is an object, that means the address is correct, otherwise it is incorrect.
Let's edit the code now.

image.png

if(typeof json == "object") meaning that, if the type of json variable is an object, then it will do all the code inside it (Sending the balance of that address to user).
Otherwise, it will do all the code inside the else block (Sending message to user saying that's not a valid BTC address).

Let's save it and run the code !

image.png

Work as expected !


Now, we are going to format our response message, to make it looks better.

Documentation from Telegram
You can see all the parameters of sendMessage on the above link, you can see there is a parse_mode optional variable. This is what we are going to use. You can see there are different optional parameters you can use, you can try to use them.

image.png

This is how you add optional parameters for sendMessage function. If you want to add reply_to_message_id to the parameters, you can change the third argument of sendMessage to this:
image.png

This will now reply the message sent by user. Let's save it and see the result.

image.png

This is the result without reply and with reply.

So, we are going to use HTML to style our message.
There are total five style you can use to style the message in telegram.
HTML Style Click this link to learn more.

i will just tell you guys the few tags that i use the most.

<b>text</b> is bold
<i>text</i> is italic
<code>text</code> is code

You can edit the message just like this:

image.png

Result:

image.png

It looks much better now :)


We will check the balance of an address and notify you if it decreased or increased automatically in next tutorial. Stay tuned !

Like and share this post if this is helpful.
Comment if you want to ask me any question about this tutorial.
Thanks for supporting me :)
Bye ~

Sort:  

Hello! Your post has been resteemed and upvoted by @ilovecoding because we love coding! Keep up good work! Consider upvoting this comment to support the @ilovecoding and increase your future rewards! ^_^ Steem On!

Reply !stop to disable the comment. Thanks!

Congratulations @itdog! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You got your First payout
Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

Do not miss the last post from @steemitboard:

SteemitBoard Ranking update - Steem Power, Followers and Following added

Support SteemitBoard's project! Vote for its witness and get one more award!