Function fires off URL link when used with Twilio SMS

Hi everyone :sun_with_face: ,

I’m using Twilio to send an SMS in a lambda function. The URL included in the SMS body is being fired when the SMS is created.

Any advice to resolve this? :female_detective:

@berni, I think you are saying that the url that is part of your payload is being invoked/called by your function? How are you wrapping your body? Are you using backticks (template literals)? Something like this?

client.messages.create({
                body: `Your url here`, 
                from: process.env.TWILIO_FROM_PHONE,
                to: `+1${target_phone}`
            });

@Dennis that’s correct. I am using back ticks.

berni, can you give us more information, please? like post your code? as i have mentioned before, it is very difficult (as in we can’t do it) to help you without sufficient details. thanks.

Hi @perry,

// Docs on event and context https://www.netlify.com/docs/functions/#the-handler-method
exports.handler = async (event, context, callback) => {
  try {
    const body = JSON.parse(event.body) || null;
    if (!body.phone) throw new Error("problem with data in body");
    if (!body.code) throw new Error("problem with data in body");
    if (!body.game) throw new Error("problem with data in body");

    const accountSid = process.env.TWILIO_ACCOUNT_SID;
    const authToken = process.env.TWILIO_AUTH_TOKEN;
    const client = require("twilio")(accountSid, authToken);

    const url = encodeURI(
      `${process.env.WEBHOOK_CODE_VERIFY}/.netlify/functions/verify_code?phone=${body.phone}&code=${body.code}&game=${body.game}`
    );
    const sms = await client.messages.create({
      body: `Hi there! Click the link to verify: ${url}`,
      from: "+12029527488",
      to: `+${body.phone}`,
    });

    callback(null, { statusCode: 200, body: JSON.stringify(sms.sid) });
  } catch (err) {
    callback(null, { statusCode: 500, body: err.toString() });
  }
};
**strong text**

Hey @berni, want to try something like this?

const sms = await client.messages.create({
      body: `Hi there! Click the link to <a href="${url}">verify</a>`,
      from: "+12029527488",
      to: `+${body.phone}`,
    });

@jen what a brilliant idea. It doesn’t mention anchors in the Twilio APIs or SDKs. I’ll give that a try.

:nerd_face:

1 Like