-
Notifications
You must be signed in to change notification settings - Fork 127
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix token typo, work with docs and examples
- Loading branch information
Showing
15 changed files
with
330 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ composer.phar | |
composer.lock | ||
.idea | ||
.DS_STORE | ||
/examples/config.php |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^4.8" | ||
"phpunit/phpunit": "^4.8", | ||
"monolog/monolog": "^1.22" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
 | ||
|
||
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  at the top right of your screen | ||
|
||
2. Once you're on the Public Accounts home page, tap on the create button  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 | ||
|
||
 | ||
|
||
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) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
*/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters