joemunapo/whatsapp-php 问题修复 & 功能扩展

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

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

joemunapo/whatsapp-php

最新稳定版本:v1.0.2

Composer 安装命令:

composer require joemunapo/whatsapp-php

包简介

This package provides a seamless integration of the WhatsApp Cloud API for Laravel applications. It offers a flexible. Ideal for multi-tenant applications or businesses managing multiple WhatsApp accounts.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a simple and efficient way to integrate WhatsApp Cloud API functionality into your Laravel application.

Installation

You can install the package via composer:

composer require joemunapo/whatsapp-php

Configuration

After installation, publish the configuration file:

php artisan vendor:publish --provider="Joemunapo\Whatsapp\WhatsappServiceProvider"

Update the published config file in config/whatsapp.php:

return [
    'account_model' => \App\Models\Business::class,
    'fields' => [
        'number_id' => 'number_id',
        'token' => 'whatsapp_token',
        'catalog_id' => 'catalog_id',
    ],
];

Ensure your database model (e.g., Business) has the necessary fields to store WhatsApp account details.

Usage

Initializing the WhatsApp instance

use Joemunapo\Whatsapp\Whatsapp;

$whatsapp = Whatsapp::useNumberId('your_whatsapp_number_id');

Sending a Text Message

$to = '1234567890';
$content = (object) [
    'type' => 'text',
    'text' => [
        'body' => 'Hello, World!'
    ]
];

$messageId = $whatsapp->sendMessage($to, $content);

Sending an Interactive Message (Buttons)

$content = (object) [
    'type' => 'interactive',
    'text' => 'Please choose an option:',
    'buttons' => ['Option 1', 'Option 2', 'Option 3']
];

$whatsapp->sendMessage($to, $content);

Sending Media

$mediaType = 'image';
$mediaUrl = 'https://example.com/image.jpg';
$caption = 'Check out this image!';

$whatsapp->sendMedia($to, $mediaType, $mediaUrl, $caption);

Sending a Template Message

$templateName = 'hello_world';
$languageCode = 'en_US';
$components = [
    [
        'type' => 'body',
        'parameters' => [
            ['type' => 'text', 'text' => 'John Doe']
        ]
    ]
];

$whatsapp->sendTemplate($to, $templateName, $languageCode, $components);

Handling Webhooks

$payload = // ... webhook payload from WhatsApp
$message = Whatsapp::handleWebhook($payload);

if ($message) {
    // Process the message
    $message->reply('Thank you for your message!');
}

Getting Media

When you receive a message with media (like an image, video, or document), you can retrieve the media content using the getMedia method:

$mediaId = 'media_id_from_webhook_payload';
$mediaInfo = $whatsapp->getMedia($mediaId);

// The $mediaInfo will contain details about the media, including the URL to download it
$mediaUrl = $mediaInfo['url'];

// You can then download and process the media as needed

Features

  • Send text messages
  • Send interactive messages (buttons, lists, product lists)
  • Send media (images, videos, documents)
  • Send template messages
  • Handle incoming messages via webhooks
  • Mark messages as read
  • Retrieve media content

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-26