Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature new corusel template #9

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,14 @@ $botman->fallback(static function (BotMan $bot) {
});

$botman->listen();
```
###Send Rich Media message / Carousel content message
```php
$carousel = Carousel::create(6,3)->addElement(
CarouselElement::create('<font color="#FFFFFF">Text1</font>',6,1),
CarouselElement::create('<font color="#FFFFFF">Text2</font>',6,1),
CarouselElement::create('<font color="#FFFFFF">Text3</font>',6,1)
);

$this->say("Default text",$carousel->toArray());
```
7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "polyskalov/botman-viber-driver",
"name": "yaroslav-kozhemiaka/botman-viber-driver",
"license": "MIT",
"description": "Viber driver for BotMan",
"keywords": [
Expand All @@ -8,11 +8,10 @@
"Viber",
"Messenger"
],
"homepage": "https://github.com/polyskalov/botman-viber-driver",
"authors": [
{
"name": "Oleksii Yaryi",
"email": "oleksii.yaryi@gmail.com"
"name": "Yaroslav Kozhemiaka",
"email": "jaroslav.kozhemiaka@gmail.com"
}
],
"require": {
Expand Down
87 changes: 87 additions & 0 deletions src/Extensions/Carousel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

namespace TheArdent\Drivers\Viber\Extensions;

use Illuminate\Contracts\Support\Arrayable;

class Carousel implements Arrayable
{
const GROUP_COLUMNS = 6;
const GROUP_ROWS = 3;
const MIN_API_VERSION = 2;
const BG_COLOR = '#FFFFFF';

protected $elements = [];
protected $groupColumns;
protected $groupRows;
protected $bgColor;

public static function create(int $columns = self::GROUP_COLUMNS, int $rows = self::GROUP_ROWS, string $color = self::BG_COLOR)
{
return new self($columns, $rows, $color);
}

public function __construct($columns, $rows, $color)
{
$this->groupColumns = $columns;
$this->groupRows = $rows;
$this->bgColor = $color;
}

/**
* @param CarouselElement ...$elements
* @return $this
* @throws \Exception
*/
public function addElement(CarouselElement ...$elements)
{
$countRows = 0;

foreach ($elements as $element) {
$countRows += $element->rows;

if ($element->columns > $this->groupColumns) {
throw new \Exception("Columns in element greater, than group columns.");
}

}

if ($countRows > $this->groupRows) {
throw new \Exception("Count rows in elements greater, than group rows.");
}

$this->elements[] = $elements;

return $this;
}

protected function elementsToArray()
{
return collect($this->elements)->map(function ($coreElements) {

return collect($coreElements)->map(function (CarouselElement $element) {
return $element->toArray();
})
->toArray();

})
->flatten(1)
->toArray();
}

public function toArray()
{
return [
"min_api_version" => self::MIN_API_VERSION,
"type" => "rich_media",
"driver_type" => 'carousel',
"rich_media" => [
"Type" => "rich_media",
"ButtonsGroupColumns" => $this->groupColumns,
"ButtonsGroupRows" => $this->groupRows,
"Buttons" => $this->elementsToArray(),
"BgColor" => $this->bgColor
]
];
}
}
51 changes: 51 additions & 0 deletions src/Extensions/CarouselElement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

namespace TheArdent\Drivers\Viber\Extensions;

use Illuminate\Contracts\Support\Arrayable;

class CarouselElement implements Arrayable
{
const COLUMNS = 6;
const ROWS = 3;
const ACTION_TYPE = 'reply';

public $columns;
public $rows;
protected $text;
protected $type;

public static function create(string $text, int $columns = self::COLUMNS, int $rows = self::ROWS)
{
return new self($text, $columns, $rows);
}

public function __construct($text, $columns, $rows)
{
$this->text = $text;
$this->columns = $columns;
$this->rows = $rows;
$this->type = self::ACTION_TYPE;
}

public function type(string $type)
{
$this->type = $type;
}


public function toArray()
{
return [
'Columns' => $this->columns,
'Rows' => $this->rows,
'Text' => $this->text,
'ActionType' => $this->type,
'TextSize' => 'small',
'ActionBody' => '#',
'TextVAlign' => 'middle',
'TextHAlign' => 'middle',
'BgColor' => '#675AAA'
];
}
}
29 changes: 19 additions & 10 deletions src/ViberDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TheArdent\Drivers\Viber;

use GuzzleHttp\Client;
use JsonSerializable;
use BotMan\BotMan\Interfaces\DriverEventInterface;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -199,8 +200,8 @@ protected function convertQuestion(Question $question): array
$keyboard = new KeyboardTemplate($question->getText());
foreach ($actions as $action) {
$text = $action['text'];
$actionType = $action['additional']['url'] ? 'open-url' : 'reply';
$actionBody = $action['additional']['url'] ?? $action['value'] ?? $action['text'];
$actionType = optional($action['additional'])['url'] ? 'open-url' : 'reply';
$actionBody = optional($action['additional'])['url'] ?? $action['value'] ?? $action['text'];
$silent = isset($action['additional']['url']);
$keyboard->addButton($text, $actionType, $actionBody, 'regular', null, 6, $silent);
}
Expand Down Expand Up @@ -244,7 +245,7 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
if (!is_null($attachment)) {
$attachmentType = strtolower(basename(str_replace('\\', '/', get_class($attachment))));
if ($attachmentType === 'image' && $attachment instanceof Image) {
$template = new PictureTemplate($attachment->getUrl(), $attachment->getTitle());
$template = new PictureTemplate($attachment->getUrl(), $message->getText());
} elseif ($attachmentType === 'video' && $attachment instanceof Video) {
$template = new VideoTemplate($attachment->getUrl());
} elseif (
Expand All @@ -264,6 +265,8 @@ public function buildServicePayload($message, $matchingMessage, $additionalParam
if (isset($template)) {
$parameters = array_merge($template->jsonSerialize(), $parameters);
}
} elseif (array_key_exists('driver_type',$additionalParameters) && $additionalParameters['driver_type'] == 'carousel') {
unset($parameters['text']);
} else {
$parameters['text'] = $message->getText();
$parameters['type'] = 'text';
Expand Down Expand Up @@ -317,12 +320,18 @@ public function getUser(IncomingMessage $matchingMessage): User

$user = null;

$response = $this->sendRequest(
self::API_ENDPOINT . 'get_user_details',
['id' => $personId],
$matchingMessage
);
$responseData = json_decode($response->getContent(), true);
$client = new Client();

$response = $client->request('POST',self::API_ENDPOINT . 'get_user_details', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Viber-Auth-Token' => $this->config->get('token'),
],
'json' => ['id' => $personId]
]);

$responseData = json_decode($response->getBody()->getContents(), true);

if (($responseData['status'] ?? null) === 0 && ($responseData['user'] ?? null)) {
$user = $responseData['user'];
Expand All @@ -338,7 +347,7 @@ public function getUser(IncomingMessage $matchingMessage): User
$nameArray[0] ?? '',
$nameArray[1] ?? '',
$name,
$user
$responseData
);
}

Expand Down