4

I want to pickup call in Asterisk using AMI. I can originate call, but totally don't know, how to answer the phone... Script for calling:

#login
sock = socket.socket(af, socktype, proto)
sock.connect(sockaddr)
sock.send('Action: login\r\n')
sock.send('Events: off\r\n')
sock.send('Username: '+str(ast_server.login)+'\r\n')
sock.send('Secret: '+str(ast_server.password)+'\r\n\r\n')

#originate call
sock.send('Action: originate\r\n')
sock.send('Channel: ' + str(user.asterisk_chan_type) + '/' + str(user.internal_number)+'\r\n')
sock.send('Timeout: '+str(ast_server.wait_time*1000)+'\r\n')
sock.send('CallerId: '+str(user.callerid)+'\r\n')
sock.send('Exten: '+str(ast_number)+'\r\n')
sock.send('Context: '+str(ast_server.context)+'\r\n')
if ast_server.alert_info and user.asterisk_chan_type == 'SIP':
    sock.send('Variable: SIPAddHeader=Alert-Info: '+str(ast_server.alert_info)+'\r\n')
sock.send('Priority: '+str(ast_server.extension_priority)+'\r\n\r\n')

#logout
sock.send('Action: Logoff\r\n\r\n')
time.sleep(1)
sock.close()

I need something similar, but for answering calls. Can't find any useful command in *CLI> manager show command

Halp me, plox

voy
  • 1,568
  • 2
  • 17
  • 28

1 Answers1

12

You can't answer a call directly via AMI. This is because a new call will "arrive" at the given context/priority/extension configured in the dialplan (or it will be rejected if cant find one that applies). So whatever happens with that call will start at the given context/priority/extension in the dialplan.

If you want to handle calls via AMI, try using asynchronous AGI, like this:

exten => _X.,1,AGI(agi:async)

This will handle all calls to any extension that has at least 1 digit, by issuing an event (AsyncAGI) that you can handle with your AMI client.

Then, from your AMI client, you can send AGIAction's, like:

Action: AGI
Channel: SIP/adevice
Command: ANSWER
CommandID: MyCommandID

This will effectively allow you to run AGI commands (and handle a call like you would normally do in any AGI script) from your AMI client.

Hope it helps!

marcelog
  • 7,062
  • 1
  • 33
  • 46
  • I'm sure you're right, but I don't know, where to put _exten => _X.,1,AGI(agi:async)_. I'm a python programmer, not Asterisk guru : – voy Jun 04 '12 at 10:48
  • Hi! The dialplan is in your extensions.conf file, see: https://wiki.asterisk.org/wiki/display/AST/Dialplan+Fundamentals and http://www.voip-info.org/wiki/view/Asterisk+config+extensions.conf. You might want to create a special context for your tests, and after you modified it, remember to issue a "dialplan reload" in the cli or restart asterisk so the changes take effect – marcelog Jun 04 '12 at 10:53
  • I know where are dialplans. For example I use from-internal dialplan. Where to paste _exten => (blah)_? Maybe it's doesn't matter? Thank You for wasting your time for me :) – voy Jun 04 '12 at 11:11
  • Thats what I want to do: [schema](http://share.qwerty.yum.pl/asterisk_pickup.png) – voy Jun 04 '12 at 11:31
  • Your extensions.conf file is divided into sections, called "contexts". In this case, you say (or I'm guessing :)), you are using the context "from-internal". So put those lines into that context (that is, anywhere between [from-internal] and the next [section]). If this is a production box, I suggest you to not modified it, but try it on a dev box so you dont break anything. The lines I wrote in the answer will apply to incoming calls to an extension with at least 1 digit, so you might want to modified it to apply to a specific not-in-use extension, like 555 – marcelog Jun 04 '12 at 12:13
  • Btw, beware that the context might change, depending on where the call comes from, so it's recommended for you to get comfortable with configuring asterisk, or at least have someone near that can solve these matters for you. We're kind of deviating from the original question here and it's hard to explain all of this in a comment :) – marcelog Jun 04 '12 at 12:21
  • Screw you, Asterisk. Better I'll try solve my problem with python sip library. What I had made: [CLICK](http://share.qwerty.yum.pl/trying.png) – voy Jun 04 '12 at 13:41
  • Are you sending the action AGI with the command ANSWER (as posted in the answer) for that channel? This is needed when using async agi " for anything to happen". Async agi works, I've implemented it in PAMI (php) without any trouble. Another option would be to just use normal AGI instead of async AGI. Good luck! – marcelog Jun 04 '12 at 13:54
  • AGI command is exactly like in your answer. Thank You again :) – voy Jun 04 '12 at 14:20
  • Sure! Glad to be of help :). If the answer was useful, dont forget to mark it as answered, by clicking at the "correct" sign, at the left of the answer. Cheers! – marcelog Jun 04 '12 at 14:30