maina-david/whatsapp-sdk

将Facebook Graph API集成到WhatsApp

1.1 2023-06-19 10:27 UTC

This package is auto-updated.

Last update: 2024-09-19 15:04:10 UTC


README

Latest Stable Version

WhatsApp

此SDK为用PHP编写的应用程序提供了方便的访问WhatsApp API的方式。

文档

请参阅此处WhatsApp Business Management API文档

安装

您可以通过composer安装PHP SDK或下载源代码。

通过Composer

推荐使用Composer安装SDK。

composer require maina-david/whatsapp-sdk

可选:服务提供程序将自动注册。或者,您也可以在config/app.php文件中手动添加服务提供程序。

'providers' => [
    // ...
    MainaDavid\WhatsAppSDK\WhatsAppServiceProvider::class,
];

您应该使用以下命令发布config/whatsapp.php配置文件:

php artisan vendor:publish --provider="MainaDavid\WhatsAppSDK\WhatsAppServiceProvider"

在config/whatsapp.php中设置:电话号码ID、永久访问令牌以及您想要使用的Graph API版本(默认为V15.0),这些信息可以从开发者仪表板获取。

用法

您需要在Facebook开发者门户中设置一个电话号码和一个永久访问令牌。

了解如何创建永久令牌

实例化WhatsApp类

您可以使用或不使用参数实例化WhatsApp类。如果没有提供参数,它将尝试从whatsapp配置文件中检索值。或者,您可以在实例化类时直接传递必要的参数。

use MainaDavid\WhatsAppSDK\WhatsApp;

public function sendMessage()
{
    try {
        // Instantiate the Whatsapp class with the necessary parameters
        $whatsapp = new WhatsApp($phoneNumberId, $accessToken);

        // Prepare the options for sending a text message
        $options = [
            'to' => '+123456789', // Replace with the recipient's phone number
            'message' => 'Hello, world!' // Replace with the message content
        ];

        // Call the sendTextMessage() method of the Whatsapp class to send the message
        $response = $whatsapp->sendTextMessage($options);

        // Handle the response or perform other actions
        if ($response['status'] === 'success') {
            // Message sent successfully
            return response()->json(['message' => 'Message sent']);
        } else {
            // Failed to send message
            return response()->json(['message' => 'Failed to send message']);
        }
    } catch (Exception $e) {
        // Handle any exceptions that occur during initialization or API calls
        return response()->json(['message' => $e->getMessage()], 500);
    }
}

在上面的示例中,使用$phoneNumberId$accessToken参数实例化了WhatsApp类。您可以直接提供这些参数,或者在未指定的情况下让类从配置文件中检索它们。

请记住将+123456789替换为实际收件人的电话号码,将'Hello, world!'替换为您希望的消息内容。

请确保根据您的项目结构调整命名空间和类名(WhatsApp)。此外,修改sendMessage()方法内的代码以匹配您的特定用例。

文本消息

  • sendTextMessage(array $options):发送文本消息

    • message:消息内容。必需
    • to:电话号码。必需
  • sendReplyToTextMessage(array $options):回复消息

    • message_id:先前消息的WhatsApp消息ID。必需
    • message:消息内容。必需
    • to:电话号码。必需
  • sendMediaMessageByURL(array $options):通过URL发送媒体消息

    • type:媒体类型。仅'图像'、'文档'、'音频'、'贴纸'、'视频'。必需
    • url:要发送的媒体的URL。必需
    • to:电话号码。必需
    • caption:描述指定的图像或视频。不要与音频、贴纸或文档媒体一起使用。可选
    • filename:描述特定文档的文件名。仅用于文档媒体。可选
  • sendMediaMessageByID(array $options):通过媒体ID发送媒体消息

    • type:媒体类型。仅'图像'、'文档'、'音频'、'贴纸'、'视频'。必需
    • media_id:来自WhatsApp的媒体ID。必需
    • to:电话号码。必需
    • caption:描述指定的图像或视频。不要与音频、贴纸或文档媒体一起使用。可选
    • filename:描述特定文档的文件名。仅用于文档媒体。可选
  • sendReplyToMediaMessageByURL(array $options):通过URL回复媒体消息

    • message_id:先前消息的WhatsApp消息ID。必需
    • url:要发送的媒体的URL。必需
    • to:电话号码。必需
  • sendReplyToMediaMessageByID(array $options):通过ID回复媒体消息

    • message_id:先前消息的WhatsApp消息ID。必需
    • media_id:来自WhatsApp的媒体ID。必需
    • to:电话号码。必需
  • sendLocationMessage(array $options):向收件人发送位置消息

    • to:电话号码。必需
    • latitude:位置的经度。必需
    • longitude:位置的纬度。必需
    • name:位置名称。可选
    • address:位置的地址。只有当名称存在时,此字段才会显示。可选
  • sendReplyToLocationMessage(array $options):向收件人发送位置消息

    • message_id:先前消息的WhatsApp消息ID。必需
    • to:电话号码。必需
    • latitude:位置的经度。必需
    • longitude:位置的纬度。必需
    • name:位置名称。可选
    • address:位置的地址。只有当名称存在时,此字段才会显示。可选
  • markMessageAsRead(array $options):将消息标记为已读

    • message_id:先前消息的WhatsApp消息ID。必需

SDK测试

SDK使用PHPUnit作为测试运行器。

要运行可用的测试,请在项目的根目录下运行

# Run tests
phpunit

问题

如果您发现一个错误,请向GitHub上的问题跟踪器提交问题。