Skip to content

Instantly share code, notes, and snippets.

@mwchambers
Created October 26, 2011 11:20
Show Gist options
  • Save mwchambers/1316071 to your computer and use it in GitHub Desktop.
Save mwchambers/1316071 to your computer and use it in GitHub Desktop.
Asterisk Manager Interface - Originate
class AMISession
# http://www.voip-info.org/wiki/view/Asterisk+manager+Example:+Originate
# http://www.the-asterisk-book.com/unstable/asterisk-manager-api.html
def initialize(host, port, user, secret)
begin
@s = TCPSocket.open("127.0.0.1", 5038)
rescue Errno::ECONNREFUSED => e
puts "\n\n* Established a tunnel to asterisk AMI?\n ssh -L 5038:localhost:5038 root@192.168.1.10\n\n"
throw e
end
puts @s.gets
login(user, secret)
end
def read_response
begin
line = @s.gets
puts line
end while line != "\r\n"
end
def originate(from, to)
@s.puts("Action: Originate")
@s.puts("Channel: SIP/#{from}")
@s.puts("Exten: #{to}")
@s.puts("Priority: 1")
@s.puts("Timeout: 60000")
@s.puts("Context: from-internal")
@s.puts("CallerID: #{from}")
#s.puts("Async: true")
@s.puts("")
read_response
end
def command(command)
@s.puts("Action: Command")
@s.puts("Command: #{command}")
@s.puts("")
read_response
end
def destroy
@s.close
end
private
def login(username, secret)
@s.puts("Action: Login")
@s.puts("ActionID: 1")
@s.puts("Username: #{username}")
@s.puts("Events: off")
@s.puts("Secret: #{secret}")
@s.puts("")
read_response
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment