dogfromthemoon/laravel-whatsapp-sender

Laravel WhatsApp Sender 允许您从 Laravel 应用程序发送 WhatsApp 消息。

dev-main 2023-10-24 07:59 UTC

This package is auto-updated.

Last update: 2024-09-24 09:58:55 UTC


README

Latest Version on Packagist Total Downloads GitHub Actions

Laravel WhatsApp Sender 是一个用于从 Laravel 发送 WhatsApp 消息的软件包。

安装

您可以通过 composer 安装此软件包

composer require dogfromthemoon/laravel-whatsapp-sender

配置

在您可以使用 Laravel WhatsApp Sender 之前,您需要在您的 .env 文件中设置 WhatsApp API 凭证

WHATSAPP_PHONE_NUMBER_ID=
WHATSAPP_TOKEN=

您可以从 Facebook Business Manager 网站获取您的 WhatsApp API 凭证。

  1. 创建一个新的应用。
  2. 将 WhatsApp 产品添加到您的应用中。
  3. 为您的应用添加一个电话号码,该号码将作为您的 WhatsApp 商业账户。
  4. 为您的 WhatsApp 商业账户生成一个永久令牌。
  5. 将您的 webhook URL 添加到您的应用中。

以下是一个验证 webhook 挑战代码的简单示例

Route::get('/verification', function(Request $request) {
    $challenge = $request->input('hub_challenge');

    // Validate the challenge code you provided when you created the webhook
    // For example, you can compare it with a value stored in your configuration
    $isValid = ($challenge === 'super-secret-challenge-code');

    if ($isValid) {
        return $challenge;
    } else {
        abort(404);
    }
});

使用方法

请记住导入它

use Dogfromthemoon\LaravelWhatsappSender\LaravelWhatsappSender;

发送消息

// If the conversation was started by the user, you can use this method to send messages.
// If you want to start a new conversation, you will need to use a template.

$whatsappSender = new LaravelWhatsappSender();

$phone = '1234567890'; // Phone number in E.164 format

$message = 'Hello, this is a test message!';

$response = $whatsappSender->sendTextMessage($phone, $message);

发送交互式列表

$phone = '1234567890';
$message = 'Please select an item from the list';
$sections = [
    [
        "rows" => [
            [
                "title" => "Item 1",
                "description" => "Description of Item 1",
                "id" => "item1"
            ],
            [
                "title" => "Item 2",
                "description" => "Description of Item 2",
                "id" => "item2"
            ]
        ]
    ]
];

$whatsappSender = new LaravelWhatsappSender();
$response = $whatsappSender->sendInteractiveList($phone, $message, $sections, 'VIEW');

发送按钮列表

$phone = '1234567890';
$message = 'Please choose an option:';
$buttons = [
    [
        'type' => 'reply',
        'reply' => [
            'id' => 'unique-id-paynow',
            'title' => 'button_pay_now',
        ]
    ],
    [
        'type' => 'reply',
        'reply' => [
            'id' => 'unique-id-moreoptions',
            'title' => 'button_more_options',
        ]
    ]
];

$response = $whatsappSender->sendButtonsMessage($phone, $message, $buttons);

发送多产品消息

$header = 'header-content';
$body = 'body-content';
$footer = 'footer-content';
$catalogId = 'CATALOG_ID';
$sections = [
    [
        'title' => 'section-title',
        'product_items' => [
            ['product_retailer_id' => 'product-SKU-in-catalog'],
            ['product_retailer_id' => 'product-SKU-in-catalog']
        ]
    ],
    [
        'title' => 'section-title',
        'product_items' => [
            ['product_retailer_id' => 'product-SKU-in-catalog'],
            ['product_retailer_id' => 'product-SKU-in-catalog']
        ]
    ]
];

$phone = '+1234567890';

$response = $whatsappSender->sendMultiProductMessage($phone, $header, $body, $footer, $catalogId, $sections);

发送单产品消息

$phone = '1234567890'; // Phone number in E.164 format
$catalogId = 'CATALOG_ID'; // The catalog ID for the product
$productId = 'PRODUCT_ID'; // The product retailer ID in the catalog
$bodyText = 'This is the body text of the product message';
$footerText = 'This is the footer text of the product message';

$response = $whatsappSender->sendProductMessage($phone, $catalogId, $productId, $bodyText, $footerText);

回复消息

// The phone number of the recipient in E.164 format
$phone = '1234567890';
// The message ID of the message you're replying to
$messageId = '12345';
// The text of the reply message
$reply = 'This is my reply to your message!';
$response = $whatsappSender->replyToMessage($phone, $messageId, $reply);

反应消息

$phone = '1234567890';
$messageId = 'wamid.HBgLM...';
$emoji = '\uD83D\uDE00';
$response = $whatsappSender->sendReaction($phone, $messageId, $emoji);

媒体消息

$phone = '1234567890'; // Phone number in E.164 format
$imageId = 'MEDIA-OBJECT-ID';

$response = $whatsappSender->sendImageMessage($phone, $imageId);

位置消息

$phone = '1234567890'; // Phone number in E.164 format
$longitude = -122.431297;
$latitude = 37.773972;
$locationName = 'Golden Gate Bridge';
$locationAddress = 'Golden Gate Bridge, San Francisco, CA, USA';

$response = $whatsappSender->sendLocationMessage($phone, $longitude, $latitude, $locationName, $locationAddress);

联系消息

$phone = '1234567890'; // Phone number in E.164 format
$contacts = [
        [
            "addresses" => [
                [
                    "street" => "STREET",
                    "city" => "CITY",
                    "state" => "STATE",
                    "zip" => "ZIP",
                    "country" => "COUNTRY",
                    "country_code" => "COUNTRY_CODE",
                    "type" => "HOME"
                ],
                [
                    // Additional addresses
                ]
            ],
            "birthday" => "1980-01-01",
            "emails" => [
                [
                    "email" => "EMAIL",
                    "type" => "WORK"
                ],
                // Additional emails
            ],
            "name" => [
                "formatted_name" => "NAME",
                "first_name" => "FIRST_NAME",
                "last_name" => "LAST_NAME",
                "middle_name" => "MIDDLE_NAME",
                "suffix" => "SUFFIX",
                "prefix" => "PREFIX"
            ],
            "org" => [
                "company" => "COMPANY",
                "department" => "DEPARTMENT",
                "title" => "TITLE"
            ],
            "phones" => [
                [
                    "phone" => "PHONE_NUMBER",
                    "type" => "HOME"
                ],
                [
                    "phone" => "PHONE_NUMBER",
                    "type" => "WORK",
                    "wa_id" => "PHONE_OR_WA_ID"
                ]
            ],
            "urls" => [
                [
                    "url" => "URL",
                    "type" => "WORK"
                ],
                // Additional urls
            ]
        ]
    ];

$response = $whatsappSender->sendContactsMessage($phone, $contacts);

模板消息

$phone = '1234567890'; // Phone number in E.164 format
$templateName = 'sample_shipping_confirmation';
$languageCode = 'en_US';
$text = '7 to 15';
$response = $whatsappSender->sendTextTemplateMessage($phone, $templateName, $languageCode, $text);

上传媒体

$filePath = public_path('images/demo.jpg');
$mimeType = 'image/jpeg';
$response = $whatsappSender->uploadMedia($filePath, $mimeType);

更新日志

请参阅 更新日志 了解最近更改了哪些内容。

贡献

请参阅 贡献 了解详细信息。

安全性

如果您发现任何与安全相关的问题,请通过电子邮件 hello@dogfromthemoon.com 联系我们,而不是使用问题跟踪器。

鸣谢

许可

MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。

Laravel 包模板

此软件包是使用 Laravel 包模板 生成的。