alserom/viber-php

用于与Viber REST API协同工作以及为Viber平台开发机器人的非官方库。

1.0.0 2019-04-25 15:53 UTC

This package is auto-updated.

Last update: 2024-09-26 04:54:59 UTC


README

Build Status Minimum PHP Version Coding Style Software License

使用此库开发Viber平台机器人或简单与Viber REST API协同工作。

注意:要与Viber API协同工作,您必须有一个认证令牌。请访问partners.viber.com,创建机器人账户并获取令牌。

安装

composer require alserom/viber-php

为了正确使用此包,您还需要安装PSR-17请求/响应工厂和PSR-18 HTTP客户端。
您可以在以下位置找到这些实现的包

示例

composer require nyholm/psr7 kriswallsmith/buzz

用法

此页面仅显示基础知识。对于高级用法,请阅读完整的文档

如果您想快速在实战中尝试此库,可以使用viber-bot-examples存储库中的代码。

创建对象

// Any PSR-17 implementation. In this case, we use 'nyholm/psr7' package.
$psr17Factory = new \Nyholm\Psr7\Factory\Psr17Factory();

// Any PSR-18 implementation. In this case, we use 'kriswallsmith/buzz' package.
$psr18Client = new \Buzz\Client\Curl($psr17Factory);

/* A simple object for getting all PSR-17 factories.
 * If you have an object which implements all PSR-17 interfaces, you can use a static method. E.g:
 * $psr17 = \Alserom\Viber\Psr17::useForAll($psr17Factory);
 */
$psr17 = new \Alserom\Viber\Psr17(
    $psr17Factory, // \Psr\Http\Message\RequestFactoryInterface
    $psr17Factory, // \Psr\Http\Message\ResponseFactoryInterface
    $psr17Factory, // \Psr\Http\Message\ServerRequestFactoryInterface
    $psr17Factory, // \Psr\Http\Message\StreamFactoryInterface
    $psr17Factory, // \Psr\Http\Message\UploadedFileFactoryInterface
    $psr17Factory  // \Psr\Http\Message\UriFactoryInterface
);

$token = 'YOUR-AUTHENTICATION-TOKEN';

/* An object for work with Viber API.
 * As a fourth argument, you can pass an array of options. See the full documentation for more info.
 */
$api = new \Alserom\Viber\Api($token, $psr17, $psr18Client);

/* An object for creating a logic of Viber bot.
 * As a second argument, you can pass an array of options. See the full documentation for more info.
 */
$bot = new \Alserom\Viber\Bot($api);

设置Webhook

$url = 'YOUR-HTTPS-WEBHOOK-URL';
$webhook = new \Alserom\Viber\Entity\Webhook($url);

// If you want that your bot receives user names instead of placeholder values.
// $webhook->setSendName(true);

// If you want that your bot receives user photos instead of placeholder values.
// $webhook->setSendPhoto(true); 

// If you want to filter which events would get a callback for.
// $webhook->setEventTypes(['delivered', 'seen', 'conversation_started']); 

$api->setWebhook($webhook);

发送消息

从头开始构建消息

$message = new \Alserom\Viber\Message('text');
$message
    ->setText('Hello World!')
    ->setTo(new \Alserom\Viber\Entity\User('USER-IDENTIFIER-HERE'));

// $response is a \Alserom\Viber\Response\Type\SendMessageResponse object.
$response = $api->sendMessage($message);

或使用准备好的实体

$entity = new \Alserom\Viber\Entity\Message\Text();
$entity->setText('Hello World!');

$message = new \Alserom\Viber\Message();
$message->setTo(new \Alserom\Viber\Entity\User('USER-IDENTIFIER-HERE'));
$message->setEntity($entity);

$response = $api->sendMessage($message);

注册事件(Viber回调)处理器

$handler = function (\Alserom\Viber\Event\EventInterface $event, \Alserom\Viber\Api $api) {
    // Your logic here
};

$bot->on('message', $handler);

// Or use helper methods
$bot->onMessage($handler);

处理Viber回调

Viber期望您返回HTTP状态码200的响应以确保回调已成功投递。一旦接收到conversation_started回调,您可以通过返回包含准备好的消息的响应来向用户发送欢迎消息。
此包已处理此问题。方法\Alserom\Viber\Bot :: handle将返回生成的响应,只需简单地发出即可。

您可以使用nyholm/psr7-server包或任何替代方案来从PHP超全局中创建服务器请求。
您可以使用zendframework/zend-httphandlerrunner包或任何替代方案来发出PSR-7响应。

composer require nyholm/psr7-server zendframework/zend-httphandlerrunner
$serverRequestCreator = new \Nyholm\Psr7Server\ServerRequestCreator(
    $psr17Factory, // \Psr\Http\Message\ServerRequestFactoryInterface
    $psr17Factory, // \Psr\Http\Message\UriFactoryInterface
    $psr17Factory, // \Psr\Http\Message\UploadedFileFactoryInterface
    $psr17Factory  // \Psr\Http\Message\StreamFactoryInterface
);

$serverRequest = $serverRequestCreator->fromGlobals();

// $response is a PSR-7 Response object
$response = $bot->handle($serverRequest);

$emitter = new \Zend\HttpHandlerRunner\Emitter\SapiEmitter();
$emitter->emit($response);

待办事项列表

  • 编写文档
  • 创建更多测试
  • 查看代码中的@TODO标签

贡献

欢迎提交拉取请求。

在您提交PR之前,请确保您的代码适合合并。只需运行几个脚本

composer test
composer check-code
composer check-style

许可协议

本项目采用MIT许可证 - 请参阅LICENSE文件以获取详细信息。

查看更多