[Telegraph-users] Added some missing functionality for using Mealy machines

Peter P GMX Prometheus001 at gmx.net
Mon Mar 3 13:49:07 EST 2008


What impressed me most of this telegraph api was the possiblity to use 
the model view controller pattern. But some features were missing there 
to make it really flexible. After some changes now Iam really happy. I 
would like to share some addons with you.

I had the following idea:
Why no setup dialplans graphically or via database?

What you need therefore is an implementation for a Mealy  machine (mean: 
A machine has a certain state (@params), and the input from a user (or 
even no input == Moore machine) generates the next state.

The most important issue was to render a voice to a certain view. This 
did not work in Telegraph implementation I had.

So I added the new method "render_voice_to(action)"

vendor/plugins/telegraph/lib/agi.rb:
    def render_voice_to(dest)
      begin
          #This needs improvement.  Rely's too much on defaults
          f= "#{template_root}/#{default_template_name(dest)}.voice"
          render_voice do |voice|
            eval File.read(f)
          end
      rescue Errno::EPIPE
        call_hung_up
      rescue Errno::ECONNRESET
        #User hungup.  If the hung_up callback exists, call it
        call_hung_up
      end
      @performed_render = true
    end

Now you can render e.g. the method "index" to a method named "dial", 
e.g. render_voice_to('dial').
The appropriate dial.voice may contain the following:
      voice.dial @number
or
    voice.dial_link(@actionparams, {:controller=>'dialhandlers', :action 
=> 'index', :sequence => @sequence})
    (I added e.g. a sequence number for my Mealy machine)

You may also use helpers here!!! This is important if you like to use 
e.g. menus with dynamic menu items read from the database. I defined 
voice helpers in application.rb as usual html helpers dont't work here.
You may be astonished about the method "voice.dial_link" I used. When 
you generate a Mealy machine, you have to come back to your current 
controller or to another controller. So you need to redirect your 
request, after it has finished. This is not done in current Telegraph 
implementation.
So I modified and added some new methods in 
vendor/plugins/telegraph/lib/call_connection.rb which enable this behaviour:

dial_link
      # Dial an address and redirect ro link.
      # \param address - The address to dial e.g. SIP/1234 at foo.com
      # \param waittime
      # \param time Maximum time for the call, in sec, or 0 to specify 
no time limit
      # \param extraOptions Extra options to pass to asterisk, as a string
    def dial_link(address, link, waittime=15, time=0, extraOptions="")
        options = "g#{extraOptions}"
        if time > 0
          options = "#{options}S(#{time})"
        end
        exec("Dial", "#{address}|#{waittime.to_s}|#{options}")
        @request.create_redirect link if link
    end

play_sound_link:
    # PST: Plays a sound over the channel, synchronously until the end 
of the sound.
      # Then redirect to link if available
      # Your asterisk must have the appropriate codecs installed.
      # The param "soundName" must be a full path to the sound file on 
the Asterisk server
    def play_sound_link(soundName, link=nil)
        exec("playback", soundName)
        @request.create_redirect link if link
    end

speak_text_link:
    # Calls swift.agi to speak some text and redirects to link if 
available.  Sorry, this expects the Cepstral engine.
    def speak_text_link(texttospeak, link=nil)
      fixedmessage = texttospeak
      fixedmessage = fixedmessage.gsub("\r", " ")
      fixedmessage = fixedmessage.gsub("\n", " ")
      fixedmessage = fixedmessage.strip
      exec("AGI", "swiftphp.agi|\"" + fixedmessage + "\"")
      @request.create_redirect link if link
    end

I will create some new methods in the future.
Enjoy!

P.S.: My Mealy machine works now. I setup Voice menus via Freemind(nodes 
with attributes and links), read the file into the database and redirect 
Asterisk to the Telegraph AGI server.  As I have also setup html views 
in this app, I can test the workflow on the web page and via phone at 
the same time.

Best regards
Peter



More information about the Telegraph-users mailing list