From 34e32734bcfb6dc814721423ed73b87e0729dbb0 Mon Sep 17 00:00:00 2001 From: Saurabh Newatiya <107537111+saurabhnewatiya-plivo@users.noreply.github.com> Date: Tue, 7 May 2024 15:30:34 +0530 Subject: [PATCH] Adding support for interactive messages (#341) * Adding support for interactive messages --- CHANGELOG.md | 4 + .../Resources/Message/MessageInterface.php | 18 +++ src/Plivo/Util/interactive.php | 118 ++++++++++++++++++ src/Plivo/Version.php | 2 +- 4 files changed, 141 insertions(+), 1 deletion(-) create mode 100644 src/Plivo/Util/interactive.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 677fe793..c0723b25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## [4.63.0](https://github.com/plivo/plivo-php/tree/v4.63.0) (2024-05-07) +**Feature - Adding support for interactive whatsapp messages** +- Added new param `interactive` to [send message API](https://www.plivo.com/docs/sms/api/message#send-a-message) to support interactive `whatsapp` messages + ## [4.62.0](https://github.com/plivo/plivo-php/tree/v4.62.0) (2024-05-02) **Feature - Pin Based Authentication, SubAccount and GeoMatch for Number Masking** - Pin Based Authentication, SubAccount and GeoMatch added in Create Session API for Number Masking diff --git a/src/Plivo/Resources/Message/MessageInterface.php b/src/Plivo/Resources/Message/MessageInterface.php index 9fb3d341..d8b58b6d 100644 --- a/src/Plivo/Resources/Message/MessageInterface.php +++ b/src/Plivo/Resources/Message/MessageInterface.php @@ -9,6 +9,7 @@ use Plivo\Exceptions\PlivoResponseException; use Plivo\Util\ArrayOperations; use Plivo\Util\Template; +use Plivo\Util\Interactive; use Plivo\MessageClient; use Plivo\Resources\ResourceInterface; @@ -155,6 +156,7 @@ public function create($src=null, $dst=null, $text=null,array $optionalArgs = [] $powerpackUUID = isset($optionalArgs['powerpackUUID']) ? $optionalArgs['powerpackUUID'] : null; } $template = isset($optionalArgs['template']) ? $optionalArgs['template'] : null; + $interactive = isset($optionalArgs['interactive']) ? $optionalArgs['interactive'] : null; if (is_array($dst)){ $mandatoryArgs = [ 'dst' => implode('<', $dst), @@ -195,6 +197,11 @@ public function create($src=null, $dst=null, $text=null,array $optionalArgs = [] 'Template paramater is only applicable when message_type is whatsapp' ); } + if (isset($optionalArgs['type']) && $optionalArgs['type'] != 'whatsapp' && !is_null($interactive)){ + throw new PlivoValidationException( + 'Interactive paramater is only applicable when message_type is whatsapp' + ); + } if(!is_null($template)){ $err = Template::validateTemplate($template); @@ -207,6 +214,17 @@ public function create($src=null, $dst=null, $text=null,array $optionalArgs = [] $optionalArgs['template'] = json_decode($template,True); } + if(!is_null($interactive)){ + $err = Interactive::validateInteractive($interactive); + if (!is_null($err)) + { + throw new PlivoValidationException( + $err + ); + } + $optionalArgs['interactive'] = json_decode($interactive,True); + } + $response = $this->client->update( $this->uri, array_merge($mandatoryArgs, $optionalArgs, ['src' => $src, 'powerpack_uuid' => $powerpackUUID, 'text' => $text]) diff --git a/src/Plivo/Util/interactive.php b/src/Plivo/Util/interactive.php new file mode 100644 index 00000000..04f9197e --- /dev/null +++ b/src/Plivo/Util/interactive.php @@ -0,0 +1,118 @@ +type = $data['type'] ?? null; + $this->text = $data['text'] ?? null; + $this->media = $data['media'] ?? null; + } +} + +// Body class +class Body { + public $text; + + public function __construct(array $data) + { + $this->text = $data['text'] ?? null; + } +} + +// Footer class +class Footer { + public $text; + + public function __construct(array $data) + { + $this->text = $data['text'] ?? null; + } +} + +// Buttons class +class Buttons { + public $id; + public $title; + public $cta_url; + + public function __construct(array $data) + { + $this->id = $data['id'] ?? null; + $this->title = $data['title'] ?? null; + $this->cta_url = $data['cta_url'] ?? null; + } +} + +// Row class +class Row { + public $id; + public $title; + public $description; + + public function __construct(array $data) + { + $this->id = $data['id'] ?? null; + $this->title = $data['title'] ?? null; + $this->description = $data['description'] ?? null; + } +} + +// Section class +class Section { + public $title; + public $rows; + + public function __construct(array $data) + { + $this->title = $data['title'] ?? null; + $this->rows = array_map(function($row) { return new Row($row); }, $data['rows'] ?? []); + } +} + +// Action class +class Action { + public $buttons; + public $sections; + + public function __construct(array $data) + { + $this->buttons = array_map(function($btn) { return new Buttons($btn); }, $data['buttons'] ?? []); + $this->sections = array_map(function($section) { return new Section($section); }, $data['sections'] ?? []); + } +} + +// Interactive class +class Interactive { + public $type; + public $header; + public $body; + public $footer; + public $action; + + public function __construct(array $data) + { + $this->type = $data['type'] ?? null; + $this->header = new Header($data['header'] ?? []); + $this->body = new Body($data['body'] ?? []); + $this->footer = new Footer($data['footer'] ?? []); + $this->action = new Action($data['action'] ?? []); + } + + public function validateInteractive(string $interactive) + { + // Additional validation logic can be implemented here if needed + if(is_null(json_decode($interactive, true))) { + return "Invalid JSON data for interactive messages!"; + } + //Instantiate and validate the Interactive class + $interactive = new Interactive(json_decode($interactive, true)); + return null; + } +} diff --git a/src/Plivo/Version.php b/src/Plivo/Version.php index 21ae424f..18097c3e 100644 --- a/src/Plivo/Version.php +++ b/src/Plivo/Version.php @@ -20,7 +20,7 @@ class Version /** * @const int PHP helper library minor version number */ - const MINOR = 62; + const MINOR = 63; /** * @const int PHP helper library patch number