asim-altayb / whatsapp-api-laravel
帮助您处理 WhatsApp Api 云服务。
2.0.0
2024-09-04 20:50 UTC
Requires
- php: ^7.4 || ^8.0 || ^8.1
- guzzlehttp/guzzle: ^7.0
- illuminate/support: ^6.9|^7.0|^8.0|^9.0|^10.0
- myclabs/php-enum: ^1.8
- vlucas/phpdotenv: ^5.4
README
此库是从 "netflie/whatsapp-cloud-api" 克隆的,作为原生 PHP 开发,以支持 Laravel 应用程序。
它能做什么
此包使开发人员能够轻松地在 PHP 代码中访问 WhatsApp Cloud API 服务。
每月前 1,000 次 对话免费,每次对话。
入门指南
请根据官方指南中的“入门”部分创建和配置您的 Facebook WhatsApp 应用程序。
最低要求 – 要运行 SDK,您的系统需要 PHP >= 7.4 以及带 OpenSSL 和 zlib 编译的 CURL >=7.19.4 的最新版本。
安装
composer require asim-altayb/whatsapp-api-laravel
使用 Composer Dump 加载文件
composer dump-autoload
快速示例
发送文本消息
<?php use AsimAltayb\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('96651234567', 'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.AsimAltayb.es');
发送文档
您可以通过两种方式发送文档:将文件上传到 WhatsApp 云服务器(您将收到一个标识符)或从互联网上发布的文档链接。
<?php use AsimAltayb\WhatsAppCloudApi\Message\Media\LinkID; use AsimAltayb\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('96651234567', $media_id, $document_name, $document_caption); // Or $document_link = 'https://link.com/image.png'; $link_id = new LinkID($document_link); $whatsapp_cloud_api->sendDocument('96651234567', $link_id, $document_name, $document_caption);
发送模板消息
<?php $whatsapp_cloud_api->sendTemplate('96651234567', 'hello_world', 'en_US'); // Language is optional
您还可以构建带有参数的模板
<?php use AsimAltayb\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('96651234567', 'sample_issue_resolution', 'en_US', $components); // Language is optional
发送音频消息
<?php use AsimAltayb\WhatsAppCloudApi\Message\Media\LinkID; $audio_link = 'https://link.com/file.ogg'; $link_id = new LinkID($audio_link); $whatsapp_cloud_api->sendAudio('96651234567', $link_id);
发送图片消息
<?php use AsimAltayb\WhatsAppCloudApi\Message\Media\LinkID; use AsimAltayb\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 AsimAltayb\WhatsAppCloudApi\Message\Media\LinkID; use AsimAltayb\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 AsimAltayb\WhatsAppCloudApi\Message\Media\LinkID; use AsimAltayb\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 AsimAltayb\WhatsAppCloudApi\Message\Contact\ContactName; use AsimAltayb\WhatsAppCloudApi\Message\Contact\Phone; use AsimAltayb\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 AsimAltayb\WhatsAppCloudApi\Message\OptionsList\Row; use AsimAltayb\WhatsAppCloudApi\Message\OptionsList\Section; use AsimAltayb\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 );
功能
- 发送文本消息
- 发送文档
- 发送带参数的模板
- 发送音频
- 发送图片
- 发送视频
- 发送贴纸
- 发送位置
- 发送联系人
- 发送列表
获取帮助
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG。
测试
composer unit-test
您还可以通过向 WhatsApp Cloud API 进行真实调用来运行测试。请将您的测试凭据放在 WhatsAppCloudApiTestConfiguration 文件中。
composer integration-test
贡献
有关详细信息,请参阅 CONTRIBUTING。
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件。有关更多信息,请参阅 许可证文件。
免责声明
此包不是由 Facebook 官方维护。WhatsApp 和 Facebook 的商标和标志是 Meta Platforms, Inc. 的财产。