Adding a delay to your Function's response is useful in certain use cases, particularly when it comes to interacting with Twilio Studio. One such case is if you are creating a chatbot and want longer, more realistic pauses between responses. Or perhaps you are making an HTTP request in a Studio Flow and want to add a delay to the retry loop on failure.
In all of these situations, the ability to add a delay is incredibly helpful. While Studio does not provide a native "Delay" widget, you can combine the Run Function widget with a Function that has a delayed response to emulate such behavior.
Below are some examples of what such a Function may look like. Before getting deeper into the examples, first create a Service and Function so that you have a place to write and test your Function code.
In order to run any of the following examples, you will first need to create a Function into which you can paste the example code. You can create a Function using the Twilio Console or the Serverless Toolkit as explained below:
If you prefer a UI-driven approach, creating and deploying a Function can be done entirely using the Twilio Console and the following steps:
- Log in to the Twilio Console and navigate to the Functions tab. If you need an account, you can sign up for a free Twilio account here!
- Functions are contained within Services. Create a Service by clicking the Create Service button and providing a name such as test-function.
- Once you've been redirected to the new Service, click the Add + button and select Add Function from the dropdown.
- This will create a new Protected Function for you with the option to rename it. The name of the file will be path it is accessed from.
- Copy any one of the example code snippets from this page that you want to experiment with, and paste the code into your newly created Function. You can quickly switch examples by using the dropdown menu of the code rail.
- Click Save to save your Function's contents.
- Click Deploy All to build and deploy the Function. After a short delay, your Function will be accesible from:
The Serverless Toolkit enables you with local development, project deployment, and other functionality via the Twilio CLI. To get up and running with these examples using Serverless Toolkit, follow this process:
- From the CLI, run
twilio serverless:init <your-service-name> --emptyto bootstrap your local environment.
- Navigate into your new project directory using
- In the
sms-reply.protected.jsfor a Protected Function intended to handle incoming SMS.
- Populate the file using the code example of your choice and save.
Note A Function can only export a single handler. You will want to create separate files if you want to run and/or deploy multiple examples at once.
Once your Function(s) code is written and saved, you can test it either by running it locally (and optionally tunneling requests to it via a tool like ngrok), or by deploying the Function and executing against the deployed url(s).
twilio serverless:start from your CLI to start the project locally. The Function(s) in your project will be accesible from
- If you want to test a Function as a Twilio webhook, run:
twilio phone-numbers:update <your Twilio phone number> --sms-url "http://localhost:3000/sms-reply"
This will automatically generate an ngrok tunnel from Twilio to your locally running Function, so you can start sending texts to it. You can apply the same process but with the
voice-urlflag instead if you want to test with Twilio Voice.
- If your code does not connect to Twilio Voice/Messages as a webhook, you can start your dev server and start an ngrok tunnel in the same command with the
ngrokflag. For example:
twilio serverless:start --ngrok=""
To deploy your Function and have access to live url(s), run
twilio serverless:deploy from your CLI. This will deploy your Function(s) to Twilio under a development environment by default, where they can be accessed from:
The act of delaying a Function's response is mostly just a matter of using a built-in method, such as
setTimeout, to delay the act of calling the
callback method and signaling that the Function has completed. If this Function were to be called by a Run Function widget, the Studio Flow containing that call will be delayed until this Function returns a response.
To provide a more
await friendly syntax in your Functions, this example demonstrates how to write a
sleep helper method that wraps
setTimeout in a Promise.
If your Function has no other actions to execute or if you don't see the need for Promises in this case, the next example demonstrates the same functionality, but without the
To see this in action, first create a new Function named
utils, and set its privacy level to Private. Paste in the following code, which exports the
sleep helper from before.
Next, open your existing Delay Function, remove the inline declaration of sleep, and add the highlighted line of code. Save and deploy all changes.