You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Feature: Process Twilio Programmable Messaging webhooks (#2)
* replace mentions of Mailgun with Twilio
* convert to an array and check for both CallStatus and SmsStatus to allow for Programmable Messaging webhooks
* add Programmable Messaging (Outbound) Webhook Event Types section to readme
* fix webhook validation by removing query parameters in callback url from request data
* update readme to include information about passing metadata in callback url
* update readme to include note about alphabetizing url parameters in statusCallback URLs to avoid verification failures
* readme typo
* restore $key and adjust setter
* create two new tests to confirm CallStatus or SmsStatus
At the time of this writing, the following event types are used by Programmable Messaging Webhooks:
129
+
130
+
-`queued`
131
+
-`canceled`
132
+
-`sent`
133
+
-`failed`
134
+
-`delivered`
135
+
-`undelivered`
136
+
-`read`
137
+
138
+
For the most up-to-date information and additional details, please refer to the official Twilio documentation: [Twilio Programmable Messaging: Outbound Message Status in Status Callbacks](https://www.twilio.com/docs/messaging/guides/outbound-message-status-in-status-callbacks#message-status-changes-triggering-status-callback-requests).
139
+
140
+
126
141
### Handling webhook requests using jobs
127
142
If you want to do something when a specific event type comes in you can define a job that does the work. Here's an example of such a job:
128
143
@@ -158,7 +173,7 @@ class HandleInitiated implements ShouldQueue
158
173
}
159
174
```
160
175
161
-
Spatie highly recommends that you make this job queueable, because this will minimize the response time of the webhook requests. This allows you to handle more Mailgun webhook requests and avoid timeouts.
176
+
Spatie highly recommends that you make this job queueable, because this will minimize the response time of the webhook requests. This allows you to handle more Twilio webhook requests and avoid timeouts.
162
177
163
178
After having created your job you must register it at the `jobs` array in the `twilio-webhooks.php` config file.\
164
179
The key should be the name of twilio event type.\
@@ -214,12 +229,24 @@ class InitiatedCall implements ShouldQueue
214
229
}
215
230
```
216
231
217
-
Spatie highly recommends that you make the event listener queueable, as this will minimize the response time of the webhook requests. This allows you to handle more Mailgun webhook requests and avoid timeouts.
232
+
Spatie highly recommends that you make the event listener queueable, as this will minimize the response time of the webhook requests. This allows you to handle more Twilio webhook requests and avoid timeouts.
218
233
219
234
The above example is only one way to handle events in Laravel. To learn the other options, read [the Laravel documentation on handling events](https://laravel.com/docs/9.x/events).
220
235
221
236
## Advanced usage
222
237
238
+
### Adding Metadata to the Webhook Call
239
+
240
+
You can pass additional metadata with your Twilio webhooks by [adding URL parameters to the `statusCallback` URL](https://www.twilio.com/docs/messaging/guides/outbound-message-logging#sending-additional-message-identifiers). This metadata will be accesible in the payload (i.e. `$this->webhookCall->payload`), allowing you to pass additional context or information that you might need when processing the webhook.
241
+
242
+
To add metadata, simply append your custom key-value pairs as URL parameters to the `statusCallback` URL in your Twilio API request. For example:
In this example, order_id=12345 and user_id=67890 are custom parameters that will be passed back with the webhook payload. Twilio will include these parameters in the webhook request, allowing you to access this information directly in your webhook processing logic.
247
+
248
+
**Note:** When building your `statusCallback` URL, ensure that the query parameter keys are alphabetized. This is necessary to prevent webhook verification failures because the `Request` facade's [`fullUrl()` function](https://laravel.com/api/9.x/Illuminate/Support/Facades/Request.html#method_fullUrl) (i.e., `$request->fullUrl()`) automatically returns the query parameters in alphabetical order.
249
+
223
250
### Retry handling a webhook
224
251
225
252
All incoming webhook requests are written to the database. This is incredibly valuable when something goes wrong while handling a webhook call. You can easily retry processing the webhook call, after you've investigated and fixed the cause of failure, like this:
If this route parameter is present verify middleware will look for the secret using a different config key, by appending the given the parameter value to the default config key. E.g. If Twilio posts to `webhooks/twilio.com/my-named-secret` you'd add a new config named `signing_token_my-named-secret`.
@@ -281,7 +308,7 @@ Example config might look like:
0 commit comments