Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
add: new_user hubspot contact creation
Ben Fornefeld9732573b6a9a
diff --git a/supabase/functions/new_user/index.ts b/supabase/functions/new_user/index.tsindex f88d10876..93afe09a4 100644--- a/supabase/functions/new_user/index.ts+++ b/supabase/functions/new_user/index.ts@@ -3,16 +3,16 @@ import { serve } from 'https://deno.land/[email protected]/http/server.ts'// Created via https://slack.com/apps/new/A0F7XDUAZ-incoming-webhooksconst SLACK_WEBHOOK_URL = Deno.env.get('SLACK_USER_SIGNUP_WEBHOOK_URL')if (!SLACK_WEBHOOK_URL) {- throw new Error('Missing SLACK_NEW_USER_FEEDBACK_APP_OAUTH_TOKEN environment variable')+ throw new Error(+ 'Missing SLACK_NEW_USER_FEEDBACK_APP_OAUTH_TOKEN environment variable'+ )}const SLACK_CHANNEL = 'user-signups'--function sendSlackMessage(email: string) {const message = `:fire: *New User Signed Up *\n*User*\n${email}\n`- return fetch(SLACK_WEBHOOK_URL,{+ return fetch(SLACK_WEBHOOK_URL, {method: 'POST',headers: {'Content-Type': 'application/json',@@ -25,18 +25,61 @@ function sendSlackMessage(email: string) {}function sendToLoops(body: any) {- return fetch(- 'https://app.loops.so/api/v1/contacts/create',- {- method: 'POST',- headers: {- 'Content-Type': 'application/json',- 'Accept': 'application/json',- 'Authorization': `Bearer ${Deno.env.get('API_KEY_LOOPS')}`,- },- body: JSON.stringify(body),+ return fetch('https://app.loops.so/api/v1/contacts/create', {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ Accept: 'application/json',+ Authorization: `Bearer ${Deno.env.get('API_KEY_LOOPS')}`,+ },+ body: JSON.stringify(body),+ })+}++function createHubspotContact(userData: {+ id: string+ email: string+ raw_user_meta_data?: {+ full_name?: string+ name?: string+ }+}) {+ const name =+ userData.raw_user_meta_data?.full_name || userData.raw_user_meta_data?.name++ // Fix for names without spaces - firstName should be the entire name, lastName should be undefined+ let firstName: string | undefined+ let lastName: string | undefined++ if (name) {+ const nameParts = name.split(' ')+ if (nameParts.length > 1) {+ firstName = nameParts[0]+ lastName = nameParts[1]+ } else {+ firstName = name // Use the entire name as firstName if no space is found}- )+ }++ // Map Supabase user data to HubSpot properties+ const hubspotContactData = {+ properties: {+ db_user_id: userData.id,+ email: userData.email,+ firstname: firstName,+ lastname: lastName,+ },+ }++ return fetch('https://api.hubapi.com/crm/v3/objects/contacts', {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ Accept: 'application/json',+ Authorization: `Bearer ${Deno.env.get('ACCESS_TOKEN_HUBSPOT')}`,+ },+ body: JSON.stringify(hubspotContactData),+ })}serve(async (req) => {@@ -44,19 +87,20 @@ serve(async (req) => {* Creates a new contact in Loops.so on a new created user in users table.*/const data = await req.json()- const userEmail = data.record.email+ const userRecord = data.record+ const userEmail = userRecord.emailconsole.log(`New user with email: ${userEmail} created`)const errors: {- service: 'Loops' | 'Slack',- message: string,+ service: 'Loops' | 'Slack' | 'HubSpot'+ message: string}[] = []// Send to Loopsconst loopsResponse = await sendToLoops({email: userEmail,- userGroup: 'Authenticated'+ userGroup: 'Authenticated',})if (loopsResponse.ok) {console.log('Successfully sent email to Loops')@@ -72,8 +116,7 @@ serve(async (req) => {try {await sendSlackMessage(userEmail)console.log('Successfully sent Slack message')- }- catch (error) {+ } catch (error) {console.error(`Failed to send Slack message: ${error}`)errors.push({service: 'Slack',@@ -81,6 +124,26 @@ serve(async (req) => {})}+ // Create HubSpot contact+ try {+ const hubspotResponse = await createHubspotContact(userRecord)+ if (hubspotResponse.ok) {+ console.log('Successfully created HubSpot contact')+ } else {+ const errMessage = await hubspotResponse.text()+ errors.push({+ service: 'HubSpot',+ message: errMessage,+ })+ }+ } catch (error) {+ console.error(`Failed to create HubSpot contact: ${error}`)+ errors.push({+ service: 'HubSpot',+ message: error.message,+ })+ }+if (errors.length == 0) {return new Response(undefined, { status: 200 })} else {