Skip to content

Commit

Permalink
fix token typo, work with docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdaan committed Feb 4, 2017
1 parent 4406a68 commit 7505746
Show file tree
Hide file tree
Showing 15 changed files with 330 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ composer.phar
composer.lock
.idea
.DS_STORE
/examples/config.php
32 changes: 23 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ $bot
$apiClient->sendMessage(
(new \Viber\Api\Message\Text())
->setReceiver($event->getSender()->getId())
->setSender(botSender)
->setSender($botSender)
->setText("Hi!");
);
})
Expand All @@ -53,7 +53,7 @@ $bot
// if user send picture
return (new \Viber\Api\Message\Text())
->setReceiver($event->getSender()->getId())
->setSender(botSender)
->setSender($botSender)
->setText("Cool picture");
})
->on(function ($event) {
Expand All @@ -69,12 +69,26 @@ $bot
->run();
```

## Read more

- [Create you first Viber bot](docs/first-steps.md)
- [Cookbook](docs/cookbook.md)
- [REST api documentation](https://developers.viber.com/api/rest-bot-api/index.html)
- [SDK for node](https://github.com/Viber/viber-bot-node)
- [SDK for python](https://github.com/Viber/viber-bot-python)


## Features

[x] all api entities
[x] validate request and response signs
[x] provide webhook interface
[x] provide event interface
[ ] wrap all api response to entities
[ ] validate api entities before submit?
[ ] implement log levels with monolog
- [x] all api entities
- [x] validate request and response signs
- [x] provide webhook interface
- [x] 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.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
}
},
"require-dev": {
"phpunit/phpunit": "^4.8"
"phpunit/phpunit": "^4.8",
"monolog/monolog": "^1.22"
}
}
22 changes: 22 additions & 0 deletions docs/cookbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Cookbook

## How to react on picture-message, url-message etc. ?

You need to create own event checker. For example:
```
<?php
// ...
// $bot - \Viber\Bot instance
$bot->on(function ($event) {
return isThisIsCatPicture(event);
}, function ($event) {
// process cat pictures here
});
```


## How to process user conversation?
TODO

## How to track message delivery?
TODO
108 changes: 108 additions & 0 deletions docs/first-steps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
# How to create you first Viber bot


## Early access to public account

If you already can create special account (you have early access) - skip this step. In order to get early access you need:

1. Visit [https://www.viber.com/en/public-accounts](https://www.viber.com/en/public-accounts)

2. Click **Apply for a Public Account** button

3. Fill form and send it

Soon you will receive a message like this:
![Early access to PA](i-get-access.jpg)

After this - restart Viber app, and go to next step.


## Create public account (page)

Now you can create **Viber Public Account** (PA) from you Viber app:

1. Open Public Accounts through the Public Accounts icon ![button view](i-public_account_button.png) at the top right of your screen

2. Once you're on the Public Accounts home page, tap on the create button ![button view](i-create_button.png) at the bottom of the screen

3. Tap on Join now to start creating your Public Account

4. Fill you application info (name, category, background photo etc.)

5. On finish step you can copy api-token, or you can get token on "Edit details" page

![authToken](i-authToken.jpg)

That's all.

## Install this package with composer

You can download it from github or install with composer (recommended):
```
composer require bogdaan/viber-bot-php
```

## Setup webhook

First you need trusted CA certificate webhook url (you can get [letsencryptp](https://letsencrypt.org) cert for dev server).

Let's create setup.php and subscrive for viber events:
```
<?php
require_once("vendor/autoload.php");
use Viber\Client;
$apiKey = '<PLACE-YOU-API-KEY-HERE>'; // from PA "Edit Details" page
$webhookUrl = '<PLACE-YOU-HTTPS-URL>'; // for exmaple https://my.com/bot.php
try {
$client = new Client([ 'token' => $apiKey ]);
$result = $client->setWebhook($webhookUrl);
print_r($result);
} catch (Exception $e) {
echo "Unexpected error: ". $e->getError() ."\n";
}
```

## Create bot

We already subscribed for events. Now we can can accept messages, pictures and other events. Let's create simple bot.php:

```
<?php
require_once("vendor/autoload.php");
use Viber\Bot;
use Viber\Api\Message\Text as TextMessage;
$apiKey = '<PLACE-YOU-API-KEY-HERE>';
$botSender = new Sender([
'name' => 'Reply bot',
'avatar' => 'http://my.avatar/path.jpg',
]);
try {
$bot = new Bot([ 'token' => $apiKey ]);
$bot
->onText('|.*|s', function ($event) use ($bot) {
// .* - match any symbols (see PCRE)
$bot->getClient()->sendMessage(
(new TextMessage())
->setSender($botSender)
->setReceiver($event->getSender()->getId())
->setText("HI!")
);
})
->run();
} catch (Exception $e) {
// log errors
}
```

## Resources

- [Official documentation](https://developers.viber.com/public-accounts/index.html)
Binary file added docs/i-authToken.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/i-create_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/i-get-access.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/i-public_account_button.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions examples/bot-plus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

require_once("../vendor/autoload.php");

use Viber\Bot;
use Viber\Api\Sender;
use Viber\Api\Message\Text as TextMessage;

$config = require('./config.php');

$bot = new Bot([ 'token' => $config['apiKey'] ]);
$apiClient = $bot->getClient();

$botSender = new Sender([
'name' => 'Hello bot',
'avatar' => 'https://developers.viber.com/img/devlogo.png',
]);

$bot
->onText("|hello .*|s", function ($event) use ($apiClient) {
// reply to sender
$apiClient->sendMessage(
(new \Viber\Api\Message\Text())
->setReceiver($event->getSender()->getId())
->setSender($botSender)
->setText("Hi!")
);
})
->onSubscribe(function ($event) use ($apiClient) { // !!!! WRONG
// reply with "welcome" message
return (new \Viber\Api\Message\Text())
->setReceiver($event->getSender()->getId())
->setSender(botSender)
->setText("Can i help you?");
})
->on(function ($event) {
return (
$event->getEvent() == \Viber\Api\Event\Type::TEXT
&& $event->getMessage()->getType() == \Viber\Api\Message\Type::PICTURE
);
}, function ($event) use ($apiClient) {
// if user send picture
return (new \Viber\Api\Message\Text())
->setReceiver($event->getSender()->getId())
->setSender($botSender)
->setText("Cool picture");
})
->on(function ($event) {
return ($event->getEvent() == \Viber\Api\Event\Type::UNSUBSCRIBED);
}, function ($event) use ($apiClient) {
// process all UNSUBSCRIBED events
})
->on(function ($event) {
return true; // check if we need process this event?
}, function ($event) use ($apiClient) {
// <--- ALL OTHER EVENTS PROCESS HERE
})
->run();
74 changes: 74 additions & 0 deletions examples/bot.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

/**
* Before you run this example:
* 1. install monolog/monolog: composer require monolog/monolog
* 2. copy config.php.dist to config.php: cp config.php.dist config.php
*
* @author Novikov Bogdan <[email protected]>
*/

require_once("../vendor/autoload.php");

use Viber\Bot;
use Viber\Api\Sender;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$config = require('./config.php');
$apiKey = $config['apiKey'];

$botSender = new Sender([
'name' => 'Reply bot',
'avatar' => 'https://developers.viber.com/img/devlogo.png',
]);

$log = new Logger('bot');
$log->pushHandler(new StreamHandler('/tmp/bot.log'));

$bot = null;

try {
$bot = new Bot(['token' => $apiKey]);
$bot
->onConversation(function ($event) use ($bot, $botSender, $log) {
$log->info('onConversation '. var_export($event, true));
// 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, $log) {
$log->info('onText whois '. var_export($event, true));
// match by template, for example "whois Bogdan"
$bot->getClient()->sendMessage(
(new \Viber\Api\Message\Text())
->setSender($botSender)
->setReceiver($event->getSender()->getId())
->setText("I do not know )")
);
})
->onText('|.*|s', function ($event) use ($bot, $botSender, $log) {
$log->info('onText '. var_export($event, true));
// .* - match any symbols
$bot->getClient()->sendMessage(
(new \Viber\Api\Message\Text())
->setSender($botSender)
->setReceiver($event->getSender()->getId())
->setText("HI!")
);
})
->on(function ($event) {
return true;
}, function ($event) use ($log) {
$log->info('Other event: '. var_export($event, true));
})
->run();
} catch (Exception $e) {
$log->warning('Exception: ', $e->getMessage());
if ($bot) {
$log->warning('Actual sign: '. $bot->getSignHeaderValue());
$log->warning('Actual body: '. $bot->getInputBody());
}
}
9 changes: 9 additions & 0 deletions examples/config.php.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

/**
* Copy to "config.php" and set you values here:
*/
return [
'apiKey' => '<PLACE-YOU-API-KEY-HERE>',
'webhookUrl' => '<PLACE-YOU-HTTPS-URL>',
];
25 changes: 25 additions & 0 deletions examples/setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/**
* Before you run this example:
* 1. copy config.php.dist to config.php: cp config.php.dist config.php
*
* @author Novikov Bogdan <[email protected]>
*/

require_once("../vendor/autoload.php");

use Viber\Client;

$config = require('./config.php');

$apiKey = $config['apiKey']; // from PA "Edit Details" page
$webhookUrl = $config['webhookUrl']; // for exmaple https://my.com/bot.php

try {
$client = new Client([ 'token' => $apiKey ]);
$result = $client->setWebhook($webhookUrl);
echo "Success!\n"; // print_r($result);
} catch (Exception $e) {
echo "Error: ". $e->getError() ."\n";
}
7 changes: 6 additions & 1 deletion src/Api/Keyboard/Button.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
use Viber\Api\Entity;

/**
* Keyboard button
* Keyboard button.
*
* Pressing a keyboard button would trigger a different reply depending on the
* buttons “actionType” value.
*
* @see https://developers.viber.com/tools/keyboards/index.html
* @see https://developers.viber.com/tools/keyboards/index.html#replyLogic
* @see https://developers.viber.com/img/keyboard_guidelines.png
*
* @author Novikov Bogdan <[email protected]>
*/
Expand Down
4 changes: 2 additions & 2 deletions src/Bot.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ public function run($event = null)
$eventBody = $this->getInputBody();
if (!Signature::isValid(
$this->getSignHeaderValue(),
$this->getClient()->getToken(),
$eventBody
$eventBody,
$this->getClient()->getToken()
)) {
throw new \RuntimeException('Invalid signature header', 2);
}
Expand Down

0 comments on commit 7505746

Please sign in to comment.