-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhooks.nnb
67 lines (67 loc) · 8.41 KB
/
webhooks.nnb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{
"cells": [
{
"language": "markdown",
"source": [
"# Using Webhooks with Cloudinary"
],
"outputs": []
},
{
"language": "markdown",
"source": [
"## Use Sendgrid API to Notify \n\nThe event body will be extracted and emailed to the registered recipient"
],
"outputs": []
},
{
"language": "typescript",
"source": [
"require('dotenv').config();\nconst sgMail = require('@sendgrid/mail');\n\n// exit if not a post\nexports.handler = async function (event, context) {\n if (!event.body || event.httpMethod !== 'POST') {\n return {\n statusCode: 400,\n headers: {\n 'Access-Control-Allow-Origin': '*',\n 'Access-Control-Allow-Headers': 'Content-Type',\n },\n body: JSON.stringify({\n status: 'invalid-method',\n }),\n };\n }\n\n // get data\n const data = JSON.parse(event.body);\n console.log(JSON.stringify(data, null, 2));\n\n sgMail.setApiKey(process.env.SENDGRID_API_KEY);\n\n const msg = {\n to: process.env.TO_RECIPIENTS,\n from: process.env.FROM_VERIFIED_SENDER,\n subject: 'Webhook Notification',\n text: JSON.stringify(data, null, 2),\n };\n\n // send message\n try {\n const response = await sgMail.sendMultiple(msg);\n return {\n statusCode: response[0].statusCode,\n body: JSON.stringify({ message: response[0] }),\n };\n } catch (error) {\n console.error('error', JSON.stringify(error, null, 2));\n return {\n statusCode: error.code,\n body: error.response.body.errors[0].message,\n };\n }\n};"
],
"outputs": []
},
{
"language": "markdown",
"source": [
"## Google Moderation fires Notification when Complete\n\nThis webhook is executed when Google moderation is complete. It calls two other webhooks - one to process the approved queue and one to process the rejected queue."
],
"outputs": []
},
{
"language": "typescript",
"source": [
"require('dotenv').config();\nconst nodeFetch = require('node-fetch');\n\n// use this to call netlify functions\nasync function processQ(fnURL) {\n // return promise\n return await nodeFetch(fnURL, { method: 'POST' });\n}\n\nexports.handler = async function (event, context) {\n // exit if not a post\n if (!event.body || event.httpMethod !== 'POST') {\n return {\n statusCode: 400,\n headers: {\n 'Access-Control-Allow-Origin': '*',\n 'Access-Control-Allow-Headers': 'Content-Type',\n },\n body: JSON.stringify({\n status: 'invalid-method',\n }),\n };\n }\n\n // setup functions to process Google moderation Qs\n const approvedFn = `${process.env.PROD_FN_PATH}webhook_process_approved_queue`;\n const rejectedFn = `${process.env.PROD_FN_PATH}webhook_process_rejected_queue`;\n console.log(approvedFn);\n console.log(rejectedFn);\n\n try {\n const approvedResponse = await processQ(approvedFn);\n console.log(approvedResponse);\n const rejectedResponse = await processQ(rejectedFn);\n console.log(rejectedResponse);\n return {\n statusCode: 200,\n body: JSON.stringify({\n message: 'Google Moderation Q processing complete',\n }),\n };\n } catch (error) {\n console.error('error', JSON.stringify(error, 0, 2));\n\n return {\n statusCode: 500,\n body: JSON.stringify({ error: 'error processing Google Moderation Q' }),\n };\n }\n};"
],
"outputs": []
},
{
"language": "markdown",
"source": [
"### Process Approved Queue"
],
"outputs": []
},
{
"language": "typescript",
"source": [
"require('dotenv').config();\nconst cloudinaryV2 = require('cloudinary').v2;\n\n// get all the approved videos in the Google moderation Q\nasync function getApprovedQ() {\n try {\n const approvedQ = await cloudinary.api.resources_by_moderation(\n 'google_video_moderation',\n 'approved',\n {\n resource_type: 'video',\n }\n );\n\n console.log(JSON.stringify(approvedQ, null, 2));\n // return the promise\n return approvedQ;\n } catch (error) {\n console.log('get approved Q error', JSON.stringify(error, null, 2));\n return { resources: [] };\n }\n}\n\n// make video accessible using access_control with access_type anon\nasync function makeVideoAccessible(video) {\n try {\n const updateResponse = await cloudinary.api.update(video.public_id, {\n resource_type: 'video',\n access_control: [{ access_type: 'anonymous' }],\n invalidate: true,\n });\n console.log('update response', JSON.stringify(updateResponse, null, 2));\n return updateResponse;\n } catch (error) {\n console.log('make accessible error', JSON.stringify(error, null, 2));\n return { video_accessible_error: JSON.stringify(error, null, 2) };\n }\n}\n\nexports.handler = async function (event, context) {\n // exit if not a post\n if (event.httpMethod !== 'POST') {\n return {\n statusCode: 400,\n headers: {\n 'Access-Control-Allow-Origin': '*',\n 'Access-Control-Allow-Headers': 'Content-Type',\n },\n body: JSON.stringify({\n status: 'invalid-method',\n }),\n };\n }\n\n try {\n const approvedQ = await getApprovedQ();\n for (let i = 0; i < approvedQ.resources.length; i++) {\n let video = approvedQ.resources[i];\n let updateResponse = await makeVideoAccessible(video);\n console.log('made accessible', JSON.stringify(updateResponse, null, 2));\n }\n return {\n statusCode: 200,\n body: JSON.stringify({ message: \"approved Queue processed\" }),\n };\n } catch (error) {\n console.error('error', JSON.stringify(error, 0, 2));\n return {\n statusCode: 500,\n body: JSON.stringify({error: \"error processing approved Queue\"}),\n };\n }\n};"
],
"outputs": []
},
{
"language": "markdown",
"source": [
"### Process Rejected Queue"
],
"outputs": []
},
{
"language": "typescript",
"source": [
"// get all the rejected videos in the Google moderation Q\nasync function getRejectedQ() {\n try {\n const rejectedQ = await cloudinaryV2.api.resources_by_moderation(\n 'google_video_moderation',\n 'rejected',\n {\n resource_type: 'video',\n }\n );\n // return the promise\n return rejectedQ;\n } catch (error) {\n // if error log and return empty resources\n console.log('get rejected Q error', JSON.stringify(error, null, 2));\n return {\"resources\": []};\n }\n}\n\n// destroy rejected videos\nasync function destroyVideo(video) {\n console.log('in destroy video', video.public_id);\n try {\n const destroyResponse = cloudinaryV2.uploader.destroy(video.public_id, {\n invalidate: true,\n resource_type: 'video',\n });\n return destroyResponse;\n } catch (error) {\n console.log('destroy error', JSON.stringify(error, null, 2));\n return {\"destroy_error\": JSON.stringify(error,null,2)}\n }\n}\n\nexports.handler = async function (event, context) {\n // exit if not a post\n if (event.httpMethod !== 'POST') {\n return {\n statusCode: 400,\n headers: {\n 'Access-Control-Allow-Origin': '*',\n 'Access-Control-Allow-Headers': 'Content-Type',\n },\n body: JSON.stringify({\n status: 'invalid-method',\n }),\n };\n }\n\n try {\n const rejectedQ = await getRejectedQ();\n console.log('q resources', rejectedQ.resources);\n for (let i = 0; i < rejectedQ.resources.length; i++) {\n let video = rejectedQ.resources[i];\n let destroyResponse = await destroyVideo(video);\n console.log('destroy response', JSON.stringify(destroyResponse, null, 2));\n }\n return {\n statusCode: 200,\n body: JSON.stringify({ message: \"rejected Queue processed\" }),\n };\n } catch (error) {\n console.error('error', JSON.stringify(error, 0, 2));\n\n return {\n statusCode: 500,\n body: JSON.stringify({error: \"error processing rejected Queue\"}),\n };\n }\n};"
],
"outputs": []
}
]
}