Skip to content

Commit

Permalink
Update to use the new webhooks
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriLojda committed Apr 29, 2024
1 parent ca039a0 commit da6ec5b
Show file tree
Hide file tree
Showing 7 changed files with 974 additions and 116 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,15 @@ To run this integration, you'll need a Kontent.ai project + a [Recombee account]

You can include multiple Kontent.ai types and languages the webhook will be looking out for and synchronizing - to do so, just include them as a comma-separated list.

Subsequently, set the Kontent.ai **Delivery API triggers** to watch for _Publish_ and _Unpublish_ of your content items.
![webhook-setup](docs/webhook.jpg)

At the end, this is an example of how your webhook might look like:
Subsequently, set the `Triggers` to `published data`. Then choose `Specific events` and select only `Content item` events. (You can also unselect `Content item metadata changed` events as they won't affect the resulting data in the index.)

![webhook-setup](docs/webhook.png)
The resulting triggers setup should look like this:

Save the webhook and copy the generated **secret** as it would be required as a parameter in the following step.
![webhook-triggers-setup](docs/webhook-triggers.jpg)

Save the webhook and copy the generated **secret** as it will be required as a parameter in the following step.

4. ### Configure your Netlify functions

Expand Down
Binary file added docs/webhook-triggers.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/webhook.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/webhook.png
Binary file not shown.
1,070 changes: 962 additions & 108 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": "MIT",
"dependencies": {
"@kontent-ai/delivery-sdk": "^14.8.0",
"@kontent-ai/webhook-helper": "^2.1.0",
"@kontent-ai/webhook-helper": "^3.0.0",
"@netlify/functions": "^2.6.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
8 changes: 5 additions & 3 deletions src/functions/recombee-sync-webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { KontentConfiguration, RecombeeConfiguration } from "./model/configurati
import KontentClient from "./model/kontent-client";
import RecombeeClient from "./model/recombee-client";

const signatureHeaderName = "x-kontent-ai-signature";

const { RECOMBEE_API_KEY, KONTENT_SECRET } = process.env;

export const handler: Handler = async (event) => {
Expand Down Expand Up @@ -34,13 +36,13 @@ export const handler: Handler = async (event) => {
};

const signitureHelper = new SignatureHelper();
// Consistency check - make sure your netlify environment variable and your webhook secret matches
// Verify that the request is comming from Kontent.ai and not from somewhere else
if (
!event.headers["x-kc-signature"]
!event.headers[signatureHeaderName]
|| !signitureHelper.isValidSignatureFromString(
event.body,
KONTENT_SECRET,
event.headers["x-kc-signature"].toString(),
event.headers[signatureHeaderName].toString(),
)
) {
return { statusCode: 401, body: "Unauthorized" };
Expand Down

0 comments on commit da6ec5b

Please sign in to comment.