Skip to content

WaAPIapp/waapi-laravel-sdk

Repository files navigation

WaAPI Laravel Package

Latest Version on Packagist Total Downloads

Laravel Package to use with waapi.app.

Installation

You can install the package via composer:

composer require waapi/waapi-laravel-sdk

You can publish the config file with:

php artisan vendor:publish --tag="waapi-config"

This is the contents of the published config file:

return [
    'api_token' => env('WAAPI_API_TOKEN'),
    'instance_id' => env('WAAPI_INSTANCE_ID'),
];

Please adapt your .env with your instance ID and Token which can be obtained from your API Tokens

Replace with your token and your instance ID

WAAPI_API_TOKEN=abcdefghijkl123456789
WAAPI_INSTANCE_ID=123

Usage

$waAPI = new WaAPI\WaAPI();
$waAPI->sendMessage('1222333444@c.us', 'Hello there!');

Webhook Listener

The package provides a webhooks endpoint to handle incoming webhooks. Simply update your instance with the event types you want to listen to and the endpoint.

Make sure your app is running on a publicly availabel domain. Locally the webhooks won't work.

app(WaAPI::class)->updateInstance(
    route('waapi.webhooks'),
    [
        EventType::MESSAGE->value,
        EventType::QR->value
    ]
);

Create an event listener to listen on the webhook events

new Message example:

php artisan make:listener WaAPIMessageListener --event=\\WaAPI\\WaAPI\\Events\\MessageEvent

QR Code change example:

php artisan make:listener WaAPIQrCodeListener --event=\\WaAPI\\WaAPI\\Events\\QrEvent

Register your listener in app/Providers/EventServiceProvider.php if autodiscovery for events is disabled

    use App\Listeners\WaAPIInstanceReadyListener;
    use App\Listeners\WaAPIMessageListener;
    use WaAPI\WaAPI\Events\MessageEvent;
    use WaAPI\WaAPI\Events\QrEvent;
        
    [...]
        
    protected $listen = [
        MessageEvent::class => [
            WaAPIMessageListener::class,
        ],
        QrEvent::class => [
            WaAPIQrCodeListener::class,
        ],
    ];

Example for WaAPIMessageListener:

<?php

namespace App\Listeners;

use WaAPI\WaAPI\Events\MessageEvent;
use WaAPI\WaAPI\WaAPI;

class WaAPIMessageListener
{
    /**
     * Handle the event.
     */
    public function handle(MessageEvent $message): void
    {
        if (!$message->isFromMe()) {
            app(WaAPI::class)->sendMessage($message->getFrom(), 'Hello to you too!');
        }
    }
}

Available events:

AuthenticatedEvent
AuthFailureEvent
DisconnectedEvent
GroupJoinEvent
GroupLeaveEvent
GroupUpdateEvent
InstanceReadyEvent
LoadingScreenEvent
MediaUploadedEvent
MessageAcknowledgedEvent
MessageCreatedEvent
MessageEvent
MessageReactionEvent
MessageRevokedEveryoneEvent
MessageRevokedMeEvent
QrEvent
StateChangeEvent

Testing

composer test

Changelog

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

License

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