musoftware / botman-viber-driver
BotMan 的 Viber 驱动程序
Requires
- php: >=7.1
- ext-json: *
- botman/botman: ~2.0
Requires (Dev)
- ext-curl: *
- botman/studio-addons: ~1.0
- illuminate/contracts: ~5.5.0
- phpunit/phpunit: ~5.0
This package is auto-updated.
Last update: 2024-09-29 05:36:30 UTC
README
通过 BotMan 连接 Viber
安装与设置
首先,您需要拉取 Viber 驱动程序。
composer require musoftware/botman-viber-driver
然后在创建 BotMan 实例之前加载驱动程序(仅在您不使用 BotMan Studio 的情况下)
DriverManager::loadDriver(\TheArdent\Drivers\Viber\ViberDriver::class); // Create BotMan instance BotManFactory::create($config);
此驱动程序需要一个有效且安全的 URL 来设置 webhooks 并从聊天用户接收事件和信息。这意味着您的应用程序应通过 HTTPS URL 访问。
要将 BotMan 与您的 Viber Bot 连接,您首先需要按照官方指南创建您的 Viber Bot 和访问令牌。
一旦您获得了访问令牌,将其放在您的 .env 文件中,例如 VIBER_TOKEN=YOUR-VIBER-TOKEN-HERE。在那里它会自动加载到您的 config/botman/viber.php 文件中。
如果您不使用 BotMan Studio,请将这些行添加到您从 BotManFactory 创建对象时传递的 $config 数组中。
'viber' => [
'token' => 'YOUR-VIBER-TOKEN-HERE',
]
注册您的 Webhook
为了让您的 Viber Bot 知道如何与您的 BotMan bot 通信,您必须将 BotMan 运行在的 URL 注册到 Viber。
您可以通过向此 URL 发送 POST 请求来完成此操作:
https://chatapi.viber.com/pa/set_webhook
此 POST 请求需要一个名为 url 的参数,该参数指向您的 BotMan 逻辑/控制器。如果您使用 BotMan Studio,它将是:https://yourapp.domain/botman。出于安全原因,HTTPS 是必需的。
{
"url":"https://my.host.com",
"event_types":[
"delivered",
"seen",
"failed",
"subscribed",
"unsubscribed",
"conversation_started"
],
"send_name": true,
"send_photo": true
}
您可以在官方文档中了解请求中的其他字段。
您可以使用控制台命令而不是手动向 Viber 发送请求来注册您的 Webhook。
php artisan botman:viber:register
示例
<?php /** @var \BotMan\BotMan\BotMan $botman */ $botman = resolve('botman'); $botman->on( 'conversation_started', static function (array $payload, BotMan $bot) { $bot->reply('Hi, ' . $bot->getUser()->getUsername()); } ); $botman->hears('hi', static function (BotMan $bot) { $bot->reply('👋'); }); $botman->fallback(static function (BotMan $bot) { $bot->reply('I do not understand'); }); $botman->listen();