Last active
July 27, 2020 23:04
-
-
Save isaidspaghetti/9b6ab4ab1d38850853ab05e1d04975c6 to your computer and use it in GitHub Desktop.
Index webhook
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
//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