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
from flask import Flask, request | |
import hashlib | |
import hmac | |
import base64 | |
# Signing secret from webhook itself | |
SIGNING_SECRET = "{your_signing_secret}" | |
# Always sha256 | |
SIGNING_SECRET_ALGORITHM = "sha256" |
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
const test = () => { | |
console.log("Hello, World!") | |
} |
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
const createOrgMembership = async (res, user, org) => { | |
const url = `https://${subdomain}.zendesk.com/api/v2/organization_memberships`; | |
const config = { | |
method: "POST", | |
headers: headers, | |
body: JSON.stringify({ | |
organization_membership: { | |
organization_id: org.id, | |
user_id: user.id | |
} |
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
if (response.ok) { | |
response.json().then(data => { | |
console.log("User created/updated"); | |
createOrgMembership(res, data.user, org); | |
}); | |
} else { | |
res.status(response.status).send({error:`Cannot create/update user: ${response.statusText}`}); | |
} | |
}; |
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
const createOrUpdateUser = async (req, res, org) => { | |
const url = `https://${subdomain}.zendesk.com/api/v2/users/create_or_update`; | |
const config = { | |
method: "POST", | |
headers: headers, | |
body: JSON.stringify({ | |
user: { | |
email: req.body.email, | |
name: req.body.name, | |
verified: true, |
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
if (response.ok) { | |
response.json().then(data => { | |
console.log('Org found by name'); | |
// Provides exact matched org name as search API includes non-specific results | |
const matched_org = data.results.find(({name}) => name === req.body.organization); | |
createOrUpdateUser(req, res, matched_org); | |
}); | |
} |
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
const findOrg = async (req, res) => { | |
const url = `https://${subdomain}.zendesk.com/api/v2/search.json?query=type:organization "${req.body.organization}"`; | |
const config = { | |
method: "GET", | |
headers: headers | |
}; | |
const response = await fetch(url, config); |
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
if (response.ok) { | |
response.json().then(data => { | |
console.log('Org created/updated'); | |
createOrUpdateUser(req, res, data.organization); | |
}); | |
} | |
else if (response.status===422){ | |
// Find the existing org by name if it already exists | |
console.log(`Error creating org...${response.status}: ${response.statusText}...finding existing org by name`); | |
findOrg(req, res); |
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
const createOrUpdateOrg = async (req, res) => { | |
const url = `https://${subdomain}.zendesk.com/api/v2/organizations/create_or_update`; | |
const config = { | |
method: "POST", | |
headers: headers, | |
body: JSON.stringify({organization: {name: req.body.organization}}) | |
}; | |
const response = await fetch(url, config); |
NewerOlder