inisiatif / whatsapp-qontak-php
Sending WhatsApp message using Qontak
Installs: 3 735
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 1
Forks: 6
Open Issues: 1
Requires
- php: ^8.0
- ext-json: *
- php-http/client-common: ^2.5
- psr/http-client-implementation: ^1.0
- psr/log: ^3.0
- webmozart/assert: ^1.11
Requires (Dev)
- mockery/mockery: ^1.5
- nyholm/psr7: ^1.8
- php-http/mock-client: ^1.5
- phpunit/phpunit: ^9.5
- symplify/easy-coding-standard: ^11.1
- vimeo/psalm: ^4.27
README
Sending WhatsApp message via Qontak API, using HTTPlug.
Installation
First install HTTPlug adapter or client and PSR-17 package compatible
composer require php-http/curl-client laminas/laminas-diactoros
and then install this package :
composer require inisiatif/whatsapp-qontak-php
Usage
Non framework usage
First your must be created a valid and approved WhatsApp template.
use Inisiatif\WhatsappQontakPhp\Client; use Inisiatif\WhatsappQontakPhp\Credential; use Inisiatif\WhatsappQontakPhp\Message\Body; use Inisiatif\WhatsappQontakPhp\Message\Button; use Inisiatif\WhatsappQontakPhp\Message\Header; use Inisiatif\WhatsappQontakPhp\Message\Message; use Inisiatif\WhatsappQontakPhp\Message\Receiver; use Inisiatif\WhatsappQontakPhp\Message\Language; $credentials = new Credential('username', 'password', 'clientId', 'clientSecret'); $client = new Client($credentials); // Create message receiver $receiver = new Receiver('+6281318788271', 'Nuradiyana'); // [Optional] Create language, supported 'en' and 'id', default is 'id' $language = new Language('id'); // [Optional] Create params message body $body = [ new Body('Nuradiyana'), new Body('Gorengan'), ]; // [Optional] Create header message, support "DOCUMENT", "VIDEO", "IMAGE" $header = new Header( Header::TYPE_DOCUMENT, 'https://example.com/link-to-file-url.pdf', 'file-name.pdf' ); // [Optional] Create buttons $buttons = [new Button('url', 'https://example.com')]; $message = new Message($receiver, $language, $body, $header, $buttons); $response = $client->send('templateId', 'channelId', $message); // Message Id and Receiver Name echo $response->getMessageId(); echo $response->getName(); // All raw data \var_dump($response->getData());
Using in Laravel as Notification Channel
- Add new config value in
config/services.php
and then setting each value in.env
'qontak' => [ 'username' => env('QONTAK_USERNAME', null), 'password' => env('QONTAK_PASSWORD', null), 'client_id' => env('QONTAK_CLIENT_ID', null), 'client_secret' => env('QONTAK_CLIENT_SECRET', null), ],
- Add this code in
register
methodAppServiceProvider
$this->app->singleton(\Inisiatif\WhatsappQontakPhp\ClientInterface::class, function () { return $this->app->runningUnitTests() ? \Inisiatif\WhatsappQontakPhp\ClientFactory::makeTestingClient() : \Inisiatif\WhatsappQontakPhp\ClientFactory::makeFromArray( config('service.qontak') ); });
- Then create or register
ContakChannel
in notification class
use Illuminate\Bus\Queueable; use Illuminate\Notifications\Notification; use Illuminate\Contracts\Queue\ShouldQueue; use Inisiatif\WhatsappQontakPhp\Message\Message; use Inisiatif\WhatsappQontakPhp\Message\Receiver; use Inisiatif\WhatsappQontakPhp\Illuminate\Envelope; use Inisiatif\WhatsappQontakPhp\Illuminate\QontakChannel; use Inisiatif\WhatsappQontakPhp\Illuminate\QontakNotification; class InvoicePaid extends Notification implements QontakNotification { use Queueable; public function via($notifiable): array { return [QontakChannel::class]; } public function toQontak($notifiable): Envelope { // First create message object $receiver = new Receiver('+6281318788271', 'Nuradiyana'); $message = new Message($receiver); // Then create envelope object and return it return new Envelope('templateId', 'channelId', $message); } }
Testing
๐งน Fixing codebase with Easy Coding Standard:
composer ecs
โ๏ธ Run static analysis using Psalm:
composer psalm
๐ Run the entire test suite:
composer test
License
The MIT License (MIT). Please see License File for more information.