Chat bot’s popularity has been growing these past years because of it’s great functionality and reliability to handle some cases in business. And also, Bot has been supported by lots of instant messaging service provider such as Telegram, Line, Facebook, etc. On the other side, Laravel is one of the most popular framework to build web applications written in PHP. In this article we will try to make a simple chat bot for Telegram messaging platform. This tutorial will cover some topics such as:
Here are some requirements before getting started:
The finished project example is available on GitHub. Create a new Laravel ProjectYou may install the project in any directory in your machine as you like. For further information to create a Laravel project, check out instructions in the . Let’s get started! Open your terminal and in your local directory simply run composer create-project --prefer-dist laravel/laravel blog Now, your should have To make sure everything is going as planned, go inside Great! Now we have our Laravel project ready to roll. Next, let’s create our Telegram Bot! If you want to learn more about Telegram bot, I prefer you to explore about it here. First, open your Telegram app, and go find @BotFather. BotFather is a bot created by Telegram to manage all other bots (imagine BotFather is a GodFather of all bots). BotFatherStart a conversation with BotFather, by tapping
In the message sent by BotFather, you will get your bot’s access token. This token will be use to identify your bot and work as your bot’s secret signature key. Make sure you keep this private because if someone else got your token, they could access and do commands on your bot’s behalf. Okay! Now we have our Bot, our In this tutorial, we will use telegram-bot-sdk. This SDK helps us to do any methods or operations easily from our Laravel project. This time, we will use <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {0 version (the stable version is quite out-dated; this article will be updated after the v3.0 became stable version). Go to our Laravel project directory and run <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {1. Then after the installation completed, run <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {2 to publish telegram configuration file. The configuration file should be located now in <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {3. Now, let’s connect everything we’ve done so far! Inside <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {4 you should see lots of config regarding to the bot. Let’s take a look at <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {5 key. Here inside <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {6key, change <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {7 value to your Telegram bot username and change <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {8 value to your <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {9 given by BotFather.Preview of telegram.php fileHandling Telegram Updates Now, we got our Bot access token linked with our project. Next, let’s handle Telegram Updates. What is Telegram Update? According to official docs,
So by that, Updates are an Object containing every interaction made to our bots. For example when someone initiate a chat with our bot, an update will be made. Or when our bot added to a chat group/supergroup, an update will be made. To access Telegram Updates, we will make a new API route to test it out. Inside ...Route::post('bot/sendmessage', function() {1, let’s make a new API route to get telegram updates as shown below. <?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() { Alrighty! We got API route called ...Route::post('bot/sendmessage', function() {2 to retrieve updates from Telegram server. And also, don’t forget to use ...Route::post('bot/sendmessage', function() {3 class. By successfully getting Updates, we could choose how to act accordingly to every action user did with our bot including inviting to chat group, processing commands, or just simply replying to a chat. This method of getting updates is using to fetch updates. For advanced usage, you can set up for better experience. Sending MessagesLastly, sending messages. In this section, we will try to send a message to group, supergroup, channel or simply to individual user. We will use ...Route::post('bot/sendmessage', function() {4 method to send a message. Inside ...Route::post('bot/sendmessage', function() {1, let’s make a new API route as shown below: ...Route::post('bot/sendmessage', function() { Let’s get through the details. As we can see, ...Route::post('bot/sendmessage', function() {4 receive few parameters such as :
|