adrii/whatsapp-api
最新稳定版本:0.7.0
Composer 安装命令:
composer require adrii/whatsapp-api
包简介
WhatsApp-Api is a lightweight library to easily interact with cloud APIs of the WhatsApp Business Platform.
README 文档
README
WhatsApp-Api is a lightweight library to easily interact with cloud APIs of the WhatsApp Business Platform.
| INDEX |
|---|
| Installation |
| Configutation |
| Webhook |
| Messages |
| License |
Installation
Use Composer to install the library.
composer require adrii/whatsapp-api
Composer
use Adrii\Whatsapp\Whatsapp;
Configutation
$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
To be alerted when you receive a message or when the status of a message changes, you need to set up a webhook connection point for your app.
This method handles the entire connection request on its own. The access_token is used to validate the connection.
$ws->webhook()->connect($_GET);
Messages
| Status | Type |
|---|---|
| ✅ | Text |
| ✅ | Template |
| ✅ | Location |
| ✅ | Contact |
| ✅ | Media |
| ✅ | Interactive |
Text
Send basic text (emojis allowed).
$ws->send_message()->text("Aloha 🍍", $recipient_id);
Template
Send message templates defined in the Meta manager.
$ws->send_message()->template("hello_world", $recipient_id);
Send message templates defined in the Meta manager with parameters
$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);
Location
Sends a location, through a longitude, latitude and an address.
$ws->send_message()->location("41.403191", "2.174840", "La Sagrada Família", "C/ De Mallorca, 401, 08013 Barcelona", $recipient_id);
Contact
Send a contact message
The name is the only required parameter, the other data are optional.
$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);
You can concatenate as many contacts as you want
$ws->send_message()->addContact($contact_0, $contact_1, ...);
Media
Send a media, as a link or 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);
Describes the specified image or video media with caption.
$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");
Describes the filename for the specific document.
$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");
Interactive
Send an interactive message with reply buttons
$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");
Send an interactive message with list of buttons
$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");
Data examples for each message
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.
License
Thanks for your help! 🎉
统计信息
- 总下载量: 3.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 30
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-09-15