PHP code example of binary-cats / laravel-twilio-webhooks

1. Go to this page and download the library: Download binary-cats/laravel-twilio-webhooks 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/ */

    

binary-cats / laravel-twilio-webhooks example snippets




return [

    /*
     * Twilio will sign each webhook using a token: https://twilio.com/user/account.
     */
    'signing_token' => env('TWILIO_WEBHOOK_SECRET'),

    /*
     * You can define the job that should be run when a certain webhook hits your application
     * here. If Twilio event has a dot it will be replaced with an underscore `_`.
     *
     * You can find a list of Twilio webhook types here:
     * https://www.twilio.com/docs/usage/webhooks
     *
     * The package will automatically convert the keys to lowercase
     * Be cognisant of the fact that array keys are **case sensitive** in PHP
     */
    'jobs' => [
        // 'initiated' => \BinaryCats\TwilioWebhooks\Jobs\HandleInitiated::class,
    ],

    /*
     * The classname of the model to be used. The class should equal or extend
     * Spatie\WebhookClient\Models\WebhookCall
     */
    'model' => \Spatie\WebhookClient\Models\WebhookCall::class,

    /*
     * A class processing the job.
     * The class should extend BinaryCats\TwilioWebhooks\ProcessTwilioWebhookJob
     */
    'process_webhook_job' => \BinaryCats\TwilioWebhooks\ProcessTwilioWebhookJob::class,

    /*
     * When disabled, the package will not verify if the signature is valid.
     */
    'verify_signature' => env('TWILIO_SIGNATURE_VERIFY', true),
];

# routes\web.php
Route::twilioWebhooks('webhooks/twilio.com');

protected $except = [
    'webhooks/twilio.com',
];



namespace App\Jobs\TwilioWebhooks;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class HandleInitiated implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /** @var \Spatie\WebhookClient\Models\WebhookCall */
    public $webhookCall;

    public function __construct(WebhookCall $webhookCall)
    {
        $this->webhookCall = $webhookCall;
    }

    public function handle()
    {
        // do your work here

        // you can access the payload of the webhook call with `$this->webhookCall->payload`
    }
}

// config/twilio-webhooks.php

'jobs' => [
    'initiated' => \App\Jobs\TwilioWebhooks\HandleInitiated::class,
],

/**
 * The event listener mappings for the application.
 *
 * @var array
 */
protected $listen = [
    'twilio-webhooks::initiated' => [
        App\Listeners\InitiatedCall:class,
    ],
];



namespace App\Listeners;

use Illuminate\Contracts\Queue\ShouldQueue;
use Spatie\WebhookClient\Models\WebhookCall;

class InitiatedCall implements ShouldQueue
{
    public function handle(WebhookCall $webhookCall)
    {
        // do your work here

        // you can access the payload of the webhook call with `$webhookCall->payload`
    }
}

use Spatie\WebhookClient\Models\WebhookCall;
use BinaryCats\TwilioWebhooks\ProcessTwilioWebhookJob;

$webhook = WebhookCall::find($id);

dispatch(new ProcessTwilioWebhookJob($webhook));

use BinaryCats\TwilioWebhooks\ProcessTwilioWebhookJob;

class MyCustomTwilioWebhookJob extends ProcessTwilioWebhookJob
{
    public function handle()
    {
        // do some custom stuff before handling

        parent::handle();

        // do some custom stuff after handling
    }
}

Route::twilioWebhooks('webhooks/twilio.com/{configKey}');

Route::post('webhooks/twilio.com/{configKey}', 'BinaryCats\TwilioWebhooks\TwilioWebhooksController');

// token for when Twilio posts to webhooks/twilio.com/account
'signing_token_account' => 'whsec_abc',
// secret for when Twilio posts to webhooks/twilio.com/my-alternative-token
'signing_token_my-alternative-secret' => 'whsec_123',
bash
php artisan vendor:publish --provider="BinaryCats\TwilioWebhooks\TwilioWebhooksServiceProvider" --tag="config"
bash
php artisan vendor:publish --provider="Spatie\WebhookClient\WebhookClientServiceProvider" --tag="webhook-client-migrations"
bash
php artisan migrate