承接 wasenderapi/wasenderapi-laravel 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

wasenderapi/wasenderapi-laravel

Composer 安装命令:

composer require wasenderapi/wasenderapi-laravel

包简介

Laravel SDK for WasenderAPI with event dispatching and webhook route

README 文档

README

Laravel SDK Author: YonkoSam

A robust Laravel SDK for interacting with the WasenderAPI (https://www.wasenderapi.com). This SDK simplifies sending WhatsApp messages, managing contacts and groups, handling session statuses, and processing incoming webhooks with Laravel-native events and best practices.

Features

  • Modern Laravel Service: Injectable, testable, and supports Facade usage.
  • Message Sending:
    • Helper methods for text, image, video, document, audio, sticker, contact, and location messages.
    • DTOs for type-safe payloads.
    • RetryConfig for automatic rate-limit retries on send-message endpoints.
  • Contact Management: List, get info, get profile picture, block, and unblock contacts.
  • Group Management: List groups, get metadata, manage participants, update settings.
  • Session Management: Create, list, update, delete, connect/disconnect sessions, get QR codes, check status.
  • Webhook Handling: Securely verifies and parses incoming webhooks, dispatches Laravel events for each event type.
  • Error Handling: Custom WasenderApiException with detailed error/response info.
  • Testing: Pest and Testbench support out of the box.

Installation

composer require wasenderapi/wasenderapi-laravel

Configuration

Publish the config file:

php artisan vendor:publish --tag=wasenderapi-config

Set your credentials in .env:

WASENDERAPI_API_KEY=your_api_key_here
WASENDERAPI_PERSONAL_ACCESS_TOKEN=your_personal_token_here # (optional, for session management)
WASENDERAPI_WEBHOOK_SECRET=your_webhook_secret_here # (for webhook verification)

Usage

Direct Instantiation

Another way to use the client is by creating a new instance of the WasenderClient class directly and providing your API key as an argument:

$client = new WasenderClient('your_api_key_here');

This method is particularly powerful and becomes the recommended approach when your application needs to manage multiple sessions. Each session you create in your Wasender dashboard has a unique API key. Direct instantiation allows you to dynamically select and use the specific API key for the session you want to communicate with at runtime. This is essential for applications that send messages from different WhatsApp numbers. This approach is also the standard method for using the library in contexts outside of the Laravel framework or in situations where Laravel's service container is not readily available. To get started, you'll need a WasenderAPI account to obtain the unique API key for each of your sessions.

Dependency Injection (Recommended)

use WasenderApi\WasenderClient;

public function send(WasenderClient $client) {
    $client->sendText('1234567890', 'Hello from Laravel!');
}

Facade Usage

use WasenderApi\Facades\WasenderApi;

WasenderApi::sendText('1234567890', 'Hello via Facade!');

DTO Usage

use WasenderApi\Data\SendTextMessageData;
use WasenderApi\Data\RetryConfig;
use WasenderApi\Facades\WasenderApi;

$dto = new SendTextMessageData('1234567890', 'Hello DTO!');
$retry = new RetryConfig(true, 3); // Enable retry, max 3 times
WasenderApi::sendText($dto, null, [], $retry);

Message Sending

All message types support both direct parameters and DTOs. All return an array response.

  • sendText($to, $text, $options = [], ?RetryConfig $retry = null)
  • sendImage($to, $url, $caption = null, $options = [], ?RetryConfig $retry = null)
  • sendVideo($to, $url, $caption = null, $options = [], ?RetryConfig $retry = null)
  • sendDocument($to, $url, $fileName, $caption = null, $options = [], ?RetryConfig $retry = null)
  • sendAudio($to, $url, $options = [], ?RetryConfig $retry = null)
  • sendSticker($to, $url, $options = [], ?RetryConfig $retry = null)
  • sendContact($to, $contactName, $contactPhone, $options = [], ?RetryConfig $retry = null)
  • sendLocation($to, $latitude, $longitude, $name = null, $address = null, $options = [], ?RetryConfig $retry = null)

RetryConfig: Only applies to send-message endpoints. Retries on HTTP 429 (rate limit) up to maxRetries times, waiting for retry_after seconds if provided.

Contact Management

  • getContacts()
  • getContactInfo($phone)
  • getContactProfilePicture($phone)
  • blockContact($phone)
  • unblockContact($phone)

Group Management

  • getGroups()
  • getGroupMetadata($jid)
  • getGroupParticipants($jid)
  • addGroupParticipants($jid, $participants)
  • removeGroupParticipants($jid, $participants)
  • updateGroupSettings($jid, $settings)

Session Management

  • getAllWhatsAppSessions()
  • createWhatsAppSession($payload)
  • getWhatsAppSessionDetails($sessionId)
  • updateWhatsAppSession($sessionId, $payload)
  • deleteWhatsAppSession($sessionId)
  • connectWhatsAppSession($sessionId, $qrAsImage = false)
  • getWhatsAppSessionQrCode($sessionId)
  • disconnectWhatsAppSession($sessionId)
  • regenerateApiKey($sessionId)
  • getSessionStatus($sessionId)

Webhook Handling & Events

  • The package auto-registers a POST route (default /wasender/webhook).
  • Verifies the signature using the secret in config/env.
  • Dispatches a dedicated Laravel event for each webhook event type (e.g., MessagesUpserted, GroupsUpdated, etc.).
  • Listen for events in your app:
use WasenderApi\Events\MessagesUpserted;

Event::listen(MessagesUpserted::class, function ($event) {
    // $event->payload
});

Error Handling

All API errors throw WasenderApiException.

use WasenderApi\Exceptions\WasenderApiException;

try {
    WasenderApi::sendText('123', 'fail');
} catch (WasenderApiException $e) {
    // $e->getMessage(), $e->getCode(), $e->getResponse()
}

DTO Reference

  • SendTextMessageData($to, $text)
  • SendImageMessageData($to, $imageUrl, $text = null)
  • SendVideoMessageData($to, $videoUrl, $text = null)
  • SendDocumentMessageData($to, $documentUrl, $fileName, $text = null)
  • SendAudioMessageData($to, $audioUrl)
  • SendStickerMessageData($to, $stickerUrl)
  • SendContactMessageData($to, $contactName, $contactPhone)
  • SendLocationMessageData($to, $latitude, $longitude, $name = null, $address = null)
  • RetryConfig($enabled = false, $maxRetries = 0)

Testing

  • Pest is included for expressive, modern tests.
  • Example:
test('sendText via Facade returns success and message', function () {
    Http::fake([
        'https://www.wasenderapi.com/api/send-message' => Http::response(['success' => true, 'message' => 'Message sent'], 200),
    ]);
    $result = WasenderApi::sendText('123', 'hello');
    expect($result['success'])->toBeTrue();
    expect($result['message'])->toBe('Message sent');
});

Advanced

  • Customizing HTTP: Uses Laravel's HTTP client. You can mock or extend as needed.
  • Extending Events: You can add listeners for any event type or extend event classes for custom logic.
  • RetryConfig: Only applies to send-message endpoints. Retries on HTTP 429 (rate limit) up to maxRetries times, waiting for retry_after seconds if provided.

License

MIT

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

wasenderapi/wasenderapi-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.05k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2025 年 05 月 27 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-05-27