定制 emam/whatsapp-manager 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

emam/whatsapp-manager

Composer 安装命令:

composer require emam/whatsapp-manager

包简介

A framework-agnostic PHP package for WhatsApp integration. Supports multiple drivers (Official Meta, Textly, Ultramsg, WaPilot), dynamic configuration, webhooks, and unified messaging API.

README 文档

README

Latest Version on Packagist Total Downloads License Tests

A robust, framework-agnostic PHP package for integrating WhatsApp Business API into your applications. It supports multiple drivers, dynamic configuration, webhooks, and a unified messaging API.

🌟 Features

  • Multi-Driver Support:
    • Official Meta WhatsApp Business API
    • Textly
    • Ultramsg
    • WaPilot
    • 🔌 Custom Drivers: Easily extendable
  • Unified API: Send messages using a consistent interface regardless of the driver.
  • Dynamic Configuration: Load config from arrays, JSON, or environment variables.
  • Webhook Handling: Secure validation (HMAC signature) and processing of incoming messages.
  • Publishing System: Publish config and drivers to your project for customization.
  • Framework Agnostic: Works with Laravel, Symfony, CodeIgniter, or native PHP.
  • Type Safe: Strict types and comprehensive error handling.

📦 Installation

Install via Composer:

composer require emam/whatsapp-manager

⚙️ Configuration

1. Publish Configuration

Run the publishing command to create a configuration file in your project:

php vendor/bin/publish.php config

This creates config/whatsapp.php.

2. Set Environment Variables

Add your credentials to your .env file:

WHATSAPP_DRIVER=official

# Official Meta Driver
WHATSAPP_OFFICIAL_TOKEN=your_access_token
WHATSAPP_OFFICIAL_PHONE_ID=your_phone_number_id
WHATSAPP_OFFICIAL_VERIFY_TOKEN=your_verify_token
WHATSAPP_OFFICIAL_APP_SECRET=your_app_secret

# Textly Driver
WHATSAPP_TEXTLY_TOKEN=your_token

# Ultramsg Driver
WHATSAPP_ULTRAMSG_ID=your_instance_id
WHATSAPP_ULTRAMSG_TOKEN=your_token

# WaPilot Driver
WHATSAPP_WAPILOT_ID=your_instance_id
WHATSAPP_WAPILOT_TOKEN=your_token

🚀 Usage

Sending Messages

use Emam\WhatsappManager\Config\ConfigManager;
use Emam\WhatsappManager\WhatsappManager;
use Emam\WhatsappManager\Messages\WhatsappMessage;

// 1. Load Configuration
$config = new ConfigManager(require 'config/whatsapp.php');

// 2. Create Manager
$manager = new WhatsappManager($config->all());

// 3. Send Text Message
$response = $manager->driver()->send('+1234567890',
    WhatsappMessage::create('Hello from WhatsApp Manager!')
);

// 4. Send Media Message (Image)
$message = WhatsappMessage::create()
    ->image('https://example.com/image.png', 'Check this out!');
$manager->driver()->send('+1234567890', $message);

// 5. Send Location
$message = WhatsappMessage::create()
    ->location(25.2048, 55.2708, 'Burj Khalifa', '1 Sheikh Mohammed bin Rashid Blvd');
$manager->driver()->send('+1234567890', $message);

// 6. Upload Media & Send
$driver = $manager->driver('official');
$upload = $driver->uploadMedia('/path/to/image.jpg', 'image');
$mediaId = $upload['id'];

$message = WhatsappMessage::create()
    ->image($mediaId, 'Uploaded image');
$driver->send('+1234567890', $message);

// 7. Reply to Message
$message = WhatsappMessage::create('This is a reply')
    ->reply('wamid.HBg...');
$manager->driver()->send('+1234567890', $message);

// 8. Send Template Message (Official Driver)
$message = WhatsappMessage::create()
    ->template('hello_world', 'en_US');

$manager->driver('official')->send('+1234567890', $message);

// 9. Send Interactive Button Message
$manager->sendButtonMessage('123456')
    ->to('+1234567890')
    ->withBody('Do you confirm your appointment for tomorrow at 3 PM?')
    ->addButton('confirm', '✅ Confirm')
    ->addButton('reschedule', '🔄 Reschedule')
    ->addButton('cancel', '❌ Cancel')
    ->withFooter('Please select an option')
    ->send();

// 10. Send Interactive List Message
$manager->sendListMessage('123456')
    ->to('+1234567890')
    ->withButtonText('View Products')
    ->withBody('Our featured products:')
    ->withHeader('Digital Catalog')
    ->startSection('Laptops')
        ->addRow('laptop-pro', 'MacBook Pro', '16" - 32GB RAM - 1TB SSD')
        ->addRow('laptop-air', 'MacBook Air', '13" - M2 Chip - 8GB RAM')
    ->endSection()
    ->startSection('Smartphones')
        ->addRow('iphone-15', 'iPhone 15 Pro', '48MP Camera - 5G')
        ->addRow('samsung-s23', 'Samsung S23', '120Hz AMOLED Display')
    ->endSection()
    ->send();

🛠️ Advanced Usage

Template Management

// Get all templates
$templates = $manager->driver('official')->getTemplates(100);

// Create a template
$template = $manager->driver('official')->createTemplate(
    'order_confirmation',
    'UTILITY',
    'en_US',
    [
        ['type' => 'BODY', 'text' => 'Your order {{1}} has been confirmed.']
    ]
);

// Delete a template
$manager->driver('official')->deleteTemplate('order_confirmation');

Template Builder (Fluent API)

use Emam\WhatsappManager\Templates\TemplateBuilder;

$driver = $manager->driver('official');

// Create a complete template with fluent API
$template = TemplateBuilder::create($driver)
    ->utility()
    ->name('order_confirmation')
    ->language('en_US')
    ->header('Order Confirmed', 'text')
    ->body('Your order {{1}} has been confirmed. Delivery: {{2}}')
    ->footer('Thank you for your order!')
    ->quickReplyButton('Track Order')
    ->quickReplyButton('Contact Support')
    ->build();

// Marketing template with image
$template = TemplateBuilder::create($driver)
    ->marketing()
    ->name('summer_sale')
    ->language('en_US')
    ->header('', 'image')  // Image header
    ->body('Summer Sale! Get {{1}}% off. Use code: {{2}}')
    ->footer('Valid until end of month')
    ->urlButton('Shop Now', 'https://example.com/sale')
    ->build();

Handling Webhooks

The package provides a secure WebhookValidator to handle incoming webhooks.

Native PHP Example:

use Emam\WhatsappManager\Webhook\WebhookValidator;

$validator = new WebhookValidator($config->all());

try {
    // Validate request
    $validator->validate(
        $_SERVER['REQUEST_URI'],
        $_GET,
        json_decode(file_get_contents('php://input'), true),
        getallheaders()
    );

    // Process webhook
    $payload = json_decode(file_get_contents('php://input'), true);
    $result = $manager->driver()->handleWebhook($payload);

} catch (WebhookException $e) {
    http_response_code(403);
    echo $e->getMessage();
}

🛠️ Advanced Usage

Custom Drivers

You can easily add your own driver:

use Emam\WhatsappManager\Contracts\Driver;

class MyCustomDriver implements Driver {
    // Implement methods...
}

$manager->extend('custom', function() {
    return new MyCustomDriver();
});

$manager->driver('custom')->send(...);

Publishing Drivers

You can publish driver files to your project to modify them:

php vendor/bin/publish.php driver Official

This creates app/WhatsApp/Drivers/OfficialDriver.php which you can customize.

🧪 Testing

Run the test suite:

composer test

🔌 Adding Custom Drivers

The package provides a complete scaffolding system to easily add new WhatsApp providers:

Quick Start

# Generate a new driver scaffold
php bin/generate-driver.php ProviderName

# This creates:
# - src/Drivers/ProviderNameDriver.php (driver class)
# - tests/Unit/ProviderNameDriverTest.php (test file)
# - Step-by-step integration instructions

What You Get

  • Complete driver template with all required methods
  • Pre-configured tests ready to run
  • Step-by-step instructions for integration
  • Best practices built-in
  • Type-safe implementation

Example: Adding a New Provider

# 1. Generate scaffold
php bin/generate-driver.php Twilio

# 2. Implement provider-specific logic
# Edit: src/Drivers/TwilioDriver.php

# 3. Run tests
php vendor/bin/pest tests/Unit/TwilioDriverTest.php

# 4. Use your driver
$manager->driver('twilio')->send('+1234567890', $message);

Documentation

🤝 Contributing

Contributions are welcome! Please see CONTRIBUTING.md for details.

📄 License

The MIT License (MIT). Please see License File for more information.

emam/whatsapp-manager 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-30