Curing the Post MAGFest Blues with Twilio, Ruby, Sinatra and SEGA music

January 18, 2017
Written by
Sam Agnew
Twilion

15965787_10210850536154418_8346767457567181273_n

MAGFest is a video game music festival with just about everything fun that you can imagine: a giant arcade, amazing video game cover bands, tons of nerdy merchandise and a fantastic community. Going back to normal life can be a bit of a bummer after partying with some best friends you only see a few times a year.

Based on a suggestion from my pals at theshizz, I built a phone-based jukebox that plays happy SEGA music to relive the feeling of MAGFest whenever you want.

If you want to hear the pure positively charged ecstasy of classic SEGA music, call this phone number:

(855) 635-7342

Let’s walk through how to build your own IVR based jukebox.

Building a phone based SEGA Jukebox in Ruby

Here is all of the code you need to build a SEGA jukebox:

require 'rubygems'
require 'twilio-ruby'
require 'sinatra'


def songs
  {
    '1' => 'http://your_own_url.to/SpaceHarrier.mp3',
    '2' => 'http://your_own_url.to/EmeraldHillZone.mp3',
    '3' => 'http://your_own_url.to/OutRunSplashWave.mp3',
    '4' => 'http://your_own_url.to/PowerDrift.mp3',
    '5' => 'http://your_own_url.to/PhantasyStar2.mp3',
    '6' => 'http://your_own_url.to/quartet.mp3'
  }
end

post '/call' do
  twiml = Twilio::TwiML::Response.new do |r|
    r.Gather :numDigits => '1', :action => '/play', :method => 'get' do |g|
      g.Say 'Welcome to the post MAGFest hotline.'
      g.Say 'Where you can listen to happy SEGA tunes.'
      g.Say 'Courtesy of theshizz dot org and the MAG Underground staff'
      g.Say 'For Space Harrier press 1'
      g.Say 'For Sonic 2 press 2'
      g.Say 'For Outrun press 3'
      g.Say 'For Power Drift press 4'
      g.Say 'For Phantasy Star 2 press 5'
      g.Say 'For Quartet Stage 1 press 6'
      g.Say 'Press any other key to let me decide.'
    end
  end
  twiml.text
end

get '/play' do
  twiml = Twilio::TwiML::Response.new do |r|
    if songs.has_key?(params['Digits'])
      r.Play songs[params['Digits']]
    else
      r.Play songs['1']
    end
  end
  twiml.text
end

Copy and paste that code into a new file called app.rb. Before being able to run this code, you’ll need to install some dependencies. Open your terminal and run the following commands:

gem install twilio-ruby
gem install sinatra

Played over the hotline is a list of music from SEGA games that represented the pure nostalgic happiness and energy of MAGFest to my group of friends:

If you want to build your own jukebox, replace the URLs to music of your choice, and update their name in the voice menu. You’ll need a publicly accessible URL to access each of those.

I’ll assume you might not have SEGA soundtracks lying around in the cloud, so if you need a URL to use for testing purposes, replace the first song URL in app.rb with the following:


def songs
  {
    '1' => 'http://demo.twilio.com/docs/classic.mp3',
    '2' => 'http://your_own_url.to/EmeraldHillZone.mp3',
    '3' => 'http://your_own_url.to/OutRunSplashWave.mp3',
    '4' => 'http://your_own_url.to/PowerDrift.mp3',
    '5' => 'http://your_own_url.to/PhantasyStar2.mp3',
    '6' => 'http://your_own_url.to/quartet.mp3'
  }
end

Calling your jukebox

Now that we have the code taken care of, let’s try actually calling a phone number to hear some jams. You’ll need a Twilio account for this, but don’t worry you can sign up for free.

When someone calls your Twilio number, Twilio makes an HTTP request to your app. Twilio expects an HTTP response from your web app in the form of TwiML, which is a set of simple XML tags used to tell Twilio what to do next.

Our app needs a publicly accessible URL so that Twilio can make that request. To avoid having to deploy every time we make a change, we’ll use a nifty tool called ngrok to open a tunnel to our local machine.

Ngrok generates a custom forwarding URL that we will use to tell Twilio where to find our application. Download ngrok and run it in your terminal on port 4567:

./ngrok http 4567

Next we need to grab a phone number or use the number that comes with your trial account and configure it to send POST requests to your app whenever you receive a phone call.

Open the phone number configuration screen in your Twilio console. Scroll down to the “a call comes in” field. Before entering your URL you should see:

Screen Shot 2017-01-18 at 3.11.45 PM

Punch in the URL for our call route that was generated by ngrok. It should look like http://your-ngrok-url.ngrok.io/call.

Click save, and run you application by entering the following command in your terminal:

ruby app.rb

Call your number and press 1 to hear some awesome music!

Now it can be MAGFest forever

Now you have your own phone number to dial whenever you want to hear music of your choice. It may not be quite as glorious as running down a hallway blasting Space Harrier on a boombox, but hopefully this will come close enough to hold me over until the next MAGFest.

I can already smell the Elevation Burger.

Feel free to drop me a line if you have any suggestions for music from games made by SEGA I should add to the hotline, or just want to show off what you built: