alserom/viber-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

alserom/viber-php

Composer 安装命令:

composer require alserom/viber-php

包简介

Unofficial library to work with Viber REST API and for develop a bot for the Viber platform.

README 文档

README

Build Status Minimum PHP Version Coding Style Software License

Use this library to develop a bot for the Viber platform or simple work with Viber REST API.

Note: For work with Viber API, you must have an authentication token. Go to partners.viber.com, create bot account and get token.

Installation

composer require alserom/viber-php

For properly work with this package you do also need to install a PSR-17 request/response factory and PSR-18 HTTP Client.
You can find packages of these implementations here:

Example:

composer require nyholm/psr7 kriswallsmith/buzz

Usage

This page will just show you the basics. For advanced usage, please read the full documentation.

If you want to quickly try this library on practice, you can use the code from repository viber-bot-examples.

Create objects

// 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);

Setting a 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);

Sending message

Build a message from scratch:

$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);

Or use prepared entity:

$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);

Registering events (Viber callbacks) handlers

$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);

Handling Viber callbacks

Viber expecting you to return a response with HTTP status code 200 for be sure that callback was delivered. Also, once a conversation_started callback is received you can send a welcome message to the user by returning a response with a prepared message.
This package took care of this. The method \Alserom\Viber\Bot :: handle will return the generated response, which remains simply to emit.

You can use nyholm/psr7-server package or any alternative to create server requests from PHP superglobals.
You can use zendframework/zend-httphandlerrunner package or any alternative to emitting PSR-7 responses.

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 list

  • Write documentation
  • Create to more tests
  • See @TODO tags in the code

Contributing

Pull requests are welcome.

Before you make a PR, be sure that your code is suited to be merged. Just run several scripts:

composer test
composer check-code
composer check-style

License

This project is licensed under the MIT License - see the LICENSE file for details.

See more

alserom/viber-php 适用场景与选型建议

alserom/viber-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84 次下载、GitHub Stars 达 1, 最近一次更新时间为 2019 年 04 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「bot」 「viber」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 alserom/viber-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 alserom/viber-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 alserom/viber-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 84
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-04-13