ruff0 / whatsapp-api-laravel

帮助您处理whatsapp Api Cloud。

dev-main 2024-09-08 01:57 UTC

This package is auto-updated.

Last update: 2024-09-08 01:57:28 UTC


README

此库是从"netflie/whatsapp-cloud-api"克隆而来,以原生PHP形式开发,用于支持Laravel应用程序。

功能

此包使开发人员能够在PHP代码中轻松访问WhatsApp Cloud API服务。

每月前1,000次会话由WhatsApp Cloud API免费提供。

入门

请按照官方指南的"入门"部分创建并配置您的Facebook WhatsApp应用程序。

最低要求 - 运行SDK需要您的系统具有PHP >= 7.4,并使用带OpenSSL和zlib编译的CURL >=7.19.4。

安装

composer require asim-altayb/whatsapp-api-laravel

Composer Dump到文件

composer dump-autoload

快速示例

发送文本消息

<?php


use AsimAltayb\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('96651234567', 'Hey there! I\'m using WhatsApp Cloud API. Visit https://www.AsimAltayb.es');

发送文档

您可以通过两种方式发送文档:通过将文件上传到WhatsApp Cloud服务器(您将收到一个标识符)或从互联网上发布的文档链接。

<?php

use AsimAltayb\WhatsAppCloudApi\Message\Media\LinkID;
use AsimAltayb\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('96651234567', $media_id, $document_name, $document_caption);

// Or
$document_link = 'https://link.com/image.png';
$link_id = new LinkID($document_link);
$whatsapp_cloud_api->sendDocument('96651234567', $link_id, $document_name, $document_caption);

发送模板消息

<?php

$whatsapp_cloud_api->sendTemplate('96651234567', 'hello_world', 'en_US'); // Language is optional

您也可以构建带参数的模板

<?php

use AsimAltayb\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('96651234567', 'sample_issue_resolution', 'en_US', $components); // Language is optional

发送音频消息

<?php

use AsimAltayb\WhatsAppCloudApi\Message\Media\LinkID;

$audio_link = 'https://link.com/file.ogg';
$link_id = new LinkID($audio_link);
$whatsapp_cloud_api->sendAudio('96651234567', $link_id);

发送图片消息

<?php

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

发送视频消息

<?php

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

发送贴纸消息

贴纸示例:https://github.com/WhatsApp/stickers

<?php

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

发送位置消息

<?php

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

发送联系人消息

<?php

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

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

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

发送列表消息

<?php

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

功能

  • 发送文本消息
  • 发送文档
  • 发送带参数的模板
  • 发送音频
  • 发送图片
  • 发送视频
  • 发送贴纸
  • 发送位置
  • 发送联系人
  • 发送列表

获取帮助

变更日志

请参阅CHANGELOG以获取有关最近更改的更多信息。

测试

composer unit-test

您还可以通过向WhatsApp Clou API发出真实调用来运行测试。请将您的测试凭据放在WhatsAppCloudApiTestConfiguration文件中。

composer integration-test

贡献

请参阅CONTRIBUTING以获取详细信息。

许可

MIT许可(MIT)。请参阅许可文件以获取更多信息。请参阅许可文件以获取更多信息。

免责声明

此包不是由Facebook官方维护的。WhatsApp和Facebook商标和标志是Meta Platforms,Inc.的财产。