58 lines
1.7 KiB
TypeScript
58 lines
1.7 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 })
|
|
}
|
|
if (req.method === 'GET') {
|
|
return new Response('ok', { headers: corsHeaders })
|
|
|
|
}
|
|
|
|
|
|
try {
|
|
const { phoneNumber, message } = await req.json()
|
|
const abc = Deno.env.get('abc')
|
|
console.log("abc", abc)
|
|
|
|
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')
|
|
}
|
|
|
|
console.log("env", Deno.env)
|
|
|
|
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,
|
|
})
|
|
}
|
|
})
|