aungzawpyae/botman-viber-driver

BotMan 的 Viber 驱动程序

dev-master 2023-07-20 05:06 UTC

This package is not auto-updated.

Last update: 2024-09-27 09:11:29 UTC


README

通过 BotMan 连接 Viber

Latest Version on Packagist

安装 & 设置

首先,您需要引入 Viber 驱动程序。

composer require polyskalov/botman-viber-driver

然后在创建 BotMan 实例之前加载驱动程序(仅在您不使用 BotMan Studio 的情况下)

DriverManager::loadDriver(\TheArdent\Drivers\Viber\ViberDriver::class);

// Create BotMan instance
BotManFactory::create($config);

此驱动程序需要有效的安全 URL 来设置 webhook 并接收来自聊天用户的事件和信息。这意味着您的应用程序应可通过 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 逻辑/控制器所在的 URL。如果您使用 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();