adrii / whatsapp-api
WhatsApp-Api 是一个轻量级的库,可以轻松与 WhatsApp 商务平台的云 API 进行交互。
0.7.0
2023-05-19 14:50 UTC
Requires
- adrii/curl-helper: ^1.8
README
WhatsApp-Api 是一个轻量级的库,可以轻松与 WhatsApp 商务平台 的云 API 进行交互。
安装
使用 Composer 安装库。
composer require adrii/whatsapp-api
Composer
use Adrii\Whatsapp\Whatsapp;
配置
$graph_version = "v15.0"; $phone_number_id = "{phone_number_id}"; $access_token = "{access_token}"; $recipient_id = "{recipient_id}"; $ws = new Whatsapp($phone_number_id, $access_token, $graph_version);
Webhook
当你收到消息或消息状态发生变化时,你需要为你的应用设置一个 webhook 连接点。
此方法自行处理整个连接请求。使用 access_token 来验证连接。
$ws->webhook()->connect($_GET);
消息
文本
发送基本文本(允许使用表情符号)。
$ws->send_message()->text("Aloha 🍍", $recipient_id);
模板
发送在 Meta 管理器中定义的消息模板。
$ws->send_message()->template("hello_world", $recipient_id);
发送带有参数的消息模板
$component_header = array( "type" => "header", "parameters" => array( array( "type" => "image", "image" => array( "link" => "https://avatars.githubusercontent.com/u/29653964?v=4" ) ), ) ); $component_body = array( "type" => "body", "parameters" => array( array( "type" => "text", "text" => "Adrii 🍍" ) ) ); $component_button = array( "type" => "button", "sub_type" => "url", "index" => 0, "parameters" => array( array( "type" => "text", "text" => "https://github.com/AdrianVillamayor/Whatsapp-Api" ) ) ); $ws->send_message()->addComponent($component_header, $component_body, $component_button); $response = $ws->send_message()->template("sample_purchase_feedback", $recipient_id);
位置
通过经纬度和地址发送位置。
$ws->send_message()->location("41.403191", "2.174840", "La Sagrada Família", "C/ De Mallorca, 401, 08013 Barcelona", $recipient_id);
联系人
发送联系人消息
名称是唯一必需的参数,其他数据为可选。
$contact = array( "addresses" => array( array( "city" => "city name", "country" => "country name", "country_code" => "code", "state" => "Contact's State", "street" => "Contact's Street", "type" => "Contact's Address Type", "zip" => "Contact's Zip Code" ) ), "birthday" => "14-02-1997", "emails" => array( array( "email" => "email", "type" => "HOME" ), array( "email" => "email", "type" => "WORK" ) ), "name" => array( "formatted_name" => "formatted name value", "middle_name" => "last name value", ), "phones" => array( array( "phone" => "654034823", "type" => "MAIN" ), array( "phone" => "Phone number", "type" => "HOME" ), array( "phone" => "Phone number", "type" => "WORK" ) ), "urls" => array( array( "url" => "some url", "type" => "WORK" ) ) ); $ws->send_message()->addContact($contact); $response = $ws->send_message()->contact($recipient_id);
你可以连接任意多的联系人
$ws->send_message()->addContact($contact_0, $contact_1, ...);
媒体
通过链接或 ID 发送媒体
$ws->send_message()->media("image", "https://avatars.githubusercontent.com/u/29653964?v=4", $recipient_id); $ws->send_message()->media("video", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/04/file_example_MP4_480_1_5MG.mp4", $recipient_id); $ws->send_message()->media("document", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/10/file-sample_150kB.pdf", $recipient_id); $ws->send_message()->media("audio", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/11/file_example_MP3_700KB.mp3", $recipient_id); $ws->send_message()->media("sticker", "https://img-03.stickers.cloud/packs/210a9e68-b249-405f-8ea1-9af015ef074a/webp/c5b7bded-e0f0-4f79-86aa-ffd825aba680.webp", $recipient_id);
用标题描述指定的图片或视频媒体。
$ws->send_message()->media("image", "https://avatars.githubusercontent.com/u/29653964?v=4", $recipient_id, "individual", true, "your-image-caption-to-be-sent"); $ws->send_message()->media("video", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/04/file_example_MP4_480_1_5MG.mp4", $recipient_id, "individual", true, "your-video-caption-to-be-sent");
描述特定文档的文件名。
$ws->send_message()->media("document", "https://file-examples.com/storage/fe4658769b6331540b05587/2017/10/file-sample_150kB.pdf", $recipient_id, "individual", true, null, "example_filename.pdf");
交互式
发送带有回复按钮的交互式消息
$button = [ "header" => "Header", "body" => "Body", "footer" => "Footer", "action" => [ "buttons" => [ [ "type" => "reply", "reply" => [ "id" => "UNIQUE_BUTTON_ID_1", "title" => "BUTTON_TITLE_1" ] ], [ "type" => "reply", "reply" => [ "id" => "UNIQUE_BUTTON_ID_2", "title" => "BUTTON_TITLE_2" ] ] ] ] ]; $ws->send_message()->interactive($button, $recipient_id, "button");
发送带有按钮列表的交互式消息
$list = [ "header" => "Test Header", "body" => "Test Body", "footer" => "Test Footer", "action" => [ "button" => "BUTTON_TEXT", "sections" => [ [ "title" => "SECTION_1_TITLE", "rows" => [ [ "id" => "SECTION_1_ROW_1_ID", "title" => "SECTION_1_ROW_1_TITLE", "description" => "SECTION_1_ROW_1_DESCRIPTION" ], [ "id" => "SECTION_1_ROW_2_ID", "title" => "SECTION_1_ROW_2_TITLE", "description" => "SECTION_1_ROW_2_DESCRIPTION" ] ] ], [ "title" => "SECTION_2_TITLE", "rows" => [ [ "id" => "SECTION_2_ROW_1_ID", "title" => "SECTION_2_ROW_1_TITLE", "description" => "SECTION_2_ROW_1_DESCRIPTION" ], [ "id" => "SECTION_2_ROW_2_ID", "title" => "SECTION_2_ROW_2_TITLE", "description" => "SECTION_2_ROW_2_DESCRIPTION" ] ] ] ] ] ]; $ws->send_message()->interactive($list, $recipient_id, "list");
每种消息的数据示例
贡献
欢迎提交拉取请求。对于重大更改,请先提交一个问题来讨论你想要更改的内容。
请确保适当地更新测试。