inisiatif/whatsapp-qontak-php
最新稳定版本:1.4.0
Composer 安装命令:
composer require inisiatif/whatsapp-qontak-php
包简介
Sending WhatsApp message using Qontak
README 文档
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.phpand 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
registermethodAppServiceProvider
$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
ContakChannelin 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.
统计信息
- 总下载量: 8.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-14