PHP code example of hermesihq / hermesi

1. Go to this page and download the library: Download hermesihq/hermesi 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/ */

    

hermesihq / hermesi example snippets


use Hermesi\Hermesi;

$hermesi = new Hermesi(
    apiKey: getenv('HERMESI_SECRET_KEY'), // hm_sk_..., your SECRET key, server side only
    baseUrl: 'https://your-hermesi-host',
);

$result = $hermesi->events->trigger(
    'order.shipped',                       // <noun>.<past-tense-verb>
    'user_8821',                           // a subscriber's external id
    ['order_id' => '4821', 'tracking_url' => $url],
    idempotencyKey: "order-{$order->id}-shipped", // see below
);
echo $result->eventId, ' ', $result->status; // evt_..., accepted

use Hermesi\Subscriber;

$hermesi->events->trigger('order.shipped', new Subscriber('cust_331', email: '[email protected]', locale: 'fr'), ['order_id' => '4821']);

$run = $hermesi->events->get($result->eventId);

$run->status; // 'processed', 'no_workflow' (nothing matched) or 'invalid' (a strict payload schema refused it)
foreach ($run->notifications as $notification) { // one per recipient
    $notification->externalId; $notification->workflow; $notification->status;
    foreach ($notification->messages as $message) {
        $message->channel; $message->status; $message->provider; $message->failureCode;
        $message->isFinal; // true once nothing more will happen to it
    }
}
$run->messages(); // every message of every notification, flattened

$hermesi->subscribers->put('user_8821', [
    'email' => '[email protected]',
    'phone_e164' => '+237690000000',
    'first_name' => 'Amina',
    'locale' => 'fr',
    'timezone' => 'Africa/Douala',
    'data' => ['plan' => 'pro'],
]);
$hermesi->subscribers->put('user_8821', ['locale' => 'en']);        // only the locale changes: the rest is left alone
$hermesi->subscribers->put('user_8821', ['phone_e164' => null]);    // null clears one field
$profile = $hermesi->subscribers->get('user_8821');                 // profile, channel identities, stored preference overrides
$hermesi->subscribers->patch('user_8821', ['locale' => 'fr']);      // like put, but NotFoundException if the subscriber does not exist
$hermesi->subscribers->delete('user_8821');                         // erase the personal data; idempotent

$hermesi->subscribers->registerChannel('user_8821', 'push', $deviceToken, ['platform' => 'android']); // on every app start
$hermesi->subscribers->removeChannel('user_8821', 'push', $deviceToken);

$hermesi->subscribers->updatePreferences('user_8821', global: ['sms' => false], categories: ['marketing' => ['email' => false, 'push' => null]]);
$hermesi->subscribers->preferences('user_8821')->categories; // ['marketing' => ['email' => false]]

$result = $hermesi->subscribers->bulk([
    ['external_id' => 'user_8821', 'email' => '[email protected]', 'phone_e164' => '+237690000000', 'locale' => 'fr'],
    ['external_id' => 'user_8822', 'email' => '[email protected]', 'data' => ['plan' => 'free']],
    ['external_id' => 'user_8823', 'phone_e164' => null],   // null clears, as in put
]);
$result->created; $result->updated;                          // 3, 0
foreach ($result->subscribers as $row) { $row->externalId; $row->status; } // one per row, in order: 'created' or 'updated'

$result = $hermesi->messages->send(
    'sms', 'user_8821', 'otp-code',
    data: ['code' => '480219'], category: 'security', priority: 'critical',
    idempotencyKey: 'otp-user_8821-482',
);
$result->status;   // 'queued', or 'skipped' / 'suppressed' if the recipient's preferences or a suppression refused it
$result->messages; // one per destination: a push to three devices is three messages
$hermesi->messages->get($result->messageId)->isFinal;

$token = $hermesi->tokens->mint('user_8821', environmentId: 'env_01...'); // valid for an hour at most
// hand it to your frontend, which sends it with the public key

$link = $hermesi->subscribers->preferenceLink('user_8821');
$link->url; // a hosted page that needs no login and works for about a year

use Hermesi\Exception\ApiException;
use Hermesi\Exception\ConnectionException;
use Hermesi\Exception\NotFoundException;
use Hermesi\Exception\RateLimitException;

try {
    $hermesi->events->trigger('order.shipped', 'user_8821', ['order_id' => $id]);
} catch (NotFoundException) {
    // that recipient is not a subscriber in this environment
} catch (RateLimitException $e) {
    $e->retryAfter; // seconds the server asked to wait (it was too long to wait out)
} catch (ApiException $e) {
    $e->errorCode; $e->requestId; // branch on errorCode; quote requestId to whoever runs Hermesi
} catch (ConnectionException) {
    // could not reach Hermesi, after the retries
}

use Hermesi\RetryPolicy;

new Hermesi(retry: new RetryPolicy(maxRetries: 3, baseDelay: 0.5, maxDelay: 8.0, maxRetryAfter: 30.0)); // these are the defaults
new Hermesi(retry: new RetryPolicy(maxRetries: 0)); // no retrying

$hermesi = new Hermesi(simulate: true); // no key, no URL, no network
$hermesi->events->trigger('order.shipped', 'user_1', ['order_id' => '4821']);

$hermesi->simulated()[0]->name;    // 'order.shipped'
$hermesi->simulated()[0]->payload; // ['order_id' => '4821'], as it would have been sent