PHP code example of reshapify / sendseven

1. Go to this page and download the library: Download reshapify/sendseven library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reshapify / sendseven example snippets


use Reshapify\SendSeven\SendSeven;

$sendseven = SendSeven::client(getenv('SENDSEVEN_API_TOKEN'));

$message = $sendseven->messages()->send(
    to: '+4915112345678',
    channelId: 'ch_whatsapp',
    text: 'Your order has shipped.',
);

echo $message->id;

$contact = $sendseven->contacts()->findOrCreateByPhone('+18765550123');

$sendseven->messages()->sendButtons('ch_whatsapp', $contact->id, 'Is the shelf stocked?', [
    'yes' => 'Yes',
    'no' => 'No',
]);

// One page
$page = $sendseven->contacts()->list(search: 'acme', pageSize: 50);

foreach ($page as $contact) {
    echo $contact->name;
}

// Every contact, fetching pages only as they're reached
foreach ($sendseven->contacts()->list()->lazy() as $contact) {
    // ...
}

use Reshapify\SendSeven\Webhooks\Webhook;
use Reshapify\SendSeven\Webhooks\Events\MessageReceived;
use Reshapify\SendSeven\Webhooks\Events\MessageStatusUpdated;

$body = file_get_contents('php://input');   // the raw body, exactly as received
$headers = getallheaders();

// SendSeven activates an endpoint only after this unsigned challenge is echoed.
if (Webhook::isVerificationChallenge($body, $headers)) {
    header('Content-Type: application/json');
    echo json_encode(Webhook::challengeResponse($body));
    return;
}

$event = Webhook::constructEvent($body, $headers, $secret);   // throws InvalidSignature

if ($event instanceof MessageReceived) {
    $from = $event->message->fromId;              // phone, or the channel's ID for the person
    $text = $event->message->text;
    $button = $event->message->buttonReply()?->id; // a tapped button, however it arrived
}

if ($event instanceof MessageStatusUpdated && $event->failed()) {
    $why = $event->message->errorCode();          // e.g. "131047": outside WhatsApp's 24 hours
}

use Reshapify\SendSeven\Enums\ChannelType;
use Reshapify\SendSeven\Onboarding\ConnectLink;
use Reshapify\SendSeven\Onboarding\WhatsAppMode;

$link = $sendseven->connectLinks()->create(
    ConnectLink::for(ChannelType::WhatsApp)
        ->modes(whatsapp: [WhatsAppMode::Classic])
        ->brandedAs('Acme')
        ->redirectTo('https://app.acme.test/channels/connected')
        ->singleUse()
        ->expiresIn(hours: 48),
);

header('Location: '.$link->connectUrl);

$sendseven->forTenant($customerTenantId)->contacts()->list();

$capabilities = $sendseven->capabilities();

if (! $capabilities->canCreateTenants()) {
    echo $capabilities->whyNotCreateTenants();
}

$sendseven = SendSeven::factory()
    ->withToken($token)
    ->withRetries(5)
    ->withRateLimiter($sharedLimiter)
    ->withHttpClient($psr18Client)
    ->make();

$fake = SendSeven::fake([
    'POST /messages' => ['id' => 'msg_1', 'direction' => 'outbound', 'message_type' => 'text', /* ... */],
]);

$service = new OrderNotifier($fake->client);
$service->shipped($order);

$fake->assertSent('POST /messages', fn ($request) => $request->body['to'] === '+4915112345678');