renzojohnson/whatsapp-webhook
Composer 安装命令:
composer require renzojohnson/whatsapp-webhook
包简介
Lightweight WhatsApp Cloud API webhook parser. Typed notifications for messages, statuses, and errors. HMAC signature verification. Zero dependencies.
README 文档
README
Lightweight WhatsApp Cloud API webhook parser. Typed notifications for messages, statuses, and errors. HMAC signature verification. Zero dependencies.
Author: Renzo Johnson
Requirements
- PHP 8.4 or higher (uses
readonlyclasses and enums) ext-json- No other extensions or libraries required (zero dependencies)
Installation
composer require renzojohnson/whatsapp-webhook
Quick Start
use RenzoJohnson\WhatsAppWebhook\WebhookHandler; use RenzoJohnson\WhatsAppWebhook\MessageType; use RenzoJohnson\WhatsAppWebhook\Notification\MessageNotification; $handler = new WebhookHandler('your-app-secret'); // Register listeners $handler->onMessage(MessageType::Text, function (MessageNotification $msg): void { echo "Message from {$msg->from}: {$msg->text()}"; }); // Verify signature and handle $rawBody = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_HUB_SIGNATURE_256'] ?? ''; if (!$handler->verifySignature($rawBody, $signature)) { http_response_code(401); exit; } $handler->handle($rawBody); http_response_code(200);
Webhook Endpoint Setup
use RenzoJohnson\WhatsAppWebhook\WebhookHandler; use RenzoJohnson\WhatsAppWebhook\WebhookException; $handler = new WebhookHandler('your-app-secret'); // Step 1: Handle Meta's verification challenge (GET) if ($_SERVER['REQUEST_METHOD'] === 'GET') { $challenge = $handler->handleVerification($_GET, 'your-verify-token'); if ($challenge !== null) { echo $challenge; exit; } http_response_code(403); exit; } // Step 2: Verify signature (POST) $rawBody = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_HUB_SIGNATURE_256'] ?? ''; if (!$handler->verifySignature($rawBody, $signature)) { http_response_code(401); exit; } // Step 3: Parse and handle try { $notifications = $handler->handle($rawBody); http_response_code(200); } catch (WebhookException $e) { http_response_code(400); }
Message Types
| Type | Enum | Description |
|---|---|---|
text |
MessageType::Text |
Plain text message |
image |
MessageType::Image |
Image with optional caption |
video |
MessageType::Video |
Video with optional caption |
audio |
MessageType::Audio |
Audio message |
document |
MessageType::Document |
Document with filename |
sticker |
MessageType::Sticker |
Sticker |
location |
MessageType::Location |
GPS coordinates with name/address |
contacts |
MessageType::Contacts |
Contact cards |
button |
MessageType::Button |
Quick reply button tap |
interactive |
MessageType::Interactive |
Button reply or list selection |
reaction |
MessageType::Reaction |
Emoji reaction |
order |
MessageType::Order |
Product order |
Listen by Message Type
use RenzoJohnson\WhatsAppWebhook\MessageType; use RenzoJohnson\WhatsAppWebhook\Notification\MessageNotification; $handler->onMessage(MessageType::Text, function (MessageNotification $msg): void { echo $msg->text(); }); $handler->onMessage(MessageType::Image, function (MessageNotification $msg): void { echo "Image: {$msg->mediaId()} ({$msg->mediaMimeType()})"; echo "Caption: {$msg->caption()}"; }); $handler->onMessage(MessageType::Location, function (MessageNotification $msg): void { echo "Location: {$msg->latitude()}, {$msg->longitude()}"; echo "Name: {$msg->locationName()}"; }); $handler->onMessage(MessageType::Interactive, function (MessageNotification $msg): void { if ($msg->interactiveType() === 'button_reply') { echo "Button: {$msg->interactiveButtonTitle()} (ID: {$msg->interactiveButtonId()})"; } if ($msg->interactiveType() === 'list_reply') { echo "List: {$msg->interactiveListTitle()}"; } }); $handler->onMessage(MessageType::Reaction, function (MessageNotification $msg): void { echo "Reacted with {$msg->reactionEmoji()} to {$msg->reactionMessageId()}"; });
Listen to All Messages
$handler->onAnyMessage(function (MessageNotification $msg): void { error_log($msg->getSummary()); });
Status Updates
use RenzoJohnson\WhatsAppWebhook\StatusType; use RenzoJohnson\WhatsAppWebhook\Notification\StatusNotification; $handler->onStatus(StatusType::Delivered, function (StatusNotification $status): void { echo "Delivered to {$status->recipientId}"; }); $handler->onStatus(StatusType::Read, function (StatusNotification $status): void { echo "Read by {$status->recipientId}"; }); $handler->onStatus(StatusType::Failed, function (StatusNotification $status): void { if ($status->hasErrors()) { echo "Error {$status->errorCode()}: {$status->errorTitle()}"; } }); $handler->onAnyStatus(function (StatusNotification $status): void { error_log($status->getSummary()); });
Pricing and Conversation Info
$handler->onAnyStatus(function (StatusNotification $status): void { if ($status->isBillable()) { echo "Category: {$status->pricingCategory()}"; echo "Model: {$status->pricingModel()}"; } if ($status->conversationId()) { echo "Conversation: {$status->conversationId()}"; echo "Origin: {$status->conversationOrigin()}"; } });
Forwarded Messages
$handler->onAnyMessage(function (MessageNotification $msg): void { if ($msg->isForwarded()) { echo "This message was forwarded"; } if ($msg->isFrequentlyForwarded()) { echo "This message has been forwarded many times"; } if ($msg->contextMessageId()) { echo "Reply to: {$msg->contextMessageId()}"; } });
Parse Without Dispatching
$notifications = $handler->parse($rawBody); foreach ($notifications as $notification) { if ($notification instanceof MessageNotification) { echo $notification->getSummary(); } }
Error Handling
use RenzoJohnson\WhatsAppWebhook\WebhookException; try { $handler->parse($rawBody); } catch (WebhookException $e) { // Invalid JSON, wrong object type, empty entry }
Related Packages
- whatsapp-cloud-api — Send WhatsApp messages via Cloud API
Links
License
MIT License. Copyright (c) 2026 Renzo Johnson.
renzojohnson/whatsapp-webhook 适用场景与选型建议
renzojohnson/whatsapp-webhook 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 2, 最近一次更新时间为 2026 年 03 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「meta」 「whatsapp」 「webhook」 「whatsapp-business」 「whatsapp-cloud-api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 renzojohnson/whatsapp-webhook 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 renzojohnson/whatsapp-webhook 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 renzojohnson/whatsapp-webhook 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Harvest statistics and meta data from an URL or his source code (seo oriented).
Phalcon PHP Meta tags service
A SilverStripe module to optimise the Meta, crawling, indexing, and sharing of your website content (forked from Cyber-Duck/Silverstripe-SEO)
Help to manage meta data on Laravel Eloquent model
A simple, repeatable field type
WhatsApp Latest CLoud API Wrapper for PHP
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 22
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-03-17