A catch is called whenever a promise is rejected (i.e. This can be fixed by chaining a catch method to our promise. That means if the user texts in a non-existent cryptocurrency or CoinMarketCap is unreachable, the user will get no response. Our application is complete, but there is currently no behavior defined in our Function in the event that an error occurs. Save these changes and test the Function by texting the name of any cryptocurrency to your Twilio number (Bitcoin, for example). Navigate to your Phone Numbers, select the number your own to that you want to configure, scroll down and configure the Messaging section like this: With this code in place, the next step is to link this Function to a Twilio number you own. Twilio will handle the rest by sending an outgoing SMS. Once the callback is invoked, the Function will ensure that the MessageResponse object is turned into XML and the HTTP response’s content-type is set to text/xml. ![]() We should also refactor how the message is being constructed to something more readable: Let’s create a TwiML MessagingResponse object and pass it to the callback, similar to what was seen in the original template code. However, the Function will timeout when invoked because the callback parameter is never called. The code above contains the bulk of the application logic. The (price_change > 0 ? 'u2191' : 'u2193') expression is a conditional ternary operator which determines if the message should contain an up arrow or down. Inside that function we parse the JSON response from CoinMarketCap and obtain the currency’s value, symbol and percent-change in the last 24 hours, then piece everything together to form a message that resembles a stock ticker. Asynchronous JavaScript can be very tough for a beginner, but luckily there are tons of resources to help understand it. Since the HTTP request we’re making to the CoinMarketCap API happens asynchronously, got uses a promise to tell us when that HTTP request is complete, calling the function defined inside of the then function. The entire expression itself is wrapped as a template literal that will evaluate () and insert it into the the string to form a URL. The trim method removes any leading or trailing spaces. event.Body is the message contents of the SMS messages that will be sent to your Twilio number later on. Notice that the URL being requested contains the expression, $. The code above uses got to make an HTTP GET request to CoinMarketCap. As a result, the path that was just set above will only be accessible by incoming messages to Twilio phone numbers.Īdd the following to the top of the Function: This ensures that only requests made via SMS through Twilio can run this Function. Set the Event to “Incoming Messages”, and make sure that “Access Control” is checked. The path serves as a publicly accessible URL which can be used to invoke the Function from anywhere, not just from a Twilio call or SMS. The name should be something simple that describes what the Function does (“CryptoCurrencyLookup”, for example). The code inside of the function is creating a new TwiML response object containing a Message verb with a message of “Hello World”. You can read more about the above code in this Twilio support page. Finally, callback is the method you call to return from the Function. The event object includes information about a specific invocation, including HTTP parameters from the HTTP request. The context parameter includes information about your runtime, such as configuration variables. Every Function you write will follow this pattern. The code above determines what will happen when the Function is invoked. You’ll notice the following Node.js code already in the Code section of your Function: Click the red ‘ ’ circle and select the Hello SMS template. Creating a New Functionįunctions can be accessed from the Twilio Console by navigating to Runtime -> Functions. It may also benefit you to read the Intro to Functions blog post that explains a lot of the basics that this post will touch on. In order to build this application and use Twilio Functions you’ll need the following: ![]() ![]() If you’re already familiar with setting up a new Function, the finished code for this Function is available on Github here. The finished app will use CoinMarketCap to send us pricing information via SMS about any cryptocurrency we send (Bitcoin, Ethereum, Dogecoin, etc). ![]() Let’s get acquainted with Functions by building an app that monitors the price of cryptocurrencies. With Functions, you can build simple Twilio applications in JavaScript without worrying about configuring servers, finding a place to host TwiML or tunneling to localhost. Twilio recently introduced Functions, a serverless environment to build and run Twilio apps.
0 Comments
Leave a Reply. |