jackie-source / whatsapp-cloud-api
第一个使用托管在云端的WhatsApp商业平台发送和接收消息的PHP SDK
Requires
- php: ^7.4 || ^8.0 || ^8.1
- guzzlehttp/guzzle: ^6.0 || ^7.0
- myclabs/php-enum: ^1.8
- vlucas/phpdotenv: ^5.4
Requires (Dev)
- fakerphp/faker: ^1.19
- friendsofphp/php-cs-fixer: ^3.13
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^1.9
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^5.0
README
它做什么
此软件包使得开发者在PHP代码中轻松访问WhatsApp Cloud API服务。
每月前1000次会话免费(每次会话计为一个会话)。
入门指南
请按照官方指南的“入门”部分创建和配置您的Facebook WhatsApp应用程序。
最低要求 - 运行SDK需要系统具备PHP >= 7.4,并且需要编译了OpenSSL和zlib的CURL >=7.19.4的最近版本。
安装
composer require jackie-source/whatsapp-cloud-api
快速示例
发送文本消息
<?php // Require the Composer autoloader. require 'vendor/autoload.php'; use JackieSource\WhatsAppCloudApi\WhatsAppCloudApi; // Instantiate the WhatsAppCloudApi super class. $whatsapp_cloud_api = new WhatsAppCloudApi([ 'from_phone_number_id' => 'your-configured-from-phone-number-id', 'access_token' => 'your-facebook-whatsapp-application-token', ]); $whatsapp_cloud_api->sendTextMessage('34676104574', 'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es');
发送文档
您可以通过以下两种方式发送文档:将文件上传到WhatsApp Cloud服务器(您将收到一个标识符)或从互联网上发布的文档链接。
<?php use JackieSource\WhatsAppCloudApi\Message\Media\LinkID; use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID; $document_id = '341476474779872'; $document_name = 'whatsapp-cloud-api-from-id.pdf'; $document_caption = 'WhastApp API Cloud Guide'; // With the Media Object ID of some document upload on the WhatsApp Cloud servers $media_id = new MediaObjectID($document_id); $whatsapp_cloud_api->sendDocument('34676104574', $media_id, $document_name, $document_caption); // Or $document_link = 'https://jackie-source.es/wp-content/uploads/2022/05/image.png'; $link_id = new LinkID($document_link); $whatsapp_cloud_api->sendDocument('34676104574', $link_id, $document_name, $document_caption);
发送模板消息
<?php $whatsapp_cloud_api->sendTemplate('34676104574', 'hello_world', 'en_US'); // Language is optional
您也可以使用参数构建模板
<?php use JackieSource\WhatsAppCloudApi\Message\Template\Component; $component_header = []; $component_body = [ [ 'type' => 'text', 'text' => '*Mr Jones*', ], ]; $component_buttons = [ [ 'type' => 'button', 'sub_type' => 'quick_reply', 'index' => 0, 'parameters' => [ [ 'type' => 'text', 'text' => 'Yes', ] ] ], [ 'type' => 'button', 'sub_type' => 'quick_reply', 'index' => 1, 'parameters' => [ [ 'type' => 'text', 'text' => 'No', ] ] ] ]; $components = new Component($component_header, $component_body, $component_buttons); $whatsapp_cloud_api->sendTemplate('34676104574', 'sample_issue_resolution', 'en_US', $components); // Language is optional
发送音频消息
<?php use JackieSource\WhatsAppCloudApi\Message\Media\LinkID; $audio_link = 'https://jackie-source.es/wp-content/uploads/2022/05/file_example_OOG_1MG.ogg'; $link_id = new LinkID($audio_link); $whatsapp_cloud_api->sendAudio('34676104574', $link_id);
发送图片消息
<?php use JackieSource\WhatsAppCloudApi\Message\Media\LinkID; use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID; $link_id = new LinkID('http(s)://image-url'); $whatsapp_cloud_api->sendImage('<destination-phone-number>', $link_id); //or $media_id = new MediaObjectID('<image-object-id>'); $whatsapp_cloud_api->sendImage('<destination-phone-number>', $media_id);
发送视频消息
<?php use JackieSource\WhatsAppCloudApi\Message\Media\LinkID; use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID; $link_id = new LinkID('http(s)://video-url'); $whatsapp_cloud_api->sendVideo('<destination-phone-number>', $link_id, '<video-caption>'); //or $media_id = new MediaObjectID('<image-object-id>'); $whatsapp_cloud_api->sendVideo('<destination-phone-number>', $media_id, '<video-caption>');
发送贴纸消息
贴纸示例:https://github.com/WhatsApp/stickers
<?php use JackieSource\WhatsAppCloudApi\Message\Media\LinkID; use JackieSource\WhatsAppCloudApi\Message\Media\MediaObjectID; $link_id = new LinkID('http(s)://sticker-url'); $whatsapp_cloud_api->sendSticker('<destination-phone-number>', $link_id); //or $media_id = new MediaObjectID('<sticker-object-id>'); $whatsapp_cloud_api->sendSticker('<destination-phone-number>', $media_id);
发送位置消息
<?php $whatsapp_cloud_api->sendLocation('<destination-phone-number>', $longitude, $latitude, $name, $address);
发送联系消息
<?php use JackieSource\WhatsAppCloudApi\Message\Contact\ContactName; use JackieSource\WhatsAppCloudApi\Message\Contact\Phone; use JackieSource\WhatsAppCloudApi\Message\Contact\PhoneType; $name = new ContactName('Adams', 'Smith'); $phone = new Phone('34676204577', PhoneType::CELL()); $whatsapp_cloud_api->sendContact('<destination-phone-number>', $name, $phone);
发送列表消息
<?php use JackieSource\WhatsAppCloudApi\Message\OptionsList\Row; use JackieSource\WhatsAppCloudApi\Message\OptionsList\Section; use JackieSource\WhatsAppCloudApi\Message\OptionsList\Action; $rows = [ new Row('1', '⭐️', "Experience wasn't good enough"), new Row('2', '⭐⭐️', "Experience could be better"), new Row('3', '⭐⭐⭐️', "Experience was ok"), new Row('4', '⭐⭐️⭐⭐', "Experience was good"), new Row('5', '⭐⭐️⭐⭐⭐️', "Experience was excellent"), ]; $sections = [new Section('Stars', $rows)]; $action = new Action('Submit', $sections); $whatsapp_cloud_api->sendList( '<destination-phone-number>', 'Rate your experience', 'Please consider rating your shopping experience in our website', 'Thanks for your time', $action );
媒体消息
上传媒体资源
媒体消息接受指向公共资源(图片、视频、音频等)的Internet URL作为标识符。当您尝试从URL发送媒体消息时,必须实例化LinkID
对象。
您还可以将您的媒体资源上传到WhatsApp服务器,您将收到一个资源标识符
$response = $whatsapp_cloud_api->uploadMedia('my-image.png'); $media_id = new MediaObjectID($response->decodedBody()['id']); $whatsapp_cloud_api->sendImage('<destination-phone-number>', $media_id);
下载媒体资源
要下载媒体资源
$response = $whatsapp_cloud_api->downloadMedia('<media-id>');
消息响应
WhatsAppCloudApi实例在WhatsApp服务器返回错误时返回Response类或ResponseException。
try { $response = $this->whatsapp_app_cloud_api->sendTextMessage( '<destination-phone-number>, 'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es', true ); } catch (\JackieSource\WhatsAppCloudApi\Response\ResponseException $e) { print_r($e->response()); // You can still check the Response returned from Meta servers }
Webhooks
Webhook验证
在您的Meta App仪表板中添加您的webhook。您需要验证您的webhook
<?php require 'vendor/autoload.php'; use JackieSource\WhatsAppCloudApi\WebHook; // Instantiate the WhatsAppCloudApi super class. $webhook = new WebHook(); echo $webhook->verify($_GET, "<the-verify-token-defined-in-your-app-dashboard>");
Webhook通知
Webhook已验证,您将开始接收每次客户发送消息时的通知。
<?php require 'vendor/autoload.php'; define('STDOUT', fopen('php://stdout', 'w')); use JackieSource\WhatsAppCloudApi\WebHook; $payload = file_get_contents('php://input'); fwrite(STDOUT, print_r($payload, true) . "\n"); // Instantiate the Webhook super class. $webhook = new WebHook(); fwrite(STDOUT, print_r($webhook->read(json_decode($payload, true)), true) . "\n");
Webhook::read
函数将返回一个Notification
实例。请探索可用的不同通知。
标记消息为已读
当您从Webhooks收到传入消息时,您可以通过将其状态更改为已读来标记消息为已读。标记为已读的消息在其时间戳旁边显示两个蓝色勾选标记。
标记消息为已读也将标记对话中较早的消息为已读。
<?php $whatsapp_cloud_api->markMessageAsRead('<message-id>');
功能
- 发送文本消息
- 发送文档
- 发送带参数的模板
- 发送音频
- 发送图片
- 发送视频
- 发送贴纸
- 发送位置
- 发送联系信息
- 发送列表
- 将媒体资源上传到WhatsApp服务器
- 从WhatsApp服务器下载媒体资源
- 标记消息为已读
- Webhook验证
- Webhook通知
获取帮助
迁移到v2
请参阅UPGRADE以获取有关如何升级到v2的更多信息。
变更日志
请参阅CHANGELOG以获取有关最近更改的更多信息。
测试
composer unit-test
您还可以运行测试,向WhatsApp Cloud API进行真实调用。请将您的测试凭据放置在WhatsAppCloudApiTestConfiguration文件中。
composer integration-test
贡献
请参阅CONTRIBUTING以获取详细信息。
许可
MIT许可(MIT)。请参阅许可文件以获取更多信息。请参阅许可文件以获取更多信息。
免责声明
此包不是由Facebook官方维护的。WhatsApp和Facebook商标和标志是Meta Platforms, Inc.的财产。