This Number Rocks, Rocking Out with Elixir Lang
Time to read: 3 minutes
Until recently, Henri and his team relied on a landline as the main phone system for their company. While searching for a way to modernize, they decided to build a solution themselves with Twilio that would allow for both more flexibility and control.
The team took the simple concept of a Twimlet but decided to give it their own flair. Using Elixir with the Twilio REST API, they were quickly up and calling!
They built the CRUD app with the Phoenix Framework and deployed on Heroku. A Postgresql database table keeps track of the Twilio numbers and phone numbers they point to, stored as embedded structs using an array of maps and Elixir’s Ecto embedded schema.
All Twilio calls are routed to two functions which determine what to do if there is an incoming SMS or a call. The function for SMS sends an email and the function for a call will try in turn each “real” phone number associated with the “virtual” Twilio number.
A call comes in:
The try_real_number
private function looks for the called number in the database, tries to dial a “real” number – Henri’s phone for example – and sets an HTTP callback to Twilio keeping track of the current trial index. If no one answers, or a call comes in outside of open hours, a TwiML <Record> instruction is sent back to Twilio.
The callback action URL provided in the <Dial> instruction uses destructuring to run different routines according to the type of action:
- Voicemail recording notifications are forwarded to the other user by email.
- Completed calls are finished.
- Unanswered call attempts trigger a new call to
try_real_number
with the next “real” number in the list.
The answer
function just wraps an XML snippet in a Twilio response:
Without a helper library for Elixir, Henri said he relied heavily on the logging system in the Console. “It was really a piece of cake!”
This Number Rocks
Henri created the solution with their own use in mind. “It did not require a lot of work. It was perfect,” he said. But after sharing it with friends in the local startup community, people loved it and wanted to start using it too. “Having a Twilio number could bring a lot of benefits to them as it does for us.” They set up thisnumber.rocks to make it so others, even without technical knowledge, could also have a fancy call forwarding system.
A virtual number for freelancers and small orgs using Twilio https://t.co/JHB7YTpbIA pic.twitter.com/iywZwsCPNU
— Simon Ashley (@simonashley) March 14, 2017
Whether you’re inspired to start building your own solution from scratch or following the tutorial to get started on thisnumber.rocks is more your style, we’re always happy to hear what you’re up to.
Feel free to drop a line to:
- Email: mspeir@twilio.com
- Twitter: @meganspeir
- Github: meganspeir
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.