-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Michael
committed
Jan 13, 2023
0 parents
commit 2dbff10
Showing
7 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
vendor | ||
mix-manifest.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
## Introduction | ||
The Webhook Addon for Statamic 3 allows you to send new entries to a specified webhook URL. | ||
|
||
## Installation | ||
1. Require the package using `composer require michaelmannucci/webhook` | ||
2. The config file should auto-publish, but if not, run `php artisan vendor:publish --tag="webhook"` | ||
3. In the config/webhook.php file, specify the webhook URL, the fields you want to send, and the collections you want to target. | ||
|
||
> Tip: When testing the webhook, you can use a service like https://webhook.site/ to see the data that is being sent from your addon. This is a great way to verify that the data is being sent correctly and to troubleshoot any issues that may arise. | ||
## Example Use Case | ||
One example use case for the Webhook Addon is to send data from Statamic to another platform, such as Make or Zapier. | ||
|
||
For example, you can use the Webhook Addon to send data from a new entry in Statamic to [Make](https://make.com), where it can be used to automatically post to a Facebook page. This can be done by setting up a trigger in Make that listens for a new entry in Statamic, and then linking it to an action that posts to Facebook. | ||
|
||
Another example is that you can use the Webhook Addon to send data from a new entry in Statamic to [Zapier](https://zapier.com), where it can be used to trigger an action in another app. For example, you can set up a trigger in Zapier that listens for a new entry in Statamic and then use that trigger to automatically create a new entry in a google sheet or to send an email. | ||
|
||
In both cases, the Webhook Addon acts as the bridge between Statamic and the other platform, sending the necessary data for the trigger to be activated. | ||
|
||
## Support | ||
If you have any issues or questions, please open an issue on the [GitHub repository](https://github.com/michaelmannucci/webhook) or contact me directly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"name": "michaelmannucci/webhook", | ||
"description": "An addon for Statamic that sends new entries to a webhook, allowing for integration with other platforms", | ||
"type": "statamic-addon", | ||
"authors": [ | ||
{ | ||
"name": "Michael Mannucci", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"autoload": { | ||
"psr-4": { | ||
"Michaelmannucci\\Webhook\\": "src" | ||
} | ||
}, | ||
"extra": { | ||
"statamic": { | ||
"name": "Webhook", | ||
"description": "Webhook addon that sends new entries to a webhook, allowing for integration with other platforms." | ||
}, | ||
"laravel": { | ||
"providers": [ | ||
"Michaelmannucci\\Webhook\\ServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
return [ | ||
|
||
// Webhook url | ||
'webhook_url' => 'https://webhookurl.com', | ||
|
||
// Which fields to pass | ||
'fields' => [''], | ||
|
||
// Which collections to pass | ||
'collections' => [''] | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Michaelmannucci\Webhook\Events; | ||
|
||
use GuzzleHttp\Client; | ||
use Illuminate\Support\Facades\Log; | ||
use Statamic\Events\EntrySaved; | ||
use Statamic\Events\EntryCreated; | ||
|
||
// This class listens to the EntryCreated event | ||
class Webhook | ||
{ | ||
public function handle(EntryCreated $event) | ||
{ | ||
|
||
// Get the entry object from the event | ||
$entry = $event->entry; | ||
|
||
// Get the collections specified in the config | ||
$collections = config('webhook.collections'); | ||
|
||
// Check if the entry collection is in the collections array specified in the config | ||
if(in_array($entry->collectionHandle(), $collections)){ | ||
|
||
if($entry->published()){ | ||
// Get the fields specified in the config file | ||
$fields = config('webhook.fields'); | ||
|
||
// Create an array to hold the data to send to the webhook | ||
$data = []; | ||
|
||
// Loop through the fields and add the corresponding data from the entry | ||
foreach ($fields as $field) { | ||
if ($field === 'absoluteUrl') { | ||
$data['absoluteUrl'] = $entry->absoluteUrl(); | ||
} else { | ||
$data[$field] = $entry->{$field}; | ||
} | ||
} | ||
|
||
// Create a new Guzzle client | ||
$client = new Client(); | ||
|
||
$webhook_url = config('webhook.webhook_url'); | ||
|
||
// Send a POST request to the webhook with the title and URL of the entry as the request body | ||
$response = $client->post($webhook_url, [ | ||
'json' => $data | ||
]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Michaelmannucci\Webhook; | ||
|
||
use Michaelmannucci\Webhook\Events\Webhook; | ||
use Statamic\Providers\AddonServiceProvider; | ||
use Illuminate\Support\Facades\Event; | ||
use Statamic\Events\EntryCreated; | ||
use Statamic\Statamic; | ||
|
||
class ServiceProvider extends AddonServiceProvider | ||
{ | ||
public function boot() | ||
{ | ||
// Listen for the event of a new entry being created | ||
Event::listen( | ||
EntryCreated::class, | ||
[Webhook::class, 'handle'] | ||
); | ||
|
||
// Merge the config file from 'webhook.php' file in the config folder | ||
$this->mergeConfigFrom(__DIR__.'/../config/webhook.php', 'webhook'); | ||
|
||
// If running in console | ||
if ($this->app->runningInConsole()) { | ||
|
||
// Publish the config file to config path | ||
$this->publishes([ | ||
__DIR__.'/../config/webhook.php' => config_path('webhook.php'), | ||
], 'webhook'); | ||
} | ||
|
||
// Call vendor publish command | ||
Statamic::afterInstalled(function ($command) { | ||
$command->call('vendor:publish', ['--tag' => 'webhook']); | ||
}); | ||
} | ||
} |