diecoding/yii2-wablas

Wablas wrapper for Yii2

Installs: 4

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 2

Type:yii2-extension

v1.0.0 2025-01-14 14:10 UTC

This package is auto-updated.

Last update: 2025-01-14 14:20:54 UTC


README

This extension is wrapper of Wablas API for Yii framework 2.0 (requires PHP 7.4+).

Latest Stable Version Total Downloads Latest Unstable Version License PHP Version Require

Table of contents

Instalation

Package is available on Packagist, you can install it using Composer.

composer require diecoding/yii2-wablas ^1.0

or add to the require section of your composer.json file.

"diecoding/yii2-wablas": "^1.0"

Dependencies

Basic Usage

Add wablas component to your configuration file

'components' => [
    'wablas' => [
        'class'    => \diecoding\yii2\wablas\Wablas::class,
        'endpoint' => 'my-wablas.com/api',                  // Change with your wablas API endpoint
        'token'    => 'my-token',                           // Change with your wablas API token
        'secret'   => 'my-secret',                          // Optional, change with your wablas API secret, you can use token with format `token.secret`
    ],
],

Create Request

$data = [
    [
        'phone'   => '6281218xxxxxx',
        'message' => 'hello there',
    ]
];

/** @var \diecoding\yii2\wablas\versions\V2 $wablas */
$wablas  = $this->wablas->build('v2');
$request = $wablas->sendMessage($data)->request;

// Print request to string
print_r($request->toString());

// Short command
$request = $this->wablas->build('v2')->sendMessage($data)->request;

Create Response

$data = [
    [
        'phone'   => '6281218xxxxxx',
        'message' => 'hello there',
    ]
];

/** @var \diecoding\yii2\wablas\versions\V2 $wablas */
$wablas = $this->wablas->build('v2');
$response = $wablas
    ->sendMessage($data)
    ->send()
    ->response;

// Print whether response is OK
print_r($response->isOk);

// Print status code
print_r($response->statusCode);

// Print response data
print_r($response->data);

// Short command
$response = $this->wablas->build('v2')->sendMessage($data)->send()->response;

Custom version

You can create your own version as follows

  1. Create custom version
class CustomVersion extends BaseObject
{
    public $wablas;

    public function sendMessage(array $data): Wablas
    {
        $this->wablas->setRequest($this->wablas->client->post(['custom/send-message'])->setData($data));
        return $this->wablas;
    }
}
  1. Register custom version
'components' => [
    'wablas' => [
        'class'    => \diecoding\yii2\wablas\Wablas::class,
        'endpoint' => 'my-wablas.com',                      // Change with your endpoint
        'token'    => 'my-token',                           // Change with your wablas token,
        'secret'   => 'my-secret',                          // Optional, change with your wablas API secret, you can use token with format `token.secret`
        'versions' => [
            'custom' => CustomVersion::class,
        ]
    ],
],
  1. Call the custom version
$wablas = $this->wablas->build('custom')->sendMessage($data)->send();

Send Message Example

Step by step usage

  1. Install component
composer require diecoding/yii2-wablas ^1.0
  1. Update your components configuration
'components' => [
    // other components here...
    'wablas' => [
        'class'    => \diecoding\yii2\wablas\Wablas::class,
        'endpoint' => 'my-wablas.com/api',
        'token'    => 'my-token',
        'secret'   => 'my-secret',                          // Optional, you can use token with format `token.secret`
    ],
    // ...
],
  1. Update controller
use Yii;
use yii\web\Controller;

class TestController extends Controller
{
    public function actionTest()
    {
        $data = [
            [
                'phone'   => '6281218xxxxxx',
                'message' => 'hello there',
            ]
        ];

        $response = Yii::$app->wablas->build('v2')->sendMessage($data)->send()->response;
        
        if ($response->isOk) {
            print_r($response); // Do action
        } else {
            print_r($response); // Do action
        }
    }
}