GithubHelp home page GithubHelp logo

ruby-asterisk's Introduction

RUBY-ASTERISK

Code Climate Donate via Zerocracy Managed by Zerocracy Maintainability

This gem add support to your Ruby or RubyOnRails projects to Asterisk Manager Interface

There was a project with the same name, but it appears to be discontinued so I decided to start a new project

Installation

Rails3

Add to your Gemfile and run the bundle command to install it.

 gem "ruby-asterisk"

Rails2

Simply run in your terminal

 gem install ruby-asterisk

Usage

INITIALIZE

To create a new AMI session, just call the following command

 @ami = RubyAsterisk::AMI.new("192.168.1.1",5038)

LOGIN

To log in, provide to the created sessions a valid username and password

 @ami.login("mark","mysecret")

Like all commands, it will return a Response command that could be parsed accordingly

CORE SHOW CHANNELS

To get a list of all channels currently active on your Asterisk installation, use the following command

 @ami.core_show_channels

PARKED CALLS

To get a list of all parked calls on your Asterisk PBX, use the following command

 @ami.parked_calls

ORIGINATE

To start a new call use the following command

 @ami.originate("SIP/9100","OUTGOING","123456","1","var1=12,var2=99") # CHANNEL, CONTEXT, CALLEE, PRIORITY, VARIABLES

COMMAND

To execute a cli command use the following code

 @ami.command("core show channels")

MEETME LIST

To get a list of all active conferences use the following command

 @ami.meet_me_list

EXTENSION STATE

To get the state of an extension use the following command

 @ami.extension_state(@exten,@context)

DEVICE STATE LIST

To get list of states of devices

 @ami.device_state_list(@exten,@context)

SKINNY DEVICES AND LINES

To get list of skinny devices

 @ami.skinny_devices

To get list of skinny lines

 @ami.skinny_lines

QUEUE PAUSE

To pause or unpause a member in a call queue

 @ami.queue_pause("SIP/100", "true", "myqueue", "reason")                                                                            

PING

To ping asterisk AMI

 @ami.ping

EVENT MASK

To enable or disable sending events to this manager connection

 @ami.event_mask("on")

SIPpeers

To get a list of sip peers (equivalent to "sip show peers" call on the asterisk server). This can be used to get a buddy list.

 @ami.sip_peers

SIP SHOW PEER

To get info of a peer (equivalent to "sip show peer" call on the asterisk server).

 @ami.sip_show_peer(peer)

SIP SHOW REGISTRY

Retrieve a status of SIP registries and their statuses from the Asterisk server.

 @ami.sip_show_registry

STATUS

To get a status of a single channel or for all channels

 @ami.status 

ATXFER

Attendand transfer

 @ami.atxfer(channel, exten, context, priority = '1')

WAIT EVENT

Wait for an event to occur. Timeout in seconds to wait for events, -1 means forever

 wait_event(timeout=-1)

MONITOR

Monitor a channel

 monitor(channel,mix=false,file=nil,format='wav')

STOP MONITOR

Stop monitoring a channel

 stop_monitor(channel)

PAUSE MONITOR

Pause monitoring of a channel

 pause_monitor(channel)

UNPAUSE MONITOR

Unpause monitoring of a channel

 unpause_monitor(channel)

CHANGE MONITOR

Change monitoring filename of a channel

 change_monitor(channel,file)

THE RESPONSE OBJECT

The response object contains all information about all data received from Asterisk. Here follows a list of all object's properties:

  • type
  • success
  • action_id
  • message
  • data
  • raw_response

The data property contains all additional information obtained from Asterisk, like for example the list of active channels after a "core show channels" command.

Todo List

  • Adding initialization parameters for Telnet options like Output_log, Waittime, Dump_log, timeout;
  • Adding test cases for better code coverage;
  • Refactoring of ruby-asterisk.rb, adding method_missing for the purpose of supporting as much AMI commands as possible

Development

Questions or problems? Please post them on the issue tracker. You can contribute changes by forking the project and submitting a pull request. You can ensure the tests passing by running bundle and rake.

This gem is created by Emiliano Della Casa and is under the MIT License and it is distributed by courtesy of Engim srl.

ruby-asterisk's People

Contributors

diego-asterisk avatar egens avatar emilianodellacasa avatar felixtriller avatar grzew avatar havilchis avatar hernamvel avatar jsinx avatar kubum avatar mangelvil avatar ostapische avatar pmint93 avatar sergio-xga avatar stewartmckee avatar tmeals avatar zuevevgenii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ruby-asterisk's Issues

Unknown response for all methods and commands.

Testing ruby asterisk with ruby version 1.9, for all command and methods its printing the the same output mentioned below.

#!/usr/bin/env ruby
require 'ruby-asterisk'

@ami = RubyAsterisk::AMI.new("192.168.1.5",5038)
@ami.login("admin","passs")

puts @ami.command("sip show peers")

Output:
#<RubyAsterisk::Response:0x000000016af710>

Sip Peers Better parsing

Not sure if you want to do this but can you parse the sip peers call. I just want to have an array of hashes. For me I'm most interested in the "PeerEntry" event.

  def parse_sip_show_peers(response)
    start = response.index("\nEvent: ")+1
    data = []
    res = response[(start)..(response.size-1)]
    res = res.split("\n\n").collect{|line| line.split("\n")}
    res.each do |user_array|
      d_user = {}
      user_array.each do |attribute|
        a = attribute.split(":")
        d_user[a[0].downcase] = a[1].strip
      end
      data.push d_user if d_user["event"] == "PeerEntry"
    end

    data
  end

Also it would be even better if you could pass user status that you'd want in

  def sip_peers(options={:status=>["OK"]})
    peers = parse_sip_show_peers(@ami.sip_peers.response)

    # by default get online users
    if options[:status]
      peers = peers.delete_if do |peer| 
        !options[:status].collect{|s| peer["status"].include? s }.include? true
      end
    end

    peers
  end

core_show_channels method return nil

Environment:
~# ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [x86_64-linux]
~# gem -v
1.8.15
~# gem list -l ruby-asterisk
ruby-asterisk (0.1.0)
Code:
@ami = RubyAsterisk::AMI.new( "host", port )
@ami.login( "login", "password" )
core_show_channels = @ami.core_show_channels
Error:
can't convert nil into String
Trace:
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/string/output_safety.rb:34:in `concat_without_safety'
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.5/lib/active_support/core_ext/string/output_safety.rb:34:in `<<'
/usr/lib/ruby/gems/1.8/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk.rb:146:in `execute'
/usr/lib/ruby/1.8/net/telnet.rb:598:in `waitfor'
/usr/lib/ruby/gems/1.8/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk.rb:145:in `execute'
/usr/lib/ruby/gems/1.8/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk.rb:49:in `core_show_channels'

I'm just change line line 145 in lib/ruby-asterisk.rb from:

request.response_data << data

to:

request.response_data << data.to_s

And all works fine when core_show_channels returns nil, but I don't know why this error occurs.

no implicit conversion of Fixnum into String (TypeError)

This is a example of my code:

require 'ruby-asterisk'

@ami = RubyAsterisk::AMI.new('192.XXX.X.XXX', 5038)
@ami.login('myusername', 'mypassword')

@ami.queue_add(1001, 'PJSIP/100', '')

When I executed it, got the following error:

/var/lib/gems/2.3.0/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk/request.rb:15:in `+': no implicit conversion of Fixnum into String (TypeError)
        from /var/lib/gems/2.3.0/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk/request.rb:15:in `block in commands'
        from /var/lib/gems/2.3.0/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk/request.rb:14:in `each'
        from /var/lib/gems/2.3.0/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk/request.rb:14:in `commands'
        from /var/lib/gems/2.3.0/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk.rb:139:in `execute'
        from /var/lib/gems/2.3.0/gems/ruby-asterisk-0.1.0/lib/ruby-asterisk.rb:93:in `queue_add'
        from prueba_asterisk.rb:42:in `<main>'

My quick fix was modify the line number 15 in ruby-asterisk/request.rb
commands<<key+": "+value+"\r\n" unless value.nil?
by
_commands<<"#{key.to_s}: #{value.to_s}\r\n" unless value.nil?

now the fiile looks like this:

module RubyAsterisk
  class Request
    attr_accessor :action, :action_id, :parameters, :response_data

    def initialize(action,parameters={})
      self.action = action
      self.action_id = self.generate_action_id
      self.parameters = parameters
      self.response_data = ""
    end

    def commands
      _commands=["Action: "+self.action+"\r\n","ActionID: "+self.action_id+"\r\n"]
      self.parameters.each do |key,value|
        #commands<<key+": "+value+"\r\n" unless value.nil?
        _commands<<"#{key.to_s}: #{value.to_s}\r\n" unless value.nil?
      end
      _commands[_commands.length-1]<<"\r\n"
      _commands
    end 

Now the function is working fine

NameError on a controller

hi all,

i'm newbie to rails and used last releas 5.0.4 install gem is allready ok without problem, but when execute the command to connect to asterdsik this error happens:

NameError (uninitialized constant ConfigsController::RubyAsterisk)

ConfigsController is the controller where execute this command,

please help me

Can't login

@ami.login("xxx","xxx") NoMethodError: undefined method `write' for nil:NilClass from /Users/zven/.rvm/gems/ruby-2.0.0-p247/gems/ruby-asterisk-0.0.7/lib/ruby-asterisk.rb:43:in`block in login' from /Users/zven/.rvm/gems/ruby-2.0.0-p247/gems/ruby-asterisk-0.0.7/lib/ruby-asterisk.rb:42:in `each' from /Users/zven/.rvm/gems/ruby-2.0.0-p247/gems/ruby-asterisk-0.0.7/lib/ruby-asterisk.rb:42:in`login'

Sip Peers Command

Is there a specific configuration I have to change to run the cli command sip show peers?

this works great on server

sudo asterisk -r
sip show peers

but i can't run this via RubyAsterisk.. any ideas?

@ami = RubyAsterisk::AMI.new(ip,5038)
@ami.login("user","pass")

@ami.command("SIPpeers")
=> #<RubyAsterisk::Response:0x18800bd0 @message=nil, @data=nil, @type="Command", @success=false, @action_id="502">

@ami.command("sip show peers")
=> #<RubyAsterisk::Response:0x4694bc86 @message=nil, @data=nil, @type="Command", @success=false, @action_id="647">

Thanks

Mike

How do you deal with inconsistent responses for queue_status

Have you used your Gem to query Asterisk's Queues?

I found that Asterisk returns really inconsistent results in response to queue_status. E.g. sometimes it returns

Response: Success
ActionID: 199
Message: Queue status will follow

If I query queue_status again, it may return the whole output including "QueueMembers". I have found Asterisk does this across the board, but in production I use "Asterisk 1.8.11.0".

I've written some ugly code that:

  • checks that the response contains /Response: Success/ && /QueueStatusComplete/
  • loops over queue_status until the response has been identical 3 times in a row.

How did you solve that problem and are you interested in a PR that attempts to tackle this issue?

Bug w/ jruby 1.6.8

jruby 1.6.8 fails with this method

def queue_add(queue, exten, penalty=2, paused=false, member_name)

      execute "QueueAdd", {"Queue" => queue, "Interface" => exten, "Penalty" => penalty, "Paused" => paused, "MemberName" => member_name}

end

image

image

SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2 Protocol mismatch.

Hi
I am trying to connect to remote asterisk server

@ami = RubyAsterisk::AMI.new('104.131.40.44', 22)
@ami.connect
=> true

All is ok, but any first action catch this error:

RubyAsterisk::Response:0x007fd4fd7cb170 @raw_response="SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2\nProtocol mismatch.\n", @type="Ping", @action_id=nil, @message=nil, @DaTa="SSH-2.0-OpenSSH_6.6.1p1 Ubuntu-2ubuntu2\nProtocol mismatch.\n"


@ami.ping

For example Action: Ping, as you can see.

How I can fix "Protocol mismatch" in SSH?

Regards

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.