PART 2 Telegram Bot Tutorial – Bot Script and User Interaction

To try out BobBot for yourself, head over to telegram.me/BobRossBot

In the last post, I covered creating the bot in Telegram and setting up the webhook. In this post we’ll look at getting a script at the other end of that webhook and having your bot interact with the user.

BobBot.php

Our Bot is going to wait for a user to say /bob, and then reply with a nugget of wisdom from Bob Ross. BobBot will be written in PHP, but your bot can be written in any language really! I might cover nodejs in a future post. However, I’ll assume you don’t need a walkthrough of PHP! Check out learnXinYMinute’s PHP docs for a quick run through.

You can find BobBot on GitHub here, fully commented. I’ll go through each line here anyway. In the meantime, you can put that file up on your server, at the same domain as your webhook is set to point to. If you’ve changed your mind on the domain, simple create a new webhook. You can delete the old one by visiting

https://api.telegram.org/bot000000000:AAAAa0aAA_aaA-Aaaa0A0Aa_a0aa0A0AAAA/setwebhook

then make the new one as per part 1’s explanation. Now, let’s look at the code line per line!

BobBot’s Code

$botToken = "000000000:AAAAa0aAA_aaA-Aaaa0A0Aa_a0aa0A0AAAA";
$website = "https://api.telegram.org/bot".$botToken;

We set up our bot’s token here so that BobBot knows to send replies to our API link. Be sure to put your token in here!


$content = file_get_contents("php://input");

You can read all about file_get_contents here. Long story short, when you send a message to Bobbot in the telegram client, it pings your API link. Your webhook sends than info onto BobBob.php. That information comes in as php://input, which get’s put into the $content variable

$update = json_decode($content, TRUE);
$message = $update["message"];

We take $content, and turn it into an array called update. Then I’m grabbing the message element of that array. This is the JSON version of the layout currently inside $message

{
  "message_id":2,
  "from":{"id":126101111,"first_name":"Paul"},
  "chat":{"id":126101111,"first_name":"Paul","type":"private"},
  "date":1446297296,
  "text":"A Message"
}

Now, we can easily grab all the elements we need to send a reply.

$chatId = $message["chat"]["id"];
$text = $message["text"];

Now we have the text we were sent, and the chatId the message came from. This makes checking if we should reply and replying easy!


 

$bobWisdom is just an array of Bob Ross quotes. I wont reproduce them here. But remember! There’s no such thing as accidents, only happy little mistakes!

$todaysWisdom = array_rand($bobWisdom, 1);

$todaysWisdom grabs a random array element integer from $bobWisdom. You can read more about it here. Remember, $todaysWisdom is an integer, not a string!


 

if($text === "/bob") {
  file_get_contents($website."/sendmessage?chat_id=".$chatId."&text=".$bobWisdom[$todaysWisdom]);
}

This is where most of the magic is happening. We check if the user said /bob. If they didn’t nothing will happen. If they do, we do another file_get_contents and this time we specify our bot API link and we call the sendmessage method using the $chatId for the chat_id parameter and our random wisdom element as our text parameter. This is the equivalent of going to the URL:

https://api.telegram.org/bot000000000:AAAAa0aAA_aaA-Aaaa0A0Aa_a0aa0A0AAAA/sendmessage?text=Happy%20Little%20Clouds&chat_id=1111111111

Conclusion
Our BobBot is now being aimed at by our Webhook and is responding back to the Bot API. We have a working telegram bot!

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>