bell-ami 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +2 -0
- data/bell-ami.gemspec +25 -0
- data/bin/bell_ami +6 -0
- data/lib/bell/ami/caller.rb +104 -0
- data/lib/bell/ami/cli.rb +74 -0
- data/lib/bell/ami/stream.rb +72 -0
- data/lib/bell/ami/version.rb +5 -0
- data/lib/bell/ami.rb +9 -0
- data/test/run_sample.rb +39 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: eea60547cb3bd23bad911aec18f335020123614d
|
4
|
+
data.tar.gz: 77f0cf870903b6db084093968eaba1b1fc6fb529
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8bbc289babb031f182a90c454d91121f0d3c35498a0249680303db50a61cac76a5521ad067d9d27e3f9815ce1f6240820e4a0c979f91e9084b8ce7858b2ef626
|
7
|
+
data.tar.gz: 4e781f83dae750c96c1b8dbe1d7cb435fc7a28087aeb6b3ccd0f0213808965df6c8c95e97af6e0e1a0e02bcbaa9167f4d2583ab5b0cca768f5fb6b8d57fc475b
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Hossein Bukhamseen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Bell::Ami
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'bell-ami'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install bell-ami
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/[my-github-username]/bell-ami/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bell-ami.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'bell/ami/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "bell-ami"
|
8
|
+
spec.version = Bell::Ami::VERSION
|
9
|
+
spec.authors = ["Hossein Bukhamseen"]
|
10
|
+
spec.email = ["hossein@hungerstation.com"]
|
11
|
+
spec.summary = "Event Handler and command line utility for Asterisk Manager Api"
|
12
|
+
spec.description = "This gem is a simple wrapper around ruby_ami."
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
spec.add_dependency 'ruby_ami'
|
21
|
+
spec.add_dependency 'thor'
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/bin/bell_ami
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Bell::Ami
|
4
|
+
class Caller
|
5
|
+
attr_accessor :number, :ami_stream, :status, :call_id, :call_uniqueid, :options
|
6
|
+
|
7
|
+
def initialize(number, ami_stream,options={})
|
8
|
+
@number=number
|
9
|
+
@ami_stream=ami_stream
|
10
|
+
@ami_stream.register_handler(self)
|
11
|
+
if @ami_stream.ami_stream.started?
|
12
|
+
@status='connected'
|
13
|
+
else
|
14
|
+
@status='initialized'
|
15
|
+
end
|
16
|
+
@call_id=SecureRandom.uuid
|
17
|
+
@call_uniqueid=nil
|
18
|
+
|
19
|
+
@options={'Priority'=> '1', 'Timeout'=> '30000', 'MaxRetries'=>'0', 'RetryTime'=>'15', 'WaitTime'=> '45', }
|
20
|
+
@options.merge!(options)
|
21
|
+
end
|
22
|
+
|
23
|
+
def handle_event(event)
|
24
|
+
case event.name
|
25
|
+
when 'FullyBooted'
|
26
|
+
@status='ready'
|
27
|
+
when 'Hangup'
|
28
|
+
if event['Uniqueid']==@call_uniqueid
|
29
|
+
if @status=='in_call'
|
30
|
+
@status='succeeded'
|
31
|
+
else
|
32
|
+
case event['Cause']
|
33
|
+
when '19'#User alerting, no answer
|
34
|
+
@status='not_answered'
|
35
|
+
when '17'#User busy
|
36
|
+
@status='busy'
|
37
|
+
when '16'#Normal Clearing
|
38
|
+
@status='succeeded'
|
39
|
+
else
|
40
|
+
@status='not_available'
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
when 'NewCallerid'
|
45
|
+
if event['CallerIDName']==@call_id
|
46
|
+
@call_uniqueid=event['Uniqueid']
|
47
|
+
end
|
48
|
+
when 'Newexten'
|
49
|
+
if event['Uniqueid']==@call_uniqueid
|
50
|
+
if event['Application']=='Answer'
|
51
|
+
@status='in_call'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
when 'DTMF'
|
55
|
+
if event['Uniqueid']==@call_uniqueid
|
56
|
+
if event['End']=='Yes'
|
57
|
+
STDOUT.puts event['Digit']
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def handle_disconnected
|
64
|
+
if @status='dialing'
|
65
|
+
@status='errored'
|
66
|
+
else
|
67
|
+
@status='errored'
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def handle_connected
|
72
|
+
@status='connected'
|
73
|
+
end
|
74
|
+
|
75
|
+
def dial(context, extention)
|
76
|
+
@status='dialing'
|
77
|
+
begin
|
78
|
+
caller_id=%Q["#{@call_id}" <1000>]
|
79
|
+
result=@ami_stream.ami_stream.send_action('Originate',@options.merge({"Channel"=>@number, 'Context'=> context, 'Exten'=> extention, 'Callerid': caller_id }))
|
80
|
+
rescue RubyAMI::Error => e
|
81
|
+
if @status=='dialing'
|
82
|
+
@status='failed'
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def terminate
|
88
|
+
|
89
|
+
end
|
90
|
+
|
91
|
+
def dialed?
|
92
|
+
return @status=='dialing'
|
93
|
+
end
|
94
|
+
|
95
|
+
def finished?
|
96
|
+
return @status=='errored' || @status=='failed' || @status=='succeeded' || @status=='not_answered' || @status=='busy' || @status=='not_available'
|
97
|
+
end
|
98
|
+
|
99
|
+
def ready?
|
100
|
+
return @status=='ready'
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
data/lib/bell/ami/cli.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
require 'thor'
|
2
|
+
require 'bell/ami/caller'
|
3
|
+
|
4
|
+
module Bell::Ami
|
5
|
+
class Cli < Thor
|
6
|
+
def self.exit_on_failure?
|
7
|
+
true
|
8
|
+
end
|
9
|
+
|
10
|
+
desc "call PHONENUMBER", "Call a phone number"
|
11
|
+
long_desc <<-STARTDOC
|
12
|
+
|
13
|
+
`call PHONENUMBER` will try to call the phone number.
|
14
|
+
|
15
|
+
call command will use asterisk manager api to originate a call.
|
16
|
+
Exit codes:
|
17
|
+
0 : Successfull call
|
18
|
+
1 : General error
|
19
|
+
2 : Asterisk Manager connection error
|
20
|
+
3 : Asterisk server error(maybe sip trunk is down)
|
21
|
+
4 : Number did not answer
|
22
|
+
5 : Number Busy
|
23
|
+
6 : Number Not available/wrong
|
24
|
+
|
25
|
+
If called number enter any key it will go to stdout.
|
26
|
+
|
27
|
+
http://stackoverflow.com/a/12785204
|
28
|
+
STARTDOC
|
29
|
+
|
30
|
+
option :host, :required => true
|
31
|
+
option :port
|
32
|
+
option :username, required: true
|
33
|
+
option :password, required: true
|
34
|
+
option :context, required: true
|
35
|
+
option :extension, required: true
|
36
|
+
def call( phonenumber )
|
37
|
+
logger=Logger.new(STDERR)
|
38
|
+
my_ami=Bell::Ami::Stream.new(options[:host],options[:port] || 5038, options[:username], options[:password], logger)
|
39
|
+
bell_caller=Bell::Ami::Caller.new(phonenumber,my_ami)
|
40
|
+
|
41
|
+
logger.info "Connecting to #{options[:host]}"
|
42
|
+
my_ami.async_run
|
43
|
+
while !bell_caller.finished?
|
44
|
+
if bell_caller.ready?
|
45
|
+
logger.info "Calling #{phonenumber}"
|
46
|
+
result=bell_caller.dial(options[:context],options[:extension])
|
47
|
+
else
|
48
|
+
# wait for call to finish
|
49
|
+
end
|
50
|
+
sleep 1
|
51
|
+
end
|
52
|
+
|
53
|
+
#terminal stream
|
54
|
+
my_ami.terminate
|
55
|
+
|
56
|
+
case bell_caller.status
|
57
|
+
when 'succeeded'
|
58
|
+
exit 0
|
59
|
+
when 'errored'
|
60
|
+
exit 2
|
61
|
+
when 'failed'
|
62
|
+
exit 3
|
63
|
+
when 'not_answered'
|
64
|
+
exit 4
|
65
|
+
when 'busy'
|
66
|
+
exit 5
|
67
|
+
when 'not_available'
|
68
|
+
exit 6
|
69
|
+
end
|
70
|
+
exit 1
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'ruby_ami'
|
2
|
+
|
3
|
+
module Bell
|
4
|
+
module Ami
|
5
|
+
class Stream
|
6
|
+
attr_accessor :host, :port, :user, :password, :ami_stream, :logger, :event_handlers
|
7
|
+
|
8
|
+
def initialize(host,port,user,password, logger)
|
9
|
+
@host=host
|
10
|
+
@port=port
|
11
|
+
@user=user
|
12
|
+
@password=password
|
13
|
+
@logger=logger
|
14
|
+
@ami_stream = RubyAMI::Stream.new @host, @port, @user, @password, lambda{|e| self.handle_event(e) }, @logger, 10
|
15
|
+
@event_handlers=[]
|
16
|
+
end
|
17
|
+
|
18
|
+
def login
|
19
|
+
@ami_stream.login
|
20
|
+
end
|
21
|
+
|
22
|
+
def handle_event(event)
|
23
|
+
if event.class.name=='RubyAMI::Stream::Disconnected'
|
24
|
+
#handle disconnection
|
25
|
+
event_handlers.each do |event_handler|
|
26
|
+
if event_handler.class.method_defined? :handle_disconnected
|
27
|
+
event_handler.handle_disconnected
|
28
|
+
end
|
29
|
+
end
|
30
|
+
elsif event.class.name=='RubyAMI::Stream::Connected'
|
31
|
+
event_handlers.each do |event_handler|
|
32
|
+
if event_handler.class.method_defined? :handle_connected
|
33
|
+
event_handler.handle_connected
|
34
|
+
end
|
35
|
+
end
|
36
|
+
elsif event.class.name=='RubyAMI::Event'
|
37
|
+
event_handlers.each do |event_handler|
|
38
|
+
if event_handler.class.method_defined? :handle_event
|
39
|
+
event_handler.handle_event(event)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def async_run
|
46
|
+
@ami_stream.async.run
|
47
|
+
end
|
48
|
+
|
49
|
+
def status
|
50
|
+
@ami_stream.send_action('Status')
|
51
|
+
end
|
52
|
+
|
53
|
+
def queue_status(queue_name)
|
54
|
+
@ami_stream.send_action('Queuestatus',{"Queue"=>queue_name })
|
55
|
+
end
|
56
|
+
|
57
|
+
def register_handler(handler)
|
58
|
+
@event_handlers << handler
|
59
|
+
end
|
60
|
+
|
61
|
+
def unregister_handler(handler)
|
62
|
+
@event_handlers.delete(handler)
|
63
|
+
end
|
64
|
+
|
65
|
+
def terminate
|
66
|
+
if @ami_stream.alive? && @ami_stream.started?
|
67
|
+
@ami_stream.send_action('LOGOFF')
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
data/lib/bell/ami.rb
ADDED
data/test/run_sample.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require_relative 'stream'
|
2
|
+
require_relative 'sse_event_handler'
|
3
|
+
|
4
|
+
logger=Logger.new(STDOUT)
|
5
|
+
|
6
|
+
sse_client=Sse::Client::Client.new
|
7
|
+
sse_client.configure do |config|
|
8
|
+
config.redis_options={url: "redis://localhost:6379"}
|
9
|
+
config.sse_namespace='hs'
|
10
|
+
config.max_queue_size=1000
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
sse_event_handler=Bell::Ami::SseEventHandler.new(sse_client,'bell/queues/support')
|
15
|
+
|
16
|
+
my_ami=Bell::Ami::Stream.new('voip.vb',5038, 'manager', '123456', logger)
|
17
|
+
my_ami.register_handler(sse_event_handler)
|
18
|
+
my_ami.async_run
|
19
|
+
|
20
|
+
|
21
|
+
i=0
|
22
|
+
while true
|
23
|
+
puts i
|
24
|
+
|
25
|
+
# my_ami.login
|
26
|
+
if my_ami.ami_stream.stopped?
|
27
|
+
puts 'Stopped'
|
28
|
+
my_ami.async_run
|
29
|
+
elsif my_ami.ami_stream.started?
|
30
|
+
puts 'started'
|
31
|
+
elsif my_ami.ami_stream.ready?
|
32
|
+
puts 'ready'
|
33
|
+
else
|
34
|
+
puts 'none'
|
35
|
+
end
|
36
|
+
|
37
|
+
i+=1
|
38
|
+
sleep 5
|
39
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bell-ami
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hossein Bukhamseen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-08 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: ruby_ami
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thor
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.6'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.6'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: This gem is a simple wrapper around ruby_ami.
|
70
|
+
email:
|
71
|
+
- hossein@hungerstation.com
|
72
|
+
executables:
|
73
|
+
- bell_ami
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- bell-ami.gemspec
|
83
|
+
- bin/bell_ami
|
84
|
+
- lib/bell/ami.rb
|
85
|
+
- lib/bell/ami/caller.rb
|
86
|
+
- lib/bell/ami/cli.rb
|
87
|
+
- lib/bell/ami/stream.rb
|
88
|
+
- lib/bell/ami/version.rb
|
89
|
+
- test/run_sample.rb
|
90
|
+
homepage: ''
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.4.5
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Event Handler and command line utility for Asterisk Manager Api
|
114
|
+
test_files:
|
115
|
+
- test/run_sample.rb
|