Skip to content

Bogdaan/viber-bot-php

Folders and files

NameName
Last commit message
Last commit date

Latest commit

4e0900d · Oct 24, 2017

History

32 Commits
Oct 11, 2017
Oct 11, 2017
Oct 24, 2017
Oct 16, 2017
Feb 2, 2017
Feb 5, 2017
Oct 16, 2017
Oct 24, 2017
Feb 2, 2017
Oct 11, 2017
Feb 4, 2017
Feb 3, 2017

Repository files navigation

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                   #
│   │   ├── 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.