• Stars
    star
    232
  • Rank 172,847 (Top 4 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Php bot interface to work with Viber API

PHP sdk for Viber api

Build Status

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.