Send a Text with Twilio from a Smart Contract
Twilio is a cloud communications platform that enables developers to programmatically make and receive texts and phone calls. Ethereum developers are finding increasing use cases for mobile notifications to communicate with their users. Sending text messages from on-chain allows smart contracts to send notifications to users without running infrastructure or separate nodes. In this post we’ll build a contract that sends texts using the Twilio SMS API. We’ll set up a midpoint to link our contract with Twilio.
Get a Twilio Auth Token and Phone Number
In order to send texts you must have a verified Twilio account with an authentication token and phone number. After registering an account — following the setup instructions to get a phone number. Texts from our contract will be sent from this number. Your account starts in trial mode until you have added funds. When in trial mode, you will only be able to send text messages to your own phone number. Continue testing in trial mode with your own number or fund your account to send texts to anyone.
Create a Twilio Midpoint
We will now create a midpoint to pass data from on-chain to Twilio. Login or create an account on Midpoint. Create a new midpoint and select the “Send a Text Message with Twilio” template. This loads a pre-configured template midpoint that can send text messages to individual recipients.
Go to the “Use This Midpoint” page, select your desired chain, and select the storage sample contract. This will generate a contract ready for deployment. Copy that contract into your favorite editor and deploy. This contract has one function — testMidpointRequest() which we’ll invoke to make an API call. For more information on the contents of this autogenerated contract see https://docs.midpointapi.com/midpoint-documentation/concepts/misc/the-use-this-midpoint-page. These two lines format our input variables and call our midpoint
// Packs our on-chain variables, message is our text message and recipient is the phone number we're sending it to bytes memory args = abi.encodePacked(message, bytes1(0x00), recipient, bytes1(0x00)); // Call our midpoint uint64 Request_ID = IMidpoint(startpointAddress).callMidpoint(midpointID, args);
Give Our Midpoint Twilio Credentials
Now we need to add our Twilio credentials to our midpoint. Go to the "Secret" tab found in the top of the right bar and paste in your phone number, SID, and auth token.
Enter a message and recipient into the “Test this midpoint” tab on the bottom of the right bar and hit “Run Request”. Make sure your target phone number consists of a country code followed by a 10-digit number.
Send a Text from On-Chain
We’re ready to go! Save and publish your midpoint. Transact to the testMidpointRequest function with your target recipient and message. You can track the progress of your request in the Requests tab.
Our invocation kicks off a Request Transaction which contains our on-chain variables.
Congratulations! You’ve just created a smart contract capable of sending a text through the Twilio API via a midpoint! This contract was just a sample that sent a message directly passed in but hopefully provides inspiration for future projects. If you build something let us know! Today developers are using Midpoint to connect their contracts to hundreds of off-chain endpoints including DocuSign, Google Cloud, Slack, Salesforce, Twitter, and Discord. If you’re a developer and want to connect your smart contract to off-chain endpoints join the conversation on Discord and check out our documentation.
Read from a Wikipedia Article with Polygon
This lesson shows how to build a Polygon smart contract that reads the summary of a Wikipedia article and posts it on-chain.
Midpoint Labs is Sponsoring ETHSanFrancisco!
We're sponsoring ETHSanFrancisco on November 4th in the Palace of Fine Arts.