sevenspan/laravel-whatsapp
Composer 安装命令:
composer require sevenspan/laravel-whatsapp
包简介
It uses whatsapp cloud API to send whatsapp messages.
README 文档
README
It uses WhatsApp cloud API to send template whatsapp messages.
Installation
You can install the package via composer:
composer require sevenspan/laravel-whatsapp
You can publish the config file with:
php artisan vendor:publish --provider="SevenSpan\WhatsApp\Providers\WhatsAppServiceProvider" --tag="config"
This is the contents of the published config file:
<?php return [ /* |-------------------------------------------------------------------------- | Whatsapp API URI |-------------------------------------------------------------------------- | | The Whatsapp Message API URI. | */ 'api_uri' => env('WHATSAPP_API_URI', 'https://graph.facebook.com/v14.0/'), /* |-------------------------------------------------------------------------- | WHATSAPP BUSINESS ACCOUNT ID |-------------------------------------------------------------------------- | | The Whatsapp business account id (waba_id). | */ 'whatsapp_business_account_id' => env('WHATSAPP_BUSINESS_ACCOUNT_ID', ''), /* |-------------------------------------------------------------------------- | ACCESS TOKEN |-------------------------------------------------------------------------- | | The Whatsapp business account user access token. | */ 'access_token' => env('ACCESS_TOKEN', ''), /* |-------------------------------------------------------------------------- | SEPARATOR |-------------------------------------------------------------------------- | | The Whatsapp separator is used for the separat your string message. | For example: $message = 'Hello,123'; then set your .env file variable (SEPARATO=,). */ 'separator' => env('SEPARATOR', '~'), /* |-------------------------------------------------------------------------- | FROM PHONE NUMBER ID |-------------------------------------------------------------------------- | | The Whatsapp register phome number Id. | You have to get from phone number id using getPhoneNumbers() function. */ 'from_phone_number_id' => env('FROM_PHONE_NUMBER_ID', ''), ];
Usage
For create access token follow this link: Click here
use WhatsApp; $whatsAppBussnessAccountId = "111111111111111"; // Your bussness account id (waba_id)(Required) $accessToken = ""; // Access Token of the user (Required) $response = WhatsApp::getPhoneNumbers(string $WhatsAppBusinessAccountId = '', string $accessToken = '') #You will get output like this: array:2 [ "data" => array:1 [ 0 => array:5 [ "verified_name" => "Test" "code_verification_status" => "NOT_VERIFIED" "display_phone_number" => "+1 123-123-1234" "quality_rating" => "GREEN" "id" => "123456789012345" ] ] "paging" => array:1 [ "cursors" => array:2 [ "before" => "QVFIUlpVY3FxaW5ZAncElreTlWM0VqU0xIcUZADYTNac2ppUXpFeDEzbmNPMXVfYlZABSVJVaTZAmM0FDWWVYeEFlUW9BTHlSZAFBYbGUyZAVhXZAVBReDF1OENn" "after" => "QVFIUlpVY3FxaW5PZAncElreTlWM0VqU0xIcUZADYTNac2ppUXpFeDEzbmNPMXVfYlZABSVJVaTZAmM0FDWWVYeEFlUW9BTHlSZAFBYbGUyZAVhXZAVBReDF1OENn" ] ] ]
Note : If you want to use to get multiple Whatsapp business account numbers for better usage then please define your number in getPhoneNumbers function otherwise set your number in the environment file
$to = "9111111111"; // Phone number with country code where we want to send message(Required). $templateName = "hello_world"; // Template name of your template (Required). $languageCode = "en_us"; // Template language code of your template (Required). $fromPhoneNumberId = '123456789012345' // You have to get from phone number id using getPhoneNumbers() function. #To send the message; use the separator character in the message with respective argument. Below is an example of how to send the text message. $message = 'test~message'; //if message is dyamic you have to passing a parameter order vice. The customized message should be passed to the components variable along with the object. You can refer this link for the create components [Click here](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates) $components = [ [ 'type' => 'header', 'parameters' => [ [ 'type' => 'image', 'image' => [ 'link' => 'http(s)://URL', ] ] ] ], [ 'type' => 'body', 'parameters' =>[ [ 'type' => 'text', 'text' => 'TEXT_STRING', ], [ 'type' => 'currency', 'currency' => [ 'fallback_value' => 'VALUE', 'code' => 'USD', 'amount_1000' => 100, ] ], [ 'type' => 'date_time', 'date_time' => [ 'fallback_value' => 'MONTH DAY, YEAR', ] ], ], ], [ 'type' => 'button', 'sub_type' => 'quick_reply', 'index' => '0', 'parameters' => [ [ 'type' => 'payload', 'payload' => 'PAYLOAD', ], ], ] ]
NOTE : The package is expecting a single paramter (either $message or $components) at a time.
$response = WhatsApp::sendTemplateMessage(string $to, string $templateName, string $languageCode, string $accessToken = '', string $fromPhoneNumberId = '', array $components = [], string $messages = ''); # A successful response includes an object with an identifier prefixed with wamid. array:3 [ "messaging_product" => "whatsapp" "contacts" => array:1 [ 0 => array:2 [ "input" => "911234567890" "wa_id" => "911234567890" ] ] "messages" => array:1 [ 0 => array:1 [ "id" => "wamid.HBgMOTE3ODc4OTE4MDXSDQIAERgSOUIwQzlGREQ5NUZBQjQ1OTkzAA==" ] ] ]
Example
For Example your template is like this:
The OTP to login into app is: {{1}}
Regards{{2}}
Thank you!
You have to pass the $message parameter like this $message = "1234~Nikunj"
Output of above template
The OTP to login into app is: 1234
Regards Nikunj
Thank you!
Note : While sending a WhatsApp message with a template you should send the data in a sequence of the template dynamic value.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Credits
License
The MIT License (MIT). Please see License File for more information.
sevenspan/laravel-whatsapp 适用场景与选型建议
sevenspan/laravel-whatsapp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.96k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2022 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「whatsapp」 「laravel-whatsapp」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sevenspan/laravel-whatsapp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sevenspan/laravel-whatsapp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sevenspan/laravel-whatsapp 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WhatsApp Latest CLoud API Wrapper for PHP
Enhance your laravel apps with WhatsApp's Cloud API
Alfabank REST API integration
WhatsApp Cloud API Notification Channel for Laravel
The PHP WhatsApp library
Cliente Wis SDK para PHP
统计信息
- 总下载量: 8.96k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 20
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-07-11