Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

send slack message when user signs up

Vasek Mlejnskyca0243ba4da6
diff --git a/supabase/functions/new_user/index.ts b/supabase/functions/new_user/index.ts
index 062d29743..14a3ad40d 100644
--- a/supabase/functions/new_user/index.ts
+++ b/supabase/functions/new_user/index.ts
@@ -1,32 +1,88 @@
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
+import { WebClient } from 'https://deno.land/x/[email protected]/mod.js'
+
+const SLACK_API_TOKEN = Deno.env.get('SLACK_NEW_USER_FEEDBACK_APP_OAUTH_TOKEN')
+if (!SLACK_API_TOKEN) {
+ throw new Error('Missing SLACK_NEW_USER_FEEDBACK_APP_OAUTH_TOKEN environment variable')
+}
+const SLACK_CHANNEL = 'user-signups'
+
+console.log('Will create slack client')
+
+const slackClient = new WebClient(SLACK_API_TOKEN)
+
+console.log('Slack client created')
+
+function sendSlackMessage(email: string) {
+ const message = `:fire: *New User Signed Up *\n*User*\n${email}\n`
+
+ return slackClient.chat.postMessage({
+ channel: SLACK_CHANNEL,
+ text: message,
+ link_names: true,
+ })
+}
+
+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),
+ }
+ )
+}
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
+
+ console.log(`New user with email: ${userEmail} created`)
- const body = {
- email: data.record.email,
+ const errors: {
+ service: 'Loops' | 'Slack',
+ message: string,
+ }[] = []
+
+ // Send to Loops
+ const loopsResponse = await sendToLoops({
+ email: userEmail,
userGroup: 'Authenticated'
+ })
+ if (loopsResponse.ok) {
+ console.log('Successfully sent email to Loops')
+ } else {
+ const errMessage = await loopsResponse.text()
+ errors.push({
+ service: 'Loops',
+ message: errMessage,
+ })
}
- console.log(`New user with email: ${body.email} created`)
- const res = await 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)
- }
- )
- if (res.ok) {
- console.log('Success')
- return new Response(await res.json())
- } else {
- console.log('Failed')
- return new Response(await res.text(), {
- status: 400,
+ // Send Slack message
+ try {
+ await sendSlackMessage(userEmail)
+ console.log('Successfully sent Slack message')
+ }
+ catch (error) {
+ console.error(`Failed to send Slack message: ${error}`)
+ errors.push({
+ service: 'Slack',
+ message: error.message,
})
}
+
+ if (errors.length == 0) {
+ return new Response(undefined, { status: 200 })
+ } else {
+ return new Response(JSON.stringify(errors), { status: 400 })
+ }
})