From 6357db32eeb0b8d01953f0dd15a89f1c0017955a Mon Sep 17 00:00:00 2001 From: Naval Date: Thu, 6 Mar 2025 11:06:16 +0530 Subject: [PATCH] cleanup and env changes Signed-off-by: Naval --- package.json | 4 +- supabase/config.toml | 5 +- supabase/functions/.env | 4 +- supabase/functions/call-sms-gateway/index.ts | 68 ++++++++++++++++++++ supabase/functions/get-messages/.npmrc | 3 - supabase/functions/get-messages/deno.json | 3 - supabase/functions/get-messages/index.ts | 61 ------------------ supabase/functions/send-sms/index.ts | 57 ---------------- 8 files changed, 75 insertions(+), 130 deletions(-) create mode 100644 supabase/functions/call-sms-gateway/index.ts delete mode 100644 supabase/functions/get-messages/.npmrc delete mode 100644 supabase/functions/get-messages/deno.json delete mode 100644 supabase/functions/get-messages/index.ts delete mode 100644 supabase/functions/send-sms/index.ts diff --git a/package.json b/package.json index 2678e3f..9d9937f 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ "build": "vite build", "build:dev": "vite build --mode development", "lint": "eslint .", - "preview": "vite preview" + "preview": "vite preview", + "supabase-local": "npx supabase functions serve", + "supabase-deploy": "npx supabase functions deploy" }, "dependencies": { "@hookform/resolvers": "^3.9.0", diff --git a/supabase/config.toml b/supabase/config.toml index 3712e59..9d8867b 100644 --- a/supabase/config.toml +++ b/supabase/config.toml @@ -264,9 +264,6 @@ policy = "oneshot" inspector_port = 8083 # Use these configurations to customize your Edge Function. -[functions.send-sms] -# enabled = true -verify_jwt = false # import_map = "./functions/MY_FUNCTION_NAME/deno.json" # Uncomment to specify a custom file path to the entrypoint. # Supported file extensions are: .ts, .js, .mjs, .jsx, .tsx @@ -275,7 +272,7 @@ verify_jwt = false # For example, if you want to serve static HTML pages in your function: # static_files = [ "./functions/MY_FUNCTION_NAME/*.html" ] -[functions.get-messages] +[functions.call-sms-gateway] verify_jwt = false [analytics] diff --git a/supabase/functions/.env b/supabase/functions/.env index 1aad653..64f43aa 100644 --- a/supabase/functions/.env +++ b/supabase/functions/.env @@ -3,4 +3,6 @@ TWILIO_AUTH_TOKEN=72be85bba93adad963360fe00ec5ad6d TWILIO_PHONE_NUMBER=+15038377084 SUPABASE_URL=https://ikmbnngahzcellthaysf.supabase.co -SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlrbWJubmdhaHpjZWxsdGhheXNmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDA1ODY5NjUsImV4cCI6MjA1NjE2Mjk2NX0.puDSeLAaTirOPyj6ndF2Mzzu8CKxlwJsoFP6gK8cWuA \ No newline at end of file +SUPABASE_ANON_KEY=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImlrbWJubmdhaHpjZWxsdGhheXNmIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NDA1ODY5NjUsImV4cCI6MjA1NjE2Mjk2NX0.puDSeLAaTirOPyj6ndF2Mzzu8CKxlwJsoFP6gK8cWuA + +SMS_ACTIVATE_API_KEY = eA103094961490b3350620b1bdd57244 \ No newline at end of file diff --git a/supabase/functions/call-sms-gateway/index.ts b/supabase/functions/call-sms-gateway/index.ts new file mode 100644 index 0000000..2c54699 --- /dev/null +++ b/supabase/functions/call-sms-gateway/index.ts @@ -0,0 +1,68 @@ + +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 { action, selectedService, selectedCountry } = await req.json() + + const smsActivateApiKey = Deno.env.get('SMS_ACTIVATE_API_KEY') + + if (!smsActivateApiKey) { + throw new Error('Missing platform credentials') + } + if(action != "getActiveActivations" && action != "getNumber"){ + throw new Error('Invalid action') + } + if(action === "getNumber" && (!selectedService || !selectedCountry)){ + throw new Error('Missing required fields') + } + + let apiResponse; + const myHeaders = new Headers(); + // myHeaders.append("Cookie", "__cf_bm=ZwMogfNq713s0IM1QbuvaGuJQK0rxRP8AVLa.yU0qYQ-1741017601-1.0.1.1-5KsvD93DDrImkQ.Inrh49z9A_TC_O44Tss_PIAnyp.x6Qfip.qoVL1pWB.MYalVT0oDPalmlcTnleNWGxVPUnGWkqig9lCl6WC.cZPhAxVg"); + + const requestOptions = { + method: "GET", + headers: myHeaders, + redirect: "follow" + }; + switch (action) { + case "getActiveActivations": + await fetch(`https://api.sms-activate.ae/stubs/handler_api.php?api_key=${smsActivateApiKey}&action=getActiveActivations`, requestOptions) + .then((response) => response.text()) + .then((result) => apiResponse = result) + .catch((error) => console.error(error)); + break; + + case "getNumber": + const apiUrl = `https://api.sms-activate.ae/stubs/handler_api.php?api_key=${smsActivateApiKey}&action=getNumber&service=${selectedService}&operator=any&country=${selectedCountry}&maxPrice=0.2250`; + await fetch(apiUrl, requestOptions) + .then((response) => response.text()) + .then((result) => (apiResponse = result)) + .catch((error) => console.error(error)); + break; + default: + break; + } + return new Response(apiResponse, { + 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, + }) + } +}) diff --git a/supabase/functions/get-messages/.npmrc b/supabase/functions/get-messages/.npmrc deleted file mode 100644 index 48c6388..0000000 --- a/supabase/functions/get-messages/.npmrc +++ /dev/null @@ -1,3 +0,0 @@ -# Configuration for private npm package dependencies -# For more information on using private registries with Edge Functions, see: -# https://supabase.com/docs/guides/functions/import-maps#importing-from-private-registries diff --git a/supabase/functions/get-messages/deno.json b/supabase/functions/get-messages/deno.json deleted file mode 100644 index f6ca845..0000000 --- a/supabase/functions/get-messages/deno.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "imports": {} -} diff --git a/supabase/functions/get-messages/index.ts b/supabase/functions/get-messages/index.ts deleted file mode 100644 index 327da39..0000000 --- a/supabase/functions/get-messages/index.ts +++ /dev/null @@ -1,61 +0,0 @@ -// Follow this setup guide to integrate the Deno language server with your editor: -// https://deno.land/manual/getting_started/setup_your_environment -// This enables autocomplete, go to definition, etc. - -// Setup type definitions for built-in Supabase Runtime APIs -import "jsr:@supabase/functions-js/edge-runtime.d.ts" -import { corsHeaders } from '../_shared/cors.ts' -import { createClient } from 'https://esm.sh/@supabase/supabase-js@2' - - -console.log("Hello from Functions!") - -Deno.serve(async (req) => { - const { name } = await req.json() - const data = { - message: `Hello ${name}!`, - } - - try { - 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", twilioAccountSid, twilioAuthToken) - - const twilioUrl = `https://api.twilio.com/2010-04-01/Accounts/${twilioAccountSid}/Messages.json?PageSize=20` - const response = await fetch(twilioUrl, { - method: 'GET', - headers: { - 'Content-Type': 'application/x-www-form-urlencoded', - Authorization: `Basic ${btoa(`${twilioAccountSid}:${twilioAuthToken}`)}`, - } - }) - - 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, - }) - } -}) - -/* To invoke locally: - - 1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start) - 2. Make an HTTP request: - - curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/get-messages' \ - --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \ - --header 'Content-Type: application/json' \ - --data '{"name":"Functions"}' - -*/ diff --git a/supabase/functions/send-sms/index.ts b/supabase/functions/send-sms/index.ts deleted file mode 100644 index 18a9294..0000000 --- a/supabase/functions/send-sms/index.ts +++ /dev/null @@ -1,57 +0,0 @@ - -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, - }) - } -})