Skip to content

Instantly share code, notes, and snippets.

//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
@isaidspaghetti
isaidspaghetti / index.js
Last active July 27, 2020 22:43
index imports
//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;
@isaidspaghetti
isaidspaghetti / index.js
Last active July 27, 2020 22:44
Index registration
//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);
@isaidspaghetti
isaidspaghetti / index.js
Last active February 9, 2021 20:26
Index createHubspotContact
//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: {
@isaidspaghetti
isaidspaghetti / Index.js4
Created July 27, 2020 03:21
Index client create
//backend/routes/index.js:47
const client = new StreamChat(apiKey, apiSecret);
@isaidspaghetti
isaidspaghetti / Index.js
Last active July 27, 2020 22:55
Index createUsers
//backend/routes/index.js:30
function createUsers(firstName, lastName) {
const customer = {
id: `${firstName}-${lastName}`.toLowerCase(),
name: firstName,
role: 'user',
};
const admin = {
id: 'admin-id',
@isaidspaghetti
isaidspaghetti / Index.js
Last active July 27, 2020 23:26
Index upsertUsers
//backend/routes/index.js:57
await client.upsertUsers([
customer,
admin
]);
@isaidspaghetti
isaidspaghetti / Index.js
Last active July 27, 2020 23:28
Index channel
//backend/routes/index.js:62
const channel = client.channel('messaging', hubspotCustomerId, {
members: [customer.id, admin.id],
});
@isaidspaghetti
isaidspaghetti / Index.js
Last active July 27, 2020 22:57
Index token
//backend/routes/index.js:66
const customerToken = client.createToken(customer.id);
@isaidspaghetti
isaidspaghetti / index.js
Last active July 27, 2020 23:04
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: {