1

I have a rails application that i'am trying to get real time events from the my asterisk through AMI.

I successfully created a script to originate calls from the adhearsion source code without creating a new adhearsion project.

I created a class extending the adhearsion's ManagerInterface class and overriding the event_message_received method. From the Rails CLI if someone calls and i press the enter 2 or 3 times i get the event but i have to intervene to get it.

Here is my code :

    class Astercall < Adhearsion::VoIP::Asterisk::Manager::ManagerInterface

  def initialize
    super(:host => "host", :username => "username", :password => "password", :events => true)
    connect!
  end




  def self.click_call(number, exten, name)
    # asterisk = connect()

    originate(:channel => "SIP/#{exten}", 
                        :context => "from-internal",
                        :exten => number,
                        :priority => "1",
                        :caller_id => "Calling  #{name}")
  end

  def event_message_received(event)
    if(event.kind_of? Adhearsion::VoIP::Asterisk::Manager::ManagerInterfaceEvent )
          puts event.inspect
    end
  end



end

Do i have to run a background process to do that. If i do, how am i going to do it????

Thanks in advance

Matsoui
  • 21
  • 4

1 Answers1

3

While it is possible to use the Adhearsion AMI classes outside of an Adhearsion app, it's not really a supported use. Part of what the Adhearsion app provides is the daemonization and eventing subsystems needed to help process the events received from Asterisk, neither of which is really necessary just for the origination as you were doing before. If you really insist on using the library outside of an Adhearsion app you will need to do more than just subclass the ManagerInterface. Just be careful: what you do not want to do (mostly for performance reasons) is to cause a new AMI connection to be created/torn down with each Rails web request.

I would strongly suggest that you use an Adhearsion app to process the events from AMI and then use something like DRb to handle communication between Rails and Adhearsion. This approach has worked very well for us in several situations where we needed Asterisk events to be accessible in Rails (for example, displaying call queue statistics).

Ben Klang
  • 306
  • 2
  • 4
  • Ok! I didn't know I could do that! So how am I going to push the event from the adhearsion project to the rails app?? If I understand correctly this with this method you can access methods from the rails app to the adhearsion app, not the other way arround! How am I going to push the event to the rails app once it happens? – Matsoui Jul 10 '12 at 15:52
  • The question is, if you are outside a request, what is your Rails app going to do with the events it receives? Why would it care about them? Far more likely is that your UI will care, and you should use something like [faye](http://faye.jcoglan.com/) to push an event from Adhearsion to the browser (likely not the actual AMI event, but something that your client app will understand easily). – Ben Langfeld Jul 24 '12 at 15:10