PHP sdk for Viber api
Library to develop a bot for the Viber platform. Create you first Viber bot step by step, see demo at viber://pa?chatURI=viber-bot-php&context=github.com
Installation
composer require bogdaan/viber-bot-php
Example
<?php
require_once("../vendor/autoload.php");
use Viber\Bot;
use Viber\Api\Sender;
$apiKey = '<PLACE-YOU-API-KEY-HERE>';
// reply name
$botSender = new Sender([
'name' => 'Whois bot',
'avatar' => 'https://developers.viber.com/img/favicon.ico',
]);
try {
$bot = new Bot(['token' => $apiKey]);
$bot
->onConversation(function ($event) use ($bot, $botSender) {
// this event fires if user open chat, you can return "welcome message"
// to user, but you can't send more messages!
return (new \Viber\Api\Message\Text())
->setSender($botSender)
->setText("Can i help you?");
})
->onText('|whois .*|si', function ($event) use ($bot, $botSender) {
// match by template, for example "whois Bogdaan"
$bot->getClient()->sendMessage(
(new \Viber\Api\Message\Text())
->setSender($botSender)
->setReceiver($event->getSender()->getId())
->setText("I do not know )")
);
})
->run();
} catch (Exception $e) {
// todo - log exceptions
}
See more in examples directory.
Library structure
.
βββ Api
βΒ Β βββ Entity.php
βΒ Β βββ Event # all remote events ("callbacks")
βΒ Β βΒ Β βββ Conversation.php # fires when user open 1v1 chat
βΒ Β βΒ Β βββ Delivered.php # fires when message delivered (for each device)
βΒ Β βΒ Β βββ Factory.php # Event factory
βΒ Β βΒ Β βββ Failed.php # fires when delivery failed (for each device)
βΒ Β βΒ Β βββ Message.php # fires when user send message
βΒ Β βΒ Β βββ Seen.php # fires when user read message (for each device)
βΒ Β βΒ Β βββ Subscribed.php # fires when user subscribe to PA
βΒ Β βΒ Β βββ Type.php # available types
βΒ Β βΒ Β βββ Unsubscribed.php # fires when user unsubscribed
βΒ Β βββ Event.php # base class for all events
βΒ Β βββ Exception #
βΒ Β βΒ Β βββ ApiException.php # remote or logic error
βΒ Β βββ Keyboard #
βΒ Β βΒ Β βββ Button.php # all types of buttons here
βΒ Β βββ Keyboard.php # button container
βΒ Β βββ Message #
βΒ Β βΒ Β βββ CarouselContent.php #
βΒ Β βΒ Β βββ Contact.php #
βΒ Β βΒ Β βββ Factory.php #
βΒ Β βΒ Β βββ File.php #
βΒ Β βΒ Β βββ Location.php #
βΒ Β βΒ Β βββ Picture.php #
βΒ Β βΒ Β βββ Sticker.php #
βΒ Β βΒ Β βββ Text.php #
βΒ Β βΒ Β βββ Type.php # available message types
βΒ Β βΒ Β βββ Url.php #
βΒ Β βΒ Β βββ Video.php #
βΒ Β βββ Message.php # base class for all messages
βΒ Β βββ Response.php # wrap api response
βΒ Β βββ Sender.php # represent bot-sender
βΒ Β βββ Signature.php # signature helper (verify or create sign)
βΒ Β βββ User #
βΒ Β βΒ Β βββ State.php # user state (online/offline etc)
βΒ Β βββ User.php # viber user
βββ Bot #
βΒ Β βββ Manager.php # manage bot closures
βββ Bot.php # bot class
βββ Client.php # api client
Read more
Features
- all api entities
- validate request and response signs
- provide webhook interface
- provide event interface
- wrap all api response to entities
- validate api entities before submit?
- implement log levels with monolog?
- post on public page
Contributing
Pull requests are welcome.