[Telegram Bot Tutorial - Part 4] Read BTC Address input and beautify response message
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:
Now, let's add this line after the request variable
:
const URL = "https://blockchain.info/balance?active=";
Our code will look like this now:
and then replace the URL in the get balance function like this:
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
:
- must start with
/balance
- 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:
after that, add a new line console.log(match)
before the get
function from request, like this:
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:
0 Match:
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:
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 :)
Let's save our code and run it, to see the result !
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 :
Remember to remove console.log(match)
first.
Now, save your code and try run it ;) to see whats the result.
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.
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 !
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.
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:
This will now reply the message sent by user. Let's save it and see the result.
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:
Result:
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 ~
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!
![](https://steemitimages.com/640x0/https://codingforspeed.com/images/i-love-coding.jpg)
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) :
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: