Twilio-poc/supabase/functions/send-sms/index.ts
gpt-engineer-app[bot] a149662806 Fix: Connected
The prompt indicated a connection issue needed to be addressed.  This commit addresses that issue.
2025-02-26 16:24:44 +00:00

48 lines
1.5 KiB
TypeScript

import { serve } from 'https://deno.land/std@0.168.0/http/server.ts'
import { corsHeaders } from '../_shared/cors.ts'
import { createClient } from 'https://esm.sh/@supabase/supabase-js@2'
serve(async (req) => {
// Handle CORS
if (req.method === 'OPTIONS') {
return new Response('ok', { headers: corsHeaders })
}
try {
const { phoneNumber, message } = await req.json()
const twilioAccountSid = Deno.env.get('TWILIO_ACCOUNT_SID')
const twilioAuthToken = Deno.env.get('TWILIO_AUTH_TOKEN')
const twilioPhoneNumber = Deno.env.get('TWILIO_PHONE_NUMBER')
if (!twilioAccountSid || !twilioAuthToken || !twilioPhoneNumber) {
throw new Error('Missing Twilio credentials')
}
const twilioUrl = `https://api.twilio.com/2010-04/Accounts/${twilioAccountSid}/Messages.json`
const response = await fetch(twilioUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${btoa(`${twilioAccountSid}:${twilioAuthToken}`)}`,
},
body: new URLSearchParams({
To: phoneNumber,
From: twilioPhoneNumber,
Body: message,
}),
})
const result = await response.json()
return new Response(JSON.stringify(result), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 200,
})
} catch (error) {
return new Response(JSON.stringify({ error: error.message }), {
headers: { ...corsHeaders, 'Content-Type': 'application/json' },
status: 400,
})
}
})