SMS-Hook with Twilio Verify

Are we able to use SMS-hook with twilio verify? I tried and the sms is sent but the edge function always returns an error to the client application in flutter.
11 Replies
rlee128
rlee128OP3w ago
I now get an invalid payload sent to hook message
inder
inder3w ago
You will need to show your edge function code. Sms hook simply calls your function and you need to send a 200 response from function. sms hook doesn't care what you do inside the function. For example: You could even be sending email from this hook.
inder
inder3w ago
You have multiple log statements. Have you checked in logs where the issue arises?
rlee128
rlee128OP3w ago
Yes from what I can see is the response status is on pending which I have no clue on how to handle that. The example on supabase shows for the normal twilio and not verify
inder
inder3w ago
But thats something you will need to check on twilio docs. For example, this is my function where I send discord webhook from when sms hook triggers the function. hook only needs you to respond with 200 status code
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { Webhook } from 'https://esm.sh/standardwebhooks@1.0.0';

Deno.serve(async (req)=>{
const payload = await req.text();

const base64_secret = Deno.env.get("WEBHOOK_SECRET").replace('v1,whsec_', '');

const headers = Object.fromEntries(req.headers);

const wh = new Webhook(base64_secret);

const { user, sms } = wh.verify(payload, headers);
const res = await fetch(Deno.env.get("DISCORD_URL"), {
method: "POST",
body: JSON.stringify({
content: `Your OTP is: ${sms.otp}`
}),
headers: {
"Content-Type": "application/json"
}
});

if (!res.ok) {
return new Response(JSON.stringify({
message: await res.text()
}), {
status: 500
});
}
return new Response(JSON.stringify({}), {
status: 200,
headers: {
'Content-Type': 'application/json'
}
});
});
import "jsr:@supabase/functions-js/edge-runtime.d.ts";
import { Webhook } from 'https://esm.sh/standardwebhooks@1.0.0';

Deno.serve(async (req)=>{
const payload = await req.text();

const base64_secret = Deno.env.get("WEBHOOK_SECRET").replace('v1,whsec_', '');

const headers = Object.fromEntries(req.headers);

const wh = new Webhook(base64_secret);

const { user, sms } = wh.verify(payload, headers);
const res = await fetch(Deno.env.get("DISCORD_URL"), {
method: "POST",
body: JSON.stringify({
content: `Your OTP is: ${sms.otp}`
}),
headers: {
"Content-Type": "application/json"
}
});

if (!res.ok) {
return new Response(JSON.stringify({
message: await res.text()
}), {
status: 500
});
}
return new Response(JSON.stringify({}), {
status: 200,
headers: {
'Content-Type': 'application/json'
}
});
});
rlee128
rlee128OP3w ago
Okay I see so that might be the issue let me adjust that. Thanks
inder
inder3w ago
in your code you have this if check: if (response.status !== 'queued') { and you mention that response.status is pending, so this if check is true
rlee128
rlee128OP3w ago
That worked
inder
inder3w ago
If it was something specific to twilio can you please add it here so that someone might benefit in the future.
rlee128
rlee128OP3w ago
So just switching the status check to check if pending or if you really don’t need the status check both worked. I am doing some more testing just to make sure everything works

Did you find this page helpful?