Skip to content

Commit

Permalink
Adding support for interactive messages (#341)
Browse files Browse the repository at this point in the history
* Adding support for interactive messages
  • Loading branch information
saurabhnewatiya-plivo authored May 7, 2024
1 parent bfeba2f commit 34e3273
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/Plivo/Resources/Message/MessageInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand All @@ -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])
Expand Down
118 changes: 118 additions & 0 deletions src/Plivo/Util/interactive.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Plivo\Util;

// Header class
class Header {
public $type;
public $text;
public $media;

public function __construct(array $data)
{
$this->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;
}
}
2 changes: 1 addition & 1 deletion src/Plivo/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 34e3273

Please sign in to comment.