PHP code example of alserom / viber-php

1. Go to this page and download the library: Download alserom/viber-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alserom / viber-php example snippets


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

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

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

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