twilio-labs / serverless-toolkit

CLI tool to develop, debug and deploy Twilio Functions

Home Page:https://www.twilio.com/docs/labs/serverless-toolkit

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can you simulate inbound SMS messages to your local function?

jamesvclements opened this issue · comments

commented

Sorry in advance for this silly question, but is there any way to "text" your function when it's running locally? Or should I just reference the docs for what fields (context, event, callback) contain?

Or is better to deploy, log the payload, and then use that as a reference when developing locally.
Thank you!

Thank you so much for opening your first issue in this project! We'll try to get back to it as quickly as possible. While you are waiting...here's a random picture of a corgi (powered by dog.ceo)

picture of dog

There's a couple of ways you can do this:

1) Manual testing

You can hook up your localhost to a phone number by using a tool like ngrok. In fact ngrok is already built-in both into the Serverless Toolkit as well as the Twilio CLI in general. Try running:

twilio phone-numbers:update <your-twilio-number> --sms-url="http://localhost:3000/<path>"

This will automatically set up an ngrok tunnel and configure your Twilio phone number to trigger that tunnel to your localhost.

Important If you use this phone number in production you should instead use a separate one.

Afterwards you can text your number like you normally do and it should trigger your local Functions. Additionally you can go to the ngrok interface: http://127.0.0.1:4040 and inside there you can replay webhook requests that way you don't have to continue to text in the same thing.

2) Webhook Plugin

We literally just released a small new utility that can emulate the requests that Twilio do as webhook requests. This is still a fairly new tool but you can try it by running:

twilio plugins:install @twilio-labs/plugin-webhook
twilio webhook:invoke <your-localhost-path> --no-signature

And it should emulate an SMS request. Note that these parameters are just emulated so it might slightly differ as much as we try to keep them accurate. You can find more info here: https://npm.im/@twilio-labs/plugin-webhook.

Let me know if this helps. There's also these docs that list which parameters get passed to an SMS webhook any of these parameters should translate into a key/value pair on event. For example Body becomes event.Body and contains the SMS body.

commented

@dkundel this was extremely helpful, thank you! Both solutions work great.

If anyone gets ERR_NGROK_9018 error when trying to follow the manual testing instructions, make sure you add your ngrok authtoken into the ngrok config that twilio uses, rather than your global instance. For me, that was located here:

cd /usr/local/Cellar/twilio/4.2.0/libexec/node_modules/.bin
./ngrok authtoken YOUR_AUTH_TOKEN

twilio-cli uses v2 of ngrok—the default instructions when creating a new ngrok account are for v3, so make sure you use the correct command for the ngrok your twilio instance is executing.

commented

@dkundel one quick follow-up. After setting up an ngrok tunnel to my localhost for manual testing, is there a fast way to reset the phone number back to the deployed twilio function? That's probably why you suggest using a different number than in production, but this is just a little side project I'm hoping not to pay for two numbers for 😅