sevenspan/laravel-whatsapp

它使用 WhatsApp 云 API 发送 WhatsApp 消息。

v1.3.1 2023-12-14 10:36 UTC

README

它使用 WhatsApp 云 API 发送模板 WhatsApp 消息。

安装

您可以通过 composer 安装此包

composer require sevenspan/laravel-whatsapp

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="SevenSpan\WhatsApp\Providers\WhatsAppServiceProvider" --tag="config"

这是发布配置文件的内容

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | Whatsapp API URI
    |--------------------------------------------------------------------------
    |
    | The Whatsapp Message API URI.
    |
    */

    'api_uri' => env('WHATSAPP_API_URI', 'https://graph.facebook.com/v14.0/'),

    /*
    |--------------------------------------------------------------------------
    | WHATSAPP BUSINESS ACCOUNT ID
    |--------------------------------------------------------------------------
    |
    | The Whatsapp business account id (waba_id).
    |
    */

    'whatsapp_business_account_id' => env('WHATSAPP_BUSINESS_ACCOUNT_ID', ''),

    /*
    |--------------------------------------------------------------------------
    | ACCESS TOKEN
    |--------------------------------------------------------------------------
    |
    | The Whatsapp business account user access token.
    |
    */
    'access_token' => env('ACCESS_TOKEN', ''),

    /*
    |--------------------------------------------------------------------------
    | SEPARATOR
    |--------------------------------------------------------------------------
    |
    | The Whatsapp separator is used for the separat your string message.
    | For example: $message = 'Hello,123'; then set your .env file variable (SEPARATO=,).
    */
    'separator' => env('SEPARATOR', '~'),

    /*
    |--------------------------------------------------------------------------
    | FROM PHONE NUMBER ID
    |--------------------------------------------------------------------------
    |
    | The Whatsapp register phome number Id.
    | You have to get from phone number id using getPhoneNumbers() function.
    */
    'from_phone_number_id' => env('FROM_PHONE_NUMBER_ID', ''),
];

用法

要创建访问令牌,请点击此链接: 点击这里

use WhatsApp;

$whatsAppBussnessAccountId = "111111111111111"; // Your bussness account id (waba_id)(Required)
$accessToken = ""; // Access Token of the user (Required)

$response = WhatsApp::getPhoneNumbers(string $WhatsAppBusinessAccountId = '', string $accessToken = '')

#You will get output like this:
array:2 [
  "data" => array:1 [
    0 => array:5 [
      "verified_name" => "Test"
      "code_verification_status" => "NOT_VERIFIED"
      "display_phone_number" => "+1 123-123-1234"
      "quality_rating" => "GREEN"
      "id" => "123456789012345"
    ]
  ]
  "paging" => array:1 [
    "cursors" => array:2 [
      "before" => "QVFIUlpVY3FxaW5ZAncElreTlWM0VqU0xIcUZADYTNac2ppUXpFeDEzbmNPMXVfYlZABSVJVaTZAmM0FDWWVYeEFlUW9BTHlSZAFBYbGUyZAVhXZAVBReDF1OENn"
      "after" => "QVFIUlpVY3FxaW5PZAncElreTlWM0VqU0xIcUZADYTNac2ppUXpFeDEzbmNPMXVfYlZABSVJVaTZAmM0FDWWVYeEFlUW9BTHlSZAFBYbGUyZAVhXZAVBReDF1OENn"
    ]
  ]
]

注意:如果您想使用多个 WhatsApp 商户账户以更好地使用,请在 getPhoneNumbers 函数中定义您的号码,否则请在环境文件中设置您的号码

$to = "9111111111"; // Phone number with country code where we want to send message(Required).
$templateName = "hello_world"; // Template name of your template (Required).
$languageCode = "en_us"; // Template language code of your template (Required).
$fromPhoneNumberId = '123456789012345' // You have to get from phone number id using getPhoneNumbers() function.

#To send the message; use the separator character in the message with respective argument. Below is an example of how to send the text message.
$message = 'test~message';  //if message is dyamic you have to passing a parameter order vice.

The customized message should be passed to the components variable along with the object.  You can refer this link for the create components [Click here](https://developers.facebook.com/docs/whatsapp/cloud-api/guides/send-message-templates)
$components  = [
        [
            'type' => 'header',
            'parameters' => [
                [
                    'type' => 'image',
                    'image' => [
                    'link' => 'http(s)://URL',
                    ]
                ]
            ]
        ],
        [
            'type' => 'body',
            'parameters' =>[
                [
                    'type' => 'text',
                    'text' => 'TEXT_STRING',
                ],
                [
                    'type' => 'currency',
                    'currency' => [
                    'fallback_value' => 'VALUE',
                    'code' => 'USD',
                    'amount_1000' => 100,
                    ]
                ],
                [
                    'type' => 'date_time',
                    'date_time' => [
                    'fallback_value' => 'MONTH DAY, YEAR',
                    ]
                ],
            ],
        ],
        [
            'type' => 'button',
            'sub_type' => 'quick_reply',
            'index' => '0',
            'parameters' => [
                [
                    'type' => 'payload',
                    'payload' => 'PAYLOAD',
                ],
            ],
        ]
      ]

注意:该包一次只期望一个参数(要么是 $message,要么是 $components)。

$response = WhatsApp::sendTemplateMessage(string $to, string $templateName, string $languageCode, string $accessToken = '', string $fromPhoneNumberId = '', array $components = [], string $messages = '');

# A successful response includes an object with an identifier prefixed with wamid.
array:3 [
  "messaging_product" => "whatsapp"
  "contacts" => array:1 [
    0 => array:2 [
      "input" => "911234567890"
      "wa_id" => "911234567890"
    ]
  ]
  "messages" => array:1 [
    0 => array:1 [
      "id" => "wamid.HBgMOTE3ODc4OTE4MDXSDQIAERgSOUIwQzlGREQ5NUZBQjQ1OTkzAA=="
    ]
  ]
]

示例

例如,您的模板如下所示

The OTP to login into app is: {{1}}
Regards{{2}}
Thank you!

您必须按如下方式传递 $message 参数:$message = "1234~Nikunj"

上述模板的输出

The OTP to login into app is: 1234
Regards Nikunj
Thank you!

注意:在发送模板 WhatsApp 消息时,您应按模板动态值的顺序发送数据。

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

致谢

许可证

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