missael-anda / laravel-whatsapp
为 Laravel 提供的 Whatsapp Business Cloud API 封装器。
Requires
- php: ^8.1
- illuminate/contracts: ^9.0 || ^10.0 || ^11.0
- illuminate/events: ^9.0 || ^10.0 || ^11.0
- illuminate/http: ^9.0 || ^10.0 || ^11.0
- illuminate/routing: ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^9.0 || ^10.0 || ^11.0
This package is auto-updated.
Last update: 2024-09-26 18:32:28 UTC
README
这是一个用于Whatsapp Business Cloud API的 Laravel 包。
安装
composer require missael-anda/laravel-whatsapp
配置
您需要在 .env 文件中设置 WHATSAPP_TOKEN 和 WHATSAPP_NUMBER_ID 的值。
有关进一步配置的详细信息,请参阅 config/whatsapp.php。您可以通过将其复制到您的本地 config 目录或定义配置文件中使用的环境变量来修改配置。
php artisan vendor:publish --provider="MissaelAnda\Whatsapp\WhatsappServiceProvider" --tag=config
用法
您可以向单个或多个客户发送消息。
use MissaelAnda\Whatsapp\Messages; Whatsapp::send('13333333333', Messages\TemplateMessage::create() ->name('one_time_password') ->language('en_US') ->body(Messages\Components\Body::create([ Messages\Components\Parameters\Text::create('000000'), ])));
您还可以以任何消息类型(除反应消息外)回复对话中的其他消息
use MissaelAnda\Whatsapp\Messages\TextMessage; Whatsapp::send('13333333333', TextMessage::create('Answer to your message')->respondTo('wamid.91n23...'));
并将消息标记为已读
Whatsapp::markRead('wamid.91n23...');
默认情况下,消息将发送到 default_number_id,如果您想使用其他号码,可以使用 Whatsapp::numberId() 或将别名添加到配置的 numbers 列表并使用 Whatsapp::numberName()。您还可以使用 Whatsapp::token() 手动设置令牌,或者您可以使用 Whatsapp::client() 同时设置令牌和 numberId。
支持的消息
- 文本消息
- 媒体消息
- 模板消息
- 反应消息
- 联系人消息
- 交互消息
- 位置消息
媒体
您还可以使用管理媒体。
Whatsapp::uploadMedia(string $file, string $type = null, bool $retrieveAllData = true): \MissaelAnda\Whatsapp\WhatsappMedia|string Whatsapp::getMedia(string $mediaId, bool $download = false): \MissaelAnda\Whatsapp\WhatsappMedia Whatsapp::deleteMedia(\MissaelAnda\Whatsapp\WhatsappMedia|string $id): bool Whatsapp::downloadMedia(string|\MissaelAnda\Whatsapp\WhatsappMedia $media): \MissaelAnda\Whatsapp\WhatsappMedia
企业资料
管理号码企业资料有两种方式
Whatsapp::getProfile(): \MissaelAnda\Whatsapp\BusinessProfile Whatsapp::updateProfile(\MissaelAnda\Whatsapp\BusinessProfile|array $data): bool
Webhooks
Whatsapp 允许您订阅 webhooks 以接收通知,最重要的是接收来自客户的消息。订阅和通知都在默认的路由 whatsapp/webhook 中处理(您可以使用 WHATSAPP_WEBHOOK_PATH 环境变量更改路径)。
当您注册 webhook 时,meta 会发送订阅,这需要一个您设置的验证令牌,您需要使用 WHATSAPP_WEBHOOK_VERIFY_TOKEN 环境设置来设置它。收到订阅意图时,将触发 MissaelAnda\Whatsapp\Events\SubscriptionIntentReceived 事件,如果请求成功,则还会触发 MissaelAnda\Whatsapp\Events\SuccessfullySubscribed 事件。
另一方面,通知将触发一个包含所有有效载荷的 MissaelAnda\Whatsapp\Events\WebhookReceived 事件,如果您想通过验证 sha256 签名来保护此路由,则必须将 WHATSAPP_WEBHOOK_SIGNATURE_VERIFY 设置为 true 并将 WHATSAPP_SECRET 设置为您的 WhatsApp 应用密钥。
如果有效载荷无效,将触发一个包含错误描述异常的 MissaelAnda\Whatsapp\Events\UnprocessableWebhookPayload 事件。
如果您想了解何时触发特定通知,您可以订阅这些事件
MissaelAnda\Whatsapp\Events\WebhookEntry通用条目MissaelAnda\Whatsapp\Events\MessagesReceived用于messages条目
通知通道
此库支持通道通知,只需将 routeNotificationForWhatsapp() 函数添加到 Notifiable 用户(它可以返回单个 whatsapp_id 或一个它们的数组)
class User extends Authenticatable { use Notifiable; /** * @return string|array */ public function routeNotificationForWhatsapp(): string|array { return "{$this->phone_code}{$this->phone}"; } }
现在只需创建一个实现 toWhatsapp() 函数的通知
//... use MissaelAnda\Whatsapp\Messages\TemplateMessage; use MissaelAnda\Whatsapp\Messages\Components\Parameters; use MissaelAnda\Whatsapp\WhatsappChannel; class VerificationCode extends Notification { use Queueable; /** * Create a new notification instance. * * @return void */ public function __construct(protected string $code) { // } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return [WhatsappChannel::class]; } /** * Get the message representation of the notification. * * @param mixed $notifiable * @return \MissaelAnda\Whatsapp\Messages\WhatsappMessage */ public function toWhatsapp($notifiable) { return TemplateMessage::create('one_time_password') ->language('en_US') ->body([ Parameters\Text::create('123456'), ]); } }
现在您可以发送 WhatsApp 通知
$user->notify(new VerificationCode('12345678'));
配置文件
return [ /** * The whatsapp token to be used. */ 'token' => env('WHATSAPP_TOKEN'), /** * The whatsapp's app secret code. Required for webhook request signature verification. */ 'secret' => env('WHATSAPP_SECRET'), /** * The default NUMBER ID used to send the messages. */ 'default_number_id' => env('WHATSAPP_NUMBER_ID'), /** * If you want to use other number id's you can add them here so you can call * `numberName` with the name you provide here and make it easier to change * the phone where the messages are sended. */ 'numbers' => [ // 'fallback' => env('WHATSAPP_FALLBACK_NUMBER_ID'), ], 'webhook' => [ /** * Wether to enable the webhook routes */ 'enabled' => env('WHATSAPP_WEBHOOK_ENABLED', true), /** * The webhook path, by default "/whatsapp/webhook" */ 'path' => env('WHATSAPP_WEBHOOK_PATH', 'whatsapp'), /** * The webhook verification token. * For more information check https://developers.facebook.com/docs/graph-api/webhooks/getting-started#verification-requests */ 'verify_token' => env('WHATSAPP_WEBHOOK_VERIFY_TOKEN'), /** * Wether the webhook request signature should be verified or not. */ 'verify_signature' => env('WHATSAPP_WEBHOOK_SIGNATURE_VERIFY', false), ], ];
缺失的功能
- 注册/注销/验证新的电话号码
许可证
本项目采用 MIT 许可协议 许可。