BotMan熟人

我想谈谈用于创建BotMan机器人( 站点 )的PHP框架。 Botman既可以用作库,也可以用作Laravel扩展(BotMan Studio)。


该框架的优点是该机器人的代码可以在许多平台上运行:

  • 亚马逊Alexa
  • 思科火花
  • Facebook Messenger
  • 环聊
  • 即时通讯
  • Microsoft Bot框架
  • Nexmo
  • 松弛
  • 电报
  • 特威里奥
  • 网页
  • 微信
  • VK

代码示例:

<?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(); 


结果:



安装BotMan Studio


在botman文件夹中创建一个新项目:

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

您可以通过命令行检查操作:

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

或通过浏览器检查。 运行测试服务器:

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

在浏览器中打开127.0.0.1:8000 / botman / tinker

并打个招呼或开始对话进行检查。

如何创建电报机器人?


首先,您需要使用@BotFather注册该机器人。





所有漫游器都已注册,我们有一个令牌。

首先,您需要安装电报驱动程序:

 composer require botman/driver-telegram 

如果使用的是BotMan Studio,则为选项A:

 php artisan botman:install-driver telegram 

在文件中

 config/botman/telegram.php 

添加您的电报令牌

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

如果您不使用BotMan Studio,请选择选项B:

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

在电报中注册Webhook


为了使Telegram知道如何与BotMan bot通信,您需要在Telegram中注册BotMan工作所在的URL。

您可以通过向该URL发送POST请求来做到这一点:

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

此POST请求仅需要一个url参数,其URL指向BotMan路由。 如果您使用的是Botman Studio,则将是:

 https://site.domain/botman 

出于安全原因,需要HTTPS。

您可以使用console命令注册Webhook,而不是手动向Telegram发送请求。 您可以传递--output标志来查看json Telegram响应。

 $ php artisan botman:telegram:register 

祝大家好运。 做机器人。

Source: https://habr.com/ru/post/zh-CN438936/


All Articles