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/.env | |
NODE_ENV=development | |
PORT=8080 | |
STREAM_API_KEY= your Stream API key here | |
STREAM_API_SECRET= your Stream API secret here | |
HUBSPOT_API_KEY=your HubSpot API key here |
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:3 | |
const { StreamChat } = require('stream-chat'); | |
const { default: axios } = require('axios'); | |
const apiKey = process.env.STREAM_API_KEY; | |
const apiSecret = process.env.STREAM_API_SECRET; | |
const hubspotKey = process.env.HUBSPOT_API_KEY; |
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:46 | |
router.post('/registrations', async (req, res) => { | |
try { | |
const firstName = req.body.firstName.replace(/\s/g, '_'); | |
const lastName = req.body.lastName.replace(/\s/g, '_'); | |
const email = req.body.email.toLowerCase(); | |
const hubspotContactId = await createHubspotContact(firstName, lastName, email); | |
const client = new StreamChat(apiKey, apiSecret); |
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:10 | |
async function createHubspotContact(firstName, lastName, email) { | |
let hubspotContact; | |
try { | |
hubspotContact = await axios.get(`https://api.hubapi.com/crm/v3/objects/contacts/${email}/?idProperty=email&hapikey=${hubspotKey}`); | |
} | |
catch { | |
hubspotContact = await axios.post(`https://api.hubapi.com/crm/v3/objects/contacts?hapikey=${hubspotKey}`, | |
{ | |
properties: { |
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:47 | |
const client = new StreamChat(apiKey, apiSecret); |
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:30 | |
function createUsers(firstName, lastName) { | |
const customer = { | |
id: `${firstName}-${lastName}`.toLowerCase(), | |
name: firstName, | |
role: 'user', | |
}; | |
const admin = { | |
id: 'admin-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
//backend/routes/index.js:57 | |
await client.upsertUsers([ | |
customer, | |
admin | |
]); |
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:62 | |
const channel = client.channel('messaging', hubspotCustomerId, { | |
members: [customer.id, admin.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
//backend/routes/index.js:66 | |
const customerToken = client.createToken(customer.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
//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: { |