Tropo is part of CiscoLearn More

Ruby on Rails Conference Caller

Posted on February 24, 2012 by Team Tropo

Have you ever wanted to initiate a conference call and have it dial everyone’s phone numbers and invite them into a standup conference call? I will show you how to do this using Ruby on Rails and the Tropo Scripting API. The following Rails application is running version 3.2.1 powered by Twitter Bootstrap for the sweet CSS styling plus Gritter jQuery alerts (like Growl) with a little AJAX mixed in to liven things up.

Here is a screencast to show off my conference caller app!

The Rails application uses the Ruby Rest-Client gem to kick off the Tropo conference caller script while passing the Tropo script a comma delimited string of numbers to dial. The Rails Rest-Client call looks like:

response = RestClient.get "http://api.tropo.com/1.0/sessions?
action=create&token=0e0a2a5742a75c...e01102c8f745818848
&phones=#{phonenumbers}"

Here is the Tropo Ruby script:

########################################################################
# Tropo script using Ruby and the Tropo Scripting API #
# The script receives a comma delimited string of phone #
# numbers from a Rails application and dials each number #
# using a seperate thread for concurrency. As people answer #
# the script asks if they would like to join the conference. #
# If the user says "one" or presses the 1 key on their phone, #
# they will be added to the conference id with the other #
# participants. #
########################################################################

# set conference options - no one is muted and DTMF tones can be heard
conferenceOptions = {
:mute => false,
:playTones => true
}

#Create conference ID based on timestamp for uniqueness
conferenceID = Time.new.strftime("%Y%H%M%S")

# Create an array to track threads
threads = []

# Loops through comma delimited string of phone numbers as an array and
# creates a thread for each number to call and conference
$phones.split(",").each do |x|

# Spin up a thread for each call and conference leg
threads << Thread.new do

# Log the number we are calling
log "@"*10 + "Calling " + x

# Call the number (x) and setup onAnswer event for further processing
call 'tel:+' + x, {
:onAnswer => lambda { |event|

# Log that the number dialed answered the call
log "@"*10 + "Invitee answered join conference"

# Set the new call object based on the event value
newCall = event.value

# Inform the person being called that this is a conference call and ask them to join
newCall.say "You are being invited to join a conference call. "

# Wait for the caller to say or press a single digit. Timeout in 15 seconds if nothing heard.
newCall.ask "Press or say 1 to accept the call", {
:choices => "[1 DIGITS]",
:timeout => 15.0,
:onChoice => lambda { |invitee|

# This routine runs when a valid digit is spoken or pressed.
# Validity is determined by the value of choices
if invitee.value == "1"

# If the called party says or presses the 1 key play a beep MP3 sound file
newCall.say "http://conferencecaller.herokuapp.com/beep.mp3"
# Add the called party to the same conference as the other participants
newCall.conference(conferenceID, conferenceOptions)

else

# The user said or pressed something other than "one".
# Tell them good bye and then hangup
newCall.say "thats o.k. now they will probably talk about you."
newCall.hangup

end
}
}
}
}
end
end

# Join the array of threads together so the Tropo session stays alive until the last person has left the conference.
threads.each { |t| t.join }

All of the source code from this demo is located on GitHub. Please keep Tropo in mind the next time you have a Voice or SMS API need in your application!

2 Responses

  1. I tested this demo and really is amazing call to others at the same time. I tested over Mexican phones.

  2. Surya says:

    Great job. Is possible to write on php script. I need this on php.

Leave a Reply