📜 ⬆️ ⬇️

BotMan acquaintance

I want to talk about the PHP framework for creating BotMan bots ( Website ). Botman can be used both as a library and as an extension Laravel (BotMan Studio).


The advantage of this framework is that the code for the bot can work for a variety of platforms:


Code example:

<?php $botman->hears('Hello BotMan!', function($bot) { $bot->reply('Hello!'); $bot->ask('Whats your name?', function($answer, $bot) { $bot->say('Welcome '.$answer->getText()); }); }); $botman->listen(); 


Result:



Install BotMan Studio


Create a new project in the botman folder:

 composer create-project --prefer-dist botman/studio botman 

You can check the operation via the command line:

 $ php artisan botman:tinker You: test BotMan: hello! 

Or check through the browser. Run the test server:

 php artisan serve Laravel development server started: <http://127.0.0.1:8000> 

Open the browser 127.0.0.1 : 8000 / botman / tinker

And write hi or start conversation for verification.

How to create a bot for a telegram?


The first step is to register the bot with @BotFather.





All bot is registered and we have a token.

The first step is to install the telegram driver:

 composer require botman/driver-telegram 

Option A, if you use BotMan Studio:

 php artisan botman:install-driver telegram 

In file

 config/botman/telegram.php 

add your telegram token

 'telegram' => [ 'token' => 'YOUR-TELEGRAM-TOKEN-HERE', ] 

Option B, if you do not use BotMan Studio:

 DriverManager::loadDriver(\BotMan\Drivers\Telegram\TelegramDriver::class); // Create BotMan instance BotManFactory::create($config); 

We register Webhook in Telegram


In order for Telegram to know how it can communicate with your BotMan bot, you need to register the URL that BotMan works with in Telegram.

You can do this by sending a POST request to this URL:

 https://api.telegram.org/bot<YOUR-TELEGRAM-TOKEN-HERE>/setWebhook 

This POST request requires only one url parameter with a URL pointing to the BotMan route. If you use Botman Studio, it will be:

 https://site.domain/botman 

HTTPS is required for security reasons.

Instead of manually sending a request to Telegram, you can use the console command to register your Webhook. You can pass the --output flag to see the json Telegram response.

 $ php artisan botman:telegram:register 

Good luck everyone. Make bots.

Source: https://habr.com/ru/post/438936/