Adhearsion-Twilio is an Adhearsion Plugin, which allows you to quickly migrate an existing app built on Twilio over to an Adhearsion app, or create a new Adhearsion app from scratch without having to worry too much about how Adhearsion works.
Chibi is a Mobile Dating and Chat Service based in Cambodia that connects it's subscribers with new friends via SMS and Voice. Subscribers can SMS or call to a local phone number and be instantly and anonymously connected with a new friend in their area. There's no need for registration or payment. Registration is done automatically when the subscriber calls or SMS's for the first time and the subscribers profile is built up overtime by automatic analysis of SMS and voice data from the subscriber.
Originally, we powered the Voice side using Twilio. Twilio makes it super-simple to bootstrap an application like this. We signed up for an account, purchased a number and hooked it up to a Twimlbin at (http://twimlbin.com/). Within minutes we had a woman's voice saying "Hello World" when we dialled the number. With a few more lines of TwiML we had it dialling and joining calls, the basics for what we needed to achieve for Chibi.
The next step was to move the static TwiML served from the Twimlbin to our main Rails app and have it dynamically generate the TwiML. Thankfully, this is also a breeze using twilio-ruby. Soon, when we dialled our test number, it would register us (if it was our first call), find us a new friend, dial our new friend and join us together. Instant love!
With our Voice app set-up and running in production our subscribers could call to a US number and be connected with new friends. But there were a few of problems to overcome.
Firstly, our subscribers are Cambodian. They don't want to call to a US number because it's too expensive and Twilio don't offer Cambodian numbers for purchase.
Secondly, every-time our subscribers called to our number to meet a new friend, we would have to foot the bill for the outgoing call. Since the outgoing call was always to Cambodia, this would cost us 14.8c / min.
Thirdly, since there is no registration or payment required for Chibi we had no clear way to charge our subscribers for the voice service.
To turn this into a business we clearly needed a different approach but we still wanted to keep our Twilio number open for subscribers and investors outside of Cambodia to try.
We demonstrated our Voice service built on Twilio to a local telco and they were impressed. After some discussion with their technical team they agreed that they could send us the SIP/RTP for an incoming call from a subscriber to a short-code that we had set up. They would also allow us to place outgoing calls to their subscribers. They would handle the billing and we would share the revenue.
This model had the potential to solve all three blockers. It gave our subscribers a local number to call, it eliminated the costly outgoing call charge and it provided a way for us to charge our subscribers.
After reading up on Adhearsion we decided to give it a try using FreeSWITCH. We started a blank EC2 instance located in Singapore (close to Cambodia) and installed FreeSWITCH. We then created an Adhearsion simon-game app, deployed it to Heroku, hooked it up to our FreeSWITCH box and tested it out with a Softphone. It worked great.
The next logical thing to do was to somehow migrate our voice application logic to Adhearsion. Soon though it became apparent that the logic didn't belong in Adhearsion. Chibi was dynamically generating the TwiML to return from the data it had about our subscribers. We wanted to keep our Twilio app running, but we didn't want to duplicate the logic between Chibi and our Adhearsion app. Furthermore we had already fully tested our call-flow by asserting TwiML using RSpec and Capybara. What we really wanted was to have Adhearsion interpret the TwiML returned by Chibi just like Twilio does.
With this in mind we decided to explore the idea of building a TwiML interpreter for Adhearsion. Thankfully, Adhearsion makes it really easy to build plugins for. Using VCR we began to map the TwiML that we needed to interpret to Adhearsion commands. This is now our Adhearsion app that powers Chibi.
class CallController < Adhearsion::CallController
include Adhearsion::Twilio::ControllerMethods
def run
answer
notify_voice_request_url
end
end
An environment variable AHN_TWILIO_STATUS_CALLBACK_URL
specifies the url in which to retrieve the TwiML from. This is set to the same URL as configured in Twilio.