Cara menggunakan does telegram use php?

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:

  1. Creating new Laravel project.
  2. Creating Telegram bot.
  3. Integrating our Telegram bot to the Laravel project.
  4. Handling Telegram updates.
  5. Sending messages.
Getting Started

Here are some requirements before getting started:

  1. Make sure you have Composer installed in your machine. If you haven’t, go to the official Composer website and follow the instructions.
  2. Basic knowledge of PHP programming language.
  3. Telegram account, and the Telegram apps installed in your device.

The finished project example is available on GitHub.

Create a new Laravel Project

You 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 blog folder containing Laravel project. Go to that directory by running cd blog.

To make sure everything is going as planned, go inside blog directory and run php artisan serve. Your application should run at localhost:8000.

localhost:8000 previewCreate a Telegram Bot

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).

BotFather

Start a conversation with BotFather, by tapping Start. After that, type /newbot and follow all of the instruction provided. By the end of the interaction, you will get some of informations like this:

Done! Congratulations on your new bot. You will find it at t.me/YOUR_BOT_USERNAME. You can now add a description, about section and profile picture for your bot, see /help for a list of commands. By the way, when you’ve finished creating your cool bot, ping our Bot Support if you want a better username for it. Just make sure the bot is fully operational before you do this.

Use this token to access the HTTP API:
<YOUR_BOT_TOKEN>

For a description of the Bot API, see this page: https://core.telegram.org/bots/api

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 BOT_TOKENand our Laravel project ready to go!

Integrating Telegram Bot to Laravel

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() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
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() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
1. Then after the installation completed, run
<?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
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() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
3.

Now, let’s connect everything we’ve done so far! Inside

<?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
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() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
5 key. Here inside
<?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
6key, change
<?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
7 value to your Telegram bot username and change
<?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
8 value to your
<?phpuse Illuminate\Http\Request;use Telegram;...Route::post('/bot/getupdates', function() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});
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,

Every interaction user made with your bot will be called as

...Route::post('bot/sendmessage', function() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...
0. Every will be formatted as JSON-serialized objects.

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() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...
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() {
$updates = Telegram::getUpdates();
return (json_encode($updates));
});

Alrighty! We got API route called

...Route::post('bot/sendmessage', function() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...
2 to retrieve updates from Telegram server. And also, don’t forget to use
...Route::post('bot/sendmessage', function() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...
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 Messages

Lastly, 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() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...
4 method to send a message. Inside
...Route::post('bot/sendmessage', function() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...
1, let’s make a new API route as shown below:

...Route::post('bot/sendmessage', function() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...

Let’s get through the details. As we can see,

...Route::post('bot/sendmessage', function() {
Telegram::sendMessage([
'chat_id' => 'RECIPIENT_CHAT_ID',
'text' => 'Hello world!'
]);
return;
});
...
4 receive few parameters such as :

  1. ...Route::post('bot/sendmessage', function() {
    Telegram::sendMessage([
    'chat_id' => 'RECIPIENT_CHAT_ID',
    'text' => 'Hello world!'
    ]);
    return;
    });
    ...
    7 : This is your recipient’s chat ID. Every user, channel, group, or supergroup has it’s own unique chat ID. To get the
    ...Route::post('bot/sendmessage', function() {
    Telegram::sendMessage([
    'chat_id' => 'RECIPIENT_CHAT_ID',
    'text' => 'Hello world!'
    ]);
    return;
    });
    ...
    7, the easiest way is try to chat with our bot and get the
    ...Route::post('bot/sendmessage', function() {
    Telegram::sendMessage([
    'chat_id' => 'RECIPIENT_CHAT_ID',
    'text' => 'Hello world!'
    ]);
    return;
    });
    ...
    7 via blog0 method. For group, supergroup or channel, we need to invite the bot first as member to get the
    ...Route::post('bot/sendmessage', function() {
    Telegram::sendMessage([
    'chat_id' => 'RECIPIENT_CHAT_ID',
    'text' => 'Hello world!'
    ]);
    return;
    });
    ...
    7.
  2. blog2 : String or integer to be sent as a message.

Note: You could only send a message to user using this method if the specific user ever started a conversation with the bot. For group, supergroup or channel, the bot need to be set as Admin to be able to send messages.