Automated bots are a way to offer customized data to your users based on their requests. Laravel and the Botman framework provide the tools to create useful bots. In this tutorial, you will be creating a Telegram bot for dog lovers using Dog API and this is what it looks like:
The first step we will be taking to create this Bot is installing Laravel and Botman. But before doing that let’s first take a quick look at what Botman is and how it works:
BotMan is a framework agnostic PHP library that is designed to simplify the task of developing innovative bots for multiple messaging platforms, including Slack, Telegram, Microsoft Bot Framework, Nexmo, HipChat, Facebook Messenger and WeChat.
Marcel Pociot, the creator of Botman, has already saved us some time by creating Botman Studio which is a ready-to-use and up-to-date Laravel application with Botman and other testing tools (covered later) included.
Go ahead and create a new project:
Now that we have a fresh installation of Laravel and Botman let’s check if everything is working fine. Open your terminal and run this command:
If you typed in “Hi” and the Bot replied with “Hello” then you are ready to go.
Our Bot should be able to respond to different message types, and this is a list of the features we will be implementing, but of course you can always add any additional commands you would love your Bot to listen to:
Let’s clear the routes/botman.php
file and start from scratch.
In order to receive a random dog photo from the Bot you must send it /random
and this is how we tell it to respond to that exact command:
In your routes/botman.php
file:
Go ahead and create the controller:
This is what it should look like:
We first created an instance of our DogService
(app//services/DogService.php
), which will be responsible for making API calls to our endpoints and fetching the image, and here is what it looks like:
For this one, we will use the command /b {breed}
and same as above, we open the routes/botman.php
file and tell the Bot to listen for that command by adding this line:
We will be using the same controller we used before. Open AllBreedsController
and add this method to it:
Let’s define the byBreed
method in our service class by opening the DogService class and adding this method to it:
And don’t forget to add the endpoint const
used above to the same file:
For sub-breed photos, let’s use the command /s {breed}:{subBreed}
Creating the controller:
And we’ll define the random
method as shown bellow:
And we add the needed endpoint and method to our DogService
class:
A conversation is what you’d normally be using when building your Bots and the docs describes it as:
When it comes to chat bots, you probably don’t want to simply react to single keywords, but instead, you might need to gather information from the user, using a conversation. Let’s say, that you want your chat bot to provide an elegant user onboarding experience for your application users.
Let’s create our conversation by adding this line to our routes/botman.php
file:
Generate the controller:
And define an index
method inside that class:
If you are using Botman Studio you should already have a Conversations folder inside the App folder, so go ahead and create a new class inside that folder and name it DefaultConversation.php:
Finally, we need to let the user know when they send a message our Bot does not recognize and we can do that by using the fallback method. Open your routes/botman.php
and this line:
Create the controller:
And we simply return the message we want the user to see:
After successfully creating and testing our commands it’s now time to integrate it with Telegram. To do that we will need to pull the Telegram driver provided by Botman:
We successfully created our Bot, defined the commands and tested it out now it’s time to create a Telegram Bot. Open the app and search for BotFather, type /newbot, enter the name the username for your bot and you are good to go.
Add this to your .env
file and replace YOUR_TOKEN with the token Telegram gave you:
Because Telegram requires a valid and secure URL to set up webhooks and receive messages from your users we will be using ngrok or you can deploy your app on a server and set up an SSL certificate, but to for the demo we will stick to ngrok. Bowse to their Download Page and click the download button that matches your operating system.
Now cd
into your app folder and run php artisan serve
Time to run ngrok, cd into the folder where ngrok is and run ./ngrok http 8000
The final step is linking our app to the Telegram Bot we created earlier and to do that we will make a POST request to this URL and pass the URL ngrok generated for us:
You can do this with Postman or CURL by running this command:
If you did that correctly you should receive this exact JSON response:
Send a random dog photo from all breeds:
Send a random dog photo by its breed:
Send a random dog photo by a its breed and sub-breed:
Have a conversation and provide help:
Respond to unrecognised commands:
I hope you found this tutorial useful, if you are following along and you created your own Bot.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Thank you very much. Detailed tutorial and easy to understand.