You can use Twilio Functions with Programmable SMS to send and receive text messages without managing your own infrastructure. All you need is a Twilio account and a few lines of Node.js code.
Before you can use Programmable SMS with a Function, you'll need to sign up for a Twilio account and purchase an SMS-capable phone number. When you search for a number to buy, make sure the number you choose supports SMS messages by checking the number's capabilities before you purchase it.
Click "Buy" to add one of these numbers to your account. Now that you have a Twilio account and a programmable phone number, it's time to write some Functions code.
To create a new function, go to Functions page in the Twilio Console and click the "+" icon.
You can either create a new Function from scratch or start with one of our templates. For this quickstart, choose the "Hello SMS" template and click "Create".
That will create your new Function and prepopulate it with code that looks like this.
Take a look at the path property at the top of your function. Type "/sms" in that field and then click "Save" at the bottom of the form. Now copy the full URL for the path field. You'll need it for our next step.
Go back to the phone number you purchased earlier. You'll find it in your list of Twilio phone numbers in the Console.
Click on your phone number and then scroll down to the "Messaging" section. Find the "A message comes in" field, choose "webhook" from the select menu, and then paste the full URL to your Twilio Function. Then click "Save" on your phone number.
Your number should be ready for action! Try sending it a text message and see if you get a reply.
If you don't get a reply, try looking in the Console debugger or your Twilio Function logs, which are updated live when you're editing your function.
With a single API request, we can send an outbound text message from the Twilio phone number we purchased earlier. Copy and paste in this code sample to your Function.
You'll want to update the to value to be your cell phone number and the from value to be your Twilio phone number. Be sure to use E.164 formatting for both numbers.
Before it can work, though, you'll need to update your Functions configuration to tell Twilio to include your account credentials in every function's environment. Check the box that says "Enable ACCOUNT_SID and AUTH_TOKEN."
NOTE: You'll also need to uncheck the box on your function called "check for valid Twilio signature" before your function will accept requests from outside Twilio.
This feature locks down your function so only Twilio can access it, which helps prevent abuse. But it's easier to test your function when this feature is turned off.
Now try invoking your Function by sending an HTTP request to it. You can send an HTTP request to your function any way you like. We recommend just copying the "path" value and opening it in a new browser tab.
You should receive a new message on your phone shortly. If you didn't, try checking the logs which appear beneath your function's code for error messages.
誰しもが一度は考える「コーディングって難しい」。そんな時は、お問い合わせフォームから質問してください。 または、Stack Overflow でTwilioタグのついた情報から欲しいものを探してみましょう。