PHP code example of renzojohnson / whatsapp-webhook
1. Go to this page and download the library: Download renzojohnson/whatsapp-webhook library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
renzojohnson / whatsapp-webhook example snippets
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);
$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()}";
}
});
$notifications = $handler->parse($rawBody);
foreach ($notifications as $notification) {
if ($notification instanceof MessageNotification) {
echo $notification->getSummary();
}
}