chandachewe / whatsapp
用于商业WhatsApp API的PHP包
v1.0.0
2024-03-19 11:08 UTC
Requires
- guzzlehttp/guzzle: ^7.8
- vlucas/phpdotenv: ^5.6
Requires (Dev)
- pestphp/pest: ^2.34
This package is auto-updated.
Last update: 2024-09-19 12:38:49 UTC
README
本包展示了如何从PHP项目发送WhatsApp Business API请求。
要求
- PHP >= 8.1
安装
使用composer require
安装此包
composer require chandachewe/whatsapp
用法
模板消息
您需要从whatsapp business API的开发者门户创建模板消息
require 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\TemplateMessage; $templateMessage = new TemplateMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $templateMessage->template( 'template_name', 'language_code (optional)' ); echo $response;
您的版本应四舍五入到一位小数,例如v19.0,而不是v19。语言代码在此是可选的。如果为空,则默认为en_US
发送文本消息
您只能将模板类型消息作为第一条消息发送给用户。如果想要发送文本消息,用户必须先向您发送消息或回复您发送的任何消息。
<?php require_once 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\TextMessage; $textMessage = new TextMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $textMessage->text( 'You have to check out this amazing messaging package at https://github.com/chandachewe10/whatsapp-api' ); echo $response;
发送列表消息
这些消息包括最多10个选项的菜单。这种类型的消息为用户在与商家互动时进行选择提供了一个更简单、更一致的方式。
<?php require_once 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\ListMessage; $listMessage = new ListMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $listMessage->list( 'Header', 'Body Message', 'Footer (Optional)', [ 'button' => 'title on drop down button', 'sections' => [ [ 'title' => 'first row title', 'rows' => [ [ 'id' => '100993900202', 'title' => 'Your heading', 'description' => 'Your description' ] ], // You can add another title and row here ... ] ] ] ); echo $response;
发送按钮选项消息
这些消息包括最多3个选项——每个选项是一个按钮。这种类型的消息为用户在与商家互动时从菜单中进行选择提供了一个更快捷的方式。
<?php require_once 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\ButtonMessage; $buttonMessage = new ButtonMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $buttonMessage->button( 'Header', 'Body Message', 'Footer (Optional)', [ 'buttons' => [ [ 'type' => 'reply', 'reply' => [ 'id' => 'unique-postback-id', 'title' => 'your button title' ] ], // You can add another type and reply here ... ] ], 'text', '' ); echo $response;
有时您可能想要在页眉中包含图像,并在图像下方包含按钮选项。您可以通过使用
image
而不是text
并提供图像link
来实现这一点。因此,您的代码现在将是
<?php require_once 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\ButtonMessage; $buttonMessage = new ButtonMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $buttonMessage->button( 'Header', 'Body Message', 'Footer (Optional)', [ 'buttons' => [ [ 'type' => 'reply', 'reply' => [ 'id' => 'unique-postback-id', 'title' => 'your button title' ] ], // You can add another type and reply here ... ] ], 'image', // note that we have replaced text with image 'https://path_to_your_image' // note that we have added the image link ); echo $response;
有时您可能想要在页眉中包含文档,并在文档下方包含按钮选项。您可以通过使用
document
而不是text
并提供文档link
来实现这一点。因此,您的代码现在将是
<?php require_once 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\ButtonMessage; $buttonMessage = new ButtonMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $buttonMessage->button( 'Header', 'Body Message', 'Footer (Optional)', [ 'buttons' => [ [ 'type' => 'reply', 'reply' => [ 'id' => 'unique-postback-id', 'title' => 'your button title' ] ], // You can add another type and reply here ... ] ], 'document', // note that we have replaced text with document 'https://path_to_your_document' // note that we have added the document link ); echo $response;
有时您可能想要在页眉中显示一个短视频剪辑,并在视频下方包含按钮选项。您可以通过使用
video
而不是text
并提供视频link
来实现这一点。因此,您的代码现在将是
<?php require_once 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\ButtonMessage; $buttonMessage = new ButtonMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $buttonMessage->button( 'Header', 'Body Message', 'Footer (Optional)', [ 'buttons' => [ [ 'type' => 'reply', 'reply' => [ 'id' => 'unique-postback-id', 'title' => 'your button title' ] ], // You can add another type and reply here ... ] ], 'video', // note that we have replaced text with video 'https://path_to_your_video' // note that we have added the video link ); echo $response;
发送位置消息
这些消息允许您向您的客户/客户发送您的位置地址。
<?php require_once 'vendor/autoload.php'; use Chandachewe\Whatsapp\Messages\LocationMessage; $locationMessage = new LocationMessage( 'v19.0', 'BUSINESS PHONE NUMBER ID', 'RECIPIENT PHONE NUMBER', 'TOKEN' ); $response = $locationMessage->location( 'Longitude', 'Latitude', 'Name of location ', 'Address', 'Body', 'optional footer', [ "name" => "send_location" ] ); echo $response;