netflie/whatsapp-cloud-api 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

netflie/whatsapp-cloud-api

Composer 安装命令:

composer require netflie/whatsapp-cloud-api

包简介

The first PHP SDK to send and receive messages using a cloud-hosted version of the WhatsApp Business Platform

README 文档

README

What It Does

This package makes it easy for developers to access WhatsApp Cloud API service in their PHP code.

The first 1,000 conversations each month are free from WhatsApp Cloud API. A conversation.

Getting Started

Please create and configure your Facebook WhatsApp application following the "Get Stared" section of the official guide.

Minimum requirements – To run the SDK, your system will require PHP >= 7.4 with a recent version of CURL >=7.19.4 compiled with OpenSSL and zlib.

Installation

composer require netflie/whatsapp-cloud-api

Quick Examples

Send a text message

<?php

// Require the Composer autoloader.
require 'vendor/autoload.php';

use Netflie\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');

Send a message using BSUID recipient

<?php

$recipient_bsuid = 'US.13491208655302741918';

// Send using BSUID only
$whatsapp_cloud_api->sendTextMessage(null, 'Hello from BSUID', false, $recipient_bsuid);

// Send both values in the same request (WhatsApp Cloud API gives precedence to `to`)
$whatsapp_cloud_api->sendTextMessage('34676104574', 'Hello with both identifiers', false, $recipient_bsuid);

The same optional $recipient parameter is available in all send* methods that send messages to /messages.

For sendContact, pass the BSUID before the phone entries:

$contact_name = new \Netflie\WhatsAppCloudApi\Message\Contact\ContactName('John', 'Doe');
$phone = new \Netflie\WhatsAppCloudApi\Message\Contact\Phone('+14155550100', \Netflie\WhatsAppCloudApi\Message\Contact\PhoneType::CELL());

$whatsapp_cloud_api->sendContact(null, $contact_name, $recipient_bsuid, $phone);

Send a document

You can send documents in two ways: by uploading a file to the WhatsApp Cloud servers (where you will receive an identifier) or from a link to a document published on internet.

<?php

use Netflie\WhatsAppCloudApi\Message\Media\LinkID;
use Netflie\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://netflie.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);

Send a template message

<?php

$whatsapp_cloud_api->sendTemplate('34676104574', 'hello_world', 'en_US'); // If not specified, Language will be default to en_US and otherwise it will be required.

You also can build templates with parameters:

<?php

use Netflie\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

Send an audio message

<?php

use Netflie\WhatsAppCloudApi\Message\Media\LinkID;

$audio_link = 'https://netflie.es/wp-content/uploads/2022/05/file_example_OOG_1MG.ogg';
$link_id = new LinkID($audio_link);
$whatsapp_cloud_api->sendAudio('34676104574', $link_id);

Send an image message

<?php

use Netflie\WhatsAppCloudApi\Message\Media\LinkID;
use Netflie\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);

Send a video message

<?php

use Netflie\WhatsAppCloudApi\Message\Media\LinkID;
use Netflie\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>');

Send a sticker message

Stickers sample: https://github.com/WhatsApp/stickers

<?php

use Netflie\WhatsAppCloudApi\Message\Media\LinkID;
use Netflie\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);

Send a location message

<?php

$whatsapp_cloud_api->sendLocation('<destination-phone-number>', $longitude, $latitude, $name, $address);

Send a location request message

<?php

$body = 'Let\'s start with your pickup. You can either manually *enter an address* or *share your current location*.';
$whatsapp_cloud_api->sendLocationRequest('<destination-phone-number>', $body);

Send a contact message

<?php

use Netflie\WhatsAppCloudApi\Message\Contact\ContactName;
use Netflie\WhatsAppCloudApi\Message\Contact\Phone;
use Netflie\WhatsAppCloudApi\Message\Contact\PhoneType;

$name = new ContactName('Adams', 'Smith');
$phone = new Phone('34676204577', PhoneType::CELL());

$whatsapp_cloud_api->sendContact('<destination-phone-number>', $name, null, $phone);

Send a list message

<?php

use Netflie\WhatsAppCloudApi\Message\OptionsList\Row;
use Netflie\WhatsAppCloudApi\Message\OptionsList\Section;
use Netflie\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
);

Send a CTA URL message

<?php

use Netflie\WhatsAppCloudApi\Message\CtaUrl\TitleHeader;

$header = new TitleHeader('Booking');

$whatsapp_cloud_api->sendCtaUrl(
    '<destination-phone-number>',
    'See Dates',
    'https://www.example.com',
    $header,
    'Tap the button below to see available dates.',
    'Dates subject to change.',
);

Send Catalog Message

<?php

$body = 'Hello! Thanks for your interest. Ordering is easy. Just visit our catalog and add items you\'d like to purchase.';
$footer = 'Best grocery deals on WhatsApp!';
$sku_thumbnail = '<product-sku-id>'; // product sku id to use as header thumbnail

$whatsapp_cloud_api->sendCatalog(
    '<destination-phone-number>',
    $body,
    $footer, // optional
    $sku_thumbnail // optional
);

Send a button reply message

<?php

use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\Button;
use Netflie\WhatsAppCloudApi\Message\ButtonReply\ButtonAction;

$whatsapp_cloud_api = new WhatsAppCloudApi([
  'from_phone_number_id' => 'your-configured-from-phone-number-id',
  'access_token' => 'your-facebook-whatsapp-application-token' 
]);

$rows = [
    new Button('button-1', 'Yes'),
    new Button('button-2', 'No'),
    new Button('button-3', 'Not Now'),
];
$action = new ButtonAction($rows);

$whatsapp_cloud_api->sendButton(
    '<destination-phone-number>',
    'Would you like to rate us on Trustpilot?',
    $action,
    'RATE US', // Optional: Specify a header (type "text")
    'Please choose an option' // Optional: Specify a footer 
);

Send Multi Product Message

<?php

use Netflie\WhatsAppCloudApi\WhatsAppCloudApi;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Row;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Section;
use Netflie\WhatsAppCloudApi\Message\MultiProduct\Action;

$rows_section_1 = [
    new Row('<product-sku-id>'),
    new Row('<product-sku-id>'),
    // etc
];

$rows_section_2 = [
    new Row('<product-sku-id>'),
    new Row('<product-sku-id>'),
    new Row('<product-sku-id>'),
    // etc
];

$sections = [
    new Section('Section 1', $rows_section_1),
    new Section('Section 2', $rows_section_2),
];

$action = new Action($sections);
$catalog_id = '<catalog-id>';
$header = 'Grocery Collections';
$body = 'Hello! Thanks for your interest. Here\'s what we can offer you under our grocery collection. Thank you for shopping with us.';
$footer = 'Subject to T&C';

$whatsapp_cloud_api->sendMultiProduct(
    '<destination-phone-number>',
    $catalog_id,
    $action,
    $header,
    $body,
    $footer // optional
);

Send Single Product Message

<?php

$catalog_id = '<catalog-id>';
$sku_id = '<product-sku-id>';
$body = 'Hello! Here\'s your requested product. Thanks for shopping with us.';
$footer = 'Subject to T&C';

$whatsapp_cloud_api->sendSingleProduct(
    '<destination-phone-number>',
    $catalog_id,
    $sku_id,
    $body, // body: optional
    $footer // footer: optional
);

Replying messages

You can reply a previous sent message:

<?php

$whatsapp_cloud_api
    ->replyTo('<whatsapp-message-id-to-reply>')
    ->sendTextMessage(
        '34676104574',
        'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.netflie.es'
    );

React to a Message

You can react to a message from your conversations if you know the messageid

<?php

$whatsapp_cloud_api->sendReaction(
        '<destination-phone-number>',
        '<message-id-to-react-to>',
        '👍', // the emoji
    );

// Unreact to a message
$whatsapp_cloud_api->sendReaction(
        '<destination-phone-number>',
        '<message-id-to-unreact-to>'
    );

Media messages

Upload media resources

Media messages accept as identifiers an Internet URL pointing to a public resource (image, video, audio, etc.). When you try to send a media message from a URL you must instantiate the LinkID object.

You can also upload your media resources to WhatsApp servers and you will receive a resource identifier:

$response = $whatsapp_cloud_api->uploadMedia('my-image.png');

$media_id = new MediaObjectID($response->decodedBody()['id']);
$whatsapp_cloud_api->sendImage('<destination-phone-number>', $media_id);

Download media resources

To download a media resource:

$response = $whatsapp_cloud_api->downloadMedia('<media-id>');

Message Response

WhatsAppCloudApi instance returns a Response class or a ResponseException if WhatsApp servers return an error.

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 (\Netflie\WhatsAppCloudApi\Response\ResponseException $e) {
    print_r($e->response()); // You can still check the Response returned from Meta servers
}

Webhooks

Webhook verification

Add your webhook in your Meta App dashboard. You need to verify your webhook:

<?php
require 'vendor/autoload.php';

use Netflie\WhatsAppCloudApi\WebHook;

// Instantiate the WhatsAppCloudApi super class.
$webhook = new WebHook();

echo $webhook->verify($_GET, "<the-verify-token-defined-in-your-app-dashboard>");

Webhook notifications

Webhook is now verified, you will start receiving notifications every time your customers send messages.

<?php
require 'vendor/autoload.php';
define('STDOUT', fopen('php://stdout', 'w'));

use Netflie\WhatsAppCloudApi\WebHook;


$payload = file_get_contents('php://input');
fwrite(STDOUT, print_r($payload, true) . "\n");

// Instantiate the Webhook super class.
$webhook = new WebHook();

// Read the first message
fwrite(STDOUT, print_r($webhook->read(json_decode($payload, true)), true) . "\n");

//Read all messages in case Meta decided to batch them
fwrite(STDOUT, print_r($webhook->readAll(json_decode($payload, true)), true) . "\n");

The Webhook::read function will return a Notification instance. Please, explore the different notifications availables.

Mark a message as read

When you receive an incoming message from Webhooks, you can mark the message as read by changing its status to read. Messages marked as read display two blue check marks alongside their timestamp.

Marking a message as read will also mark earlier messages in the conversation as read.

<?php

$whatsapp_cloud_api->markMessageAsRead('<message-id>');

Send a typing indicator

Show a "typing..." indicator in the user's chat for up to 25 seconds or until you send a message, whichever comes first. This also marks the referenced message as read.

<?php

$whatsapp_cloud_api->sendTypingIndicator('<message-id>');

Get Business Profile

<?php

$whatsapp_cloud_api->businessProfile('<fields>');

Update Business Profile

<?php

$whatsapp_cloud_api->updateBusinessProfile([
    'about' => '<about_text>',
    'email' => '<email>'
]);

Create a Template

<?php

$whatsapp = new WhatsAppCloudApi([
    'from_phone_number_id' => 'your-configured-from-phone-number-id',
    'access_token'         => 'your-facebook-whatsapp-application-token',
    'business_id'          => 'your-business-id',
]);

$whatsapp->createTemplate(
    'seasonal_promotion',
    'MARKETING', // UTILITY | MARKETING | AUTHENTICATION
    'en_US',
    [
        [
            'type' => 'HEADER',
            'format' => 'TEXT',
            'text' => 'Our {{1}} is on!',
            'example' => [
                'header_text' => ['Summer Sale']
            ]
        ],
        [
            'type' => 'BODY',
            'text' => 'Shop now through {{1}} and use code {{2}} to get {{3}} off of all merchandise.',
            'example' => [
                'body_text' => [[ 'the end of August', '25OFF', '25%' ]]
            ]
        ],
        [
            'type' => 'FOOTER',
            'text' => 'Use the buttons below to manage your marketing subscriptions'
        ],
        [
            'type' => 'BUTTONS',
            'buttons' => [
                [
                    'type' => 'QUICK_REPLY',
                    'text' => 'Unsubscribe from Promos'
                ],
                [
                    'type' => 'QUICK_REPLY',
                    'text' => 'Unsubscribe from All'
                ]
            ]
        ]
    ]
);

Update a Template

<?php

$whatsapp = new WhatsAppCloudApi([
    'from_phone_number_id' => 'your-configured-from-phone-number-id',
    'access_token'         => 'your-facebook-whatsapp-application-token',
    'business_id'          => 'your-business-id',
]);

$template_id = 'your-template-id';

// New payload to update the template
$payload = [
    'category' => 'MARKETING',
    'language' => 'en_US',
    'components' => [
        [
            'type'    => 'HEADER',
            'format'  => 'TEXT',
            'text'    => 'Hello {{1}} is live now!',
            'example' => [
                'header_text' => ['Flash Sale']
            ]
        ],
        [
            'type'    => 'BODY',
            'text'    => 'Buy now before {{1}} ends. Use code {{2}} to get {{3}} off!',
            'example' => [
                'body_text' => [['midnight', 'FLASH20', '20%']]
            ]
        ],
        [
            'type' => 'FOOTER',
            'text' => 'Tap below to shop.'
        ],
        [
            'type'    => 'BUTTONS',
            'buttons' => [
                [
                    'type' => 'QUICK_REPLY',
                    'text' => 'Show Me Deals'
                ],
                [
                    'type' => 'QUICK_REPLY',
                    'text' => 'Stop Promotions'
                ]
            ]
        ]
    ]
];

$whatsapp->updateTemplateById($template_id, $payload);

Fields list: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/business-profiles

Features

  • Send Text Messages
  • Send Documents
  • Send Templates with parameters
  • Send Audios
  • Send Images
  • Send Videos
  • Send Stickers
  • Send Locations
  • Send Location Request
  • Send Contacts
  • Send Lists
  • Send Buttons
  • Send Multi Product Message
  • Send Single Product
  • Upload media resources to WhatsApp servers
  • Download media resources from WhatsApp servers
  • Mark messages as read
  • Send typing indicator
  • React to a Message
  • Get/Update Business Profile
  • Webhook verification
  • Webhook notifications
  • Create Template
  • Update Template

Getting Help

Migration to v2

Please see UPGRADE for more information on how to upgrade to v2.

Changelog

Please see CHANGELOG for more information what has changed recently.

Testing

composer unit-test

You also can run tests making real calls to the WhastApp Clou API. Please put your testing credentials on WhatsAppCloudApiTestConfiguration file.

composer integration-test

Contributing

Please see CONTRIBUTING for details.

License

The MIT License (MIT). Please see License File for more information. Please see License file for more information.

Disclaimer

This package is not officially maintained by Facebook. WhatsApp and Facebook trademarks and logos are the property of Meta Platforms, Inc.

netflie/whatsapp-cloud-api 适用场景与选型建议

netflie/whatsapp-cloud-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 536.47k 次下载、GitHub Stars 达 666, 最近一次更新时间为 2022 年 05 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 netflie/whatsapp-cloud-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 netflie/whatsapp-cloud-api 我们能提供哪些服务?
定制开发 / 二次开发

基于 netflie/whatsapp-cloud-api 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 536.47k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 673
  • 点击次数: 19
  • 依赖项目数: 6
  • 推荐数: 0

GitHub 信息

  • Stars: 666
  • Watchers: 29
  • Forks: 238
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-24