Skip to content

Instantly share code, notes, and snippets.

@isaidspaghetti
Last active July 27, 2020 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save isaidspaghetti/9b6ab4ab1d38850853ab05e1d04975c6 to your computer and use it in GitHub Desktop.
Save isaidspaghetti/9b6ab4ab1d38850853ab05e1d04975c6 to your computer and use it in GitHub Desktop.
Index webhook
//backend/routes/index.js:81
router.post('/webhooks', async (req, res) => {
if (req.body.type === 'message.new') {
try {
const newMessage = req.body.message;
const hubspotContactId = req.body.channel_id;
const customerResponse = await axios
.get(`https://api.hubapi.com/crm/v3/objects/contacts/${hubspotContactId}`, {
params: {
properties: 'chat_transcript',
archived: false,
hapikey: hubspotKey,
}
});
let localTranscript = customerResponse.data.properties.chat_transcript;
if (!localTranscript) { localTranscript = ""; }
let updatedTranscript = `${localTranscript}\n FROM: ${newMessage.user.id}\n SENT AT: ${newMessage.created_at}\n MESSAGE: ${newMessage.text}`;
await axios
.patch(`https://api.hubapi.com/crm/v3/objects/contacts/${hubspotContactId}?hapikey=${hubspotKey}`, {
properties: {
'chat_transcript': updatedTranscript,
}
});
} catch (err) {
res.status(200).end();
}
}
res.status(200).end();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment