tizfai/laravel-whatsapp 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

tizfai/laravel-whatsapp

Composer 安装命令:

composer require tizfai/laravel-whatsapp

包简介

WhatsApp Business API integration for Laravel - Send messages, PDFs, and secure prescription links

README 文档

README

WhatsApp Business API (Meta Cloud API) integration for Laravel. Send text messages, PDFs, and generate secure time-limited share links for any model.

Installation

composer require tizfai/laravel-whatsapp

Publish config and run migration:

php artisan vendor:publish --tag=whatsapp-config
php artisan migrate

Configuration

Add to your .env:

WHATSAPP_ACCESS_TOKEN=EAAG...your_system_user_token
WHATSAPP_PHONE_NUMBER_ID=984998221371272
WHATSAPP_BUSINESS_ACCOUNT_ID=1615131750010820
WHATSAPP_WEBHOOK_VERIFY_TOKEN=your_custom_verify_token_here
WHATSAPP_API_VERSION=v21.0
WHATSAPP_LINK_EXPIRY_HOURS=24
WHATSAPP_MAX_ACCESS_COUNT=3

Usage

Send Text Message

use Tizfai\LaravelWhatsApp\Facades\WhatsApp;

$result = WhatsApp::sendText('+8801711111111', 'Hello from DocFai!');

if ($result['success']) {
    echo $result['message_id'];
}

Send PDF Document

$result = WhatsApp::sendDocument(
    phone: '+8801711111111',
    fileUrl: 'https://your-wasabi-url.com/prescription.pdf',
    filename: 'prescription_001.pdf',
    caption: 'Your prescription'
);

Send Text + PDF Together

$result = WhatsApp::sendDocumentWithText(
    toPhone: '+8801711111111',
    fileUrl: 'https://storage.com/prescription.pdf',
    filename: 'prescription.pdf',
    textMessage: '🏥 Your prescription is attached.',
    caption: 'DocFai Prescription'
);

Send Template Message

$result = WhatsApp::sendTemplate(
    toPhone: '+8801711111111',
    templateName: 'prescription_ready',
    parameters: [
        ['type' => 'text', 'text' => 'Kamal'],
        ['type' => 'text', 'text' => 'RX-2026-001'],
    ],
    languageCode: 'en'
);

Secure Share Links

Generate time-limited, access-counted secure links for any Eloquent model.

Create Share Link Only

$share = WhatsApp::createShareLink(
    model: $prescription,
    recipientPhone: '+8801711111111',
    recipientName: 'Kamal Tizfai',
    expiryHours: 24,     // optional, uses config default
    maxAccess: 3,         // optional, uses config default
    metadata: ['shared_by' => auth()->id()]
);

echo $share->getShareUrl(); // https://yourapp.com/wa-view/{token}
echo $share->isValid();     // true/false

Create Share Link AND Send via WhatsApp

$result = WhatsApp::sendShareLink(
    model: $prescription,
    recipientPhone: '+8801711111111',
    recipientName: 'Kamal Tizfai',
    messageBody: "🏥 Your prescription is ready:\n{URL}\n\nValid 24h | Max 3 views",
    expiryHours: 24,
    maxAccess: 3
);

$share = $result['share']; // WhatsAppShare model instance

Access the Share in Your Controller

Add a public route in your app:

// routes/web.php
Route::get('/wa-view/{token}', [YourPrescriptionViewController::class, 'show']);
// YourPrescriptionViewController.php
use Tizfai\LaravelWhatsApp\Models\WhatsAppShare;

public function show(string $token, Request $request)
{
    $share = WhatsAppShare::where('secure_token', $token)->firstOrFail();

    if (!$share->isValid()) {
        abort(403, 'Link has expired or access limit reached');
    }

    $share->recordAccess([
        'ip' => $request->ip(),
        'user_agent' => $request->userAgent(),
    ]);

    $prescription = $share->shareable; // Polymorphic - returns your model
    return view('prescriptions.view', compact('prescription'));
}

Webhook Setup

The package automatically registers these routes:

  • GET /api/webhooks/whatsapp - Meta webhook verification
  • POST /api/webhooks/whatsapp - Delivery status & incoming messages

In Meta Developer Console:

  1. Go to your app → WhatsApp → Configuration
  2. Webhook URL: https://yourdomain.com/api/webhooks/whatsapp
  3. Verify token: same value as WHATSAPP_WEBHOOK_VERIFY_TOKEN
  4. Subscribe to: messages

Artisan Commands

# Clean up expired shares (default: older than 30 days)
php artisan whatsapp:clean-shares

# Preview without deleting
php artisan whatsapp:clean-shares --dry-run

# Custom retention period
php artisan whatsapp:clean-shares --days=7

Schedule in app/Console/Kernel.php:

$schedule->command('whatsapp:clean-shares')->daily();

API Reference

Method Description
sendText($phone, $message, $previewUrl) Send plain text
sendDocument($phone, $url, $filename, $caption) Send PDF/file
sendTemplate($phone, $name, $params, $lang) Send template
sendDocumentWithText(...) Send text + document
sendShareLink($model, $phone, $name, ...) Create share + send
createShareLink($model, $phone, $name, ...) Create share only
validateToken() Test API credentials
formatPhone($phone) Format to E.164 (no +)

License

MIT

tizfai/laravel-whatsapp 适用场景与选型建议

tizfai/laravel-whatsapp 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 19 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「messaging」 「laravel」 「meta」 「whatsapp」 「whatsapp-business-api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 tizfai/laravel-whatsapp 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 tizfai/laravel-whatsapp 我们能提供哪些服务?
定制开发 / 二次开发

基于 tizfai/laravel-whatsapp 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 19
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 20
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-25