From 002d73c167a53ad8d31be568b3742e5a7118737e Mon Sep 17 00:00:00 2001 From: Jaroslaw Korzemiaka Date: Wed, 21 Oct 2020 15:20:17 +0200 Subject: [PATCH 1/5] fix: Fix getUser() method --- src/ViberDriver.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/src/ViberDriver.php b/src/ViberDriver.php index 2b9b577..bc02293 100644 --- a/src/ViberDriver.php +++ b/src/ViberDriver.php @@ -2,6 +2,7 @@ namespace TheArdent\Drivers\Viber; +use GuzzleHttp\Client; use JsonSerializable; use BotMan\BotMan\Interfaces\DriverEventInterface; use Illuminate\Support\Collection; @@ -317,12 +318,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']; @@ -338,7 +345,7 @@ public function getUser(IncomingMessage $matchingMessage): User $nameArray[0] ?? '', $nameArray[1] ?? '', $name, - $user + $responseData ); } From f398e45b5b68b011c69c0a4894e20b9cab5df756 Mon Sep 17 00:00:00 2001 From: "jaroslaw.kozhemiaka" Date: Thu, 22 Oct 2020 11:06:23 +0200 Subject: [PATCH 2/5] fix: Error with undefined url key. --- src/ViberDriver.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ViberDriver.php b/src/ViberDriver.php index bc02293..0e47782 100644 --- a/src/ViberDriver.php +++ b/src/ViberDriver.php @@ -200,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); } From fbe94081e62241d45f4d0c74c1ee1b7ecc0dfbc9 Mon Sep 17 00:00:00 2001 From: "jaroslaw.kozhemiaka" Date: Fri, 23 Oct 2020 15:26:12 +0200 Subject: [PATCH 3/5] feature: New extension rich Media message / carousel content message template --- src/Extensions/Carousel.php | 87 ++++++++++++++++++++++++++++++ src/Extensions/CarouselElement.php | 51 ++++++++++++++++++ src/ViberDriver.php | 4 +- 3 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/Extensions/Carousel.php create mode 100644 src/Extensions/CarouselElement.php diff --git a/src/Extensions/Carousel.php b/src/Extensions/Carousel.php new file mode 100644 index 0000000..aed3355 --- /dev/null +++ b/src/Extensions/Carousel.php @@ -0,0 +1,87 @@ +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 + ] + ]; + } +} \ No newline at end of file diff --git a/src/Extensions/CarouselElement.php b/src/Extensions/CarouselElement.php new file mode 100644 index 0000000..b4c586b --- /dev/null +++ b/src/Extensions/CarouselElement.php @@ -0,0 +1,51 @@ +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' + ]; + } +} \ No newline at end of file diff --git a/src/ViberDriver.php b/src/ViberDriver.php index 0e47782..455ae4d 100644 --- a/src/ViberDriver.php +++ b/src/ViberDriver.php @@ -245,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 ( @@ -265,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'; From 74e4a3525a81fa572d1b6098f8d459c1b3ce6225 Mon Sep 17 00:00:00 2001 From: "jaroslaw.kozhemiaka" Date: Fri, 23 Oct 2020 15:39:18 +0200 Subject: [PATCH 4/5] update composer.json and README.md --- README.md | 10 ++++++++++ composer.json | 7 +++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8be053e..852a149 100644 --- a/README.md +++ b/README.md @@ -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('Text1',6,1), + CarouselElement::create('Text2',6,1), + CarouselElement::create('Text3',6,1) + ); + +$this->say("Default text",$carousel->toArray()); ``` \ No newline at end of file diff --git a/composer.json b/composer.json index a1221e1..35f3629 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "polyskalov/botman-viber-driver", + "name": "YaroslavKozhemiaka/botman-viber-driver", "license": "MIT", "description": "Viber driver for BotMan", "keywords": [ @@ -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": { From 24d0d77ae5261ae15ae98d86361add8443a80f40 Mon Sep 17 00:00:00 2001 From: "jaroslaw.kozhemiaka" Date: Fri, 23 Oct 2020 15:48:53 +0200 Subject: [PATCH 5/5] update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 35f3629..a6f0b29 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "YaroslavKozhemiaka/botman-viber-driver", + "name": "yaroslav-kozhemiaka/botman-viber-driver", "license": "MIT", "description": "Viber driver for BotMan", "keywords": [