2
votes

I'm currently working on a project 'email to voice call'. Using python i'v extracted the email & converted it into speech and saved in a WAV file. Now using asterisk (I'v installed Asterisk 10.2.1 on my ubuntu 10.10 os) i want to generate call to the cell phone (say 919833000000 india's no.) of the user through my system.

I have written a python code to connect to asterisk manager interface. Also i have configured the sip.conf and extensions.conf files as well as manager.conf. I have registered with voip provider voiceall.com and have a username password of it.

Now when i'm executing the python code, the code is getting executed without any error but nothing is happening. No call is getting generated. Can anyone help me out with this. The python code is as below:

import sys, os, socket, random
# Asterisk Manager connection details
HOST = '127.0.0.1'
PORT = 5038
# Asterisk Manager username and password
USER = 'MYUSERNAME'
SECRET = 'MYPASSWORD'
# Set the name of the SIP trunk to use for outbound calls
TRUNK = 'voiceall'



OUTBOUND = '919833000000'

# Send the call details to the Asteirsk Manager Interface
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
sleep(3)
s.send('Action: login\r\n')
s.send('Username: ' + USER + '\r\n')
s.send('Secret: ' + SECRET + '\r\n\r\n')
sleep(3)
s.send('Action: status\r\n')
data = s.recv(1024)
print data + '\n'
s.send('Events: off\r\n\r\n')
sleep(3)
s.send('Action: originate\r\n')
s.send('Channel: Sip/' + TRUNK + '/' + OUTBOUND + '\r\n')
s.send('WaitTime: 30\r\n')
s.send('CallerId: VOICEALL_USERNAME\r\n')
s.send('Application: playback\r\n')
s.send('Data: /home/Documents/newdemo1' + '\r\n')  #newdemo1 is the wave file
s.send('Context: testing\r\n')
s.send('Async: true\r\n')
s.send('Priority: 1\r\n\r\n')
sleep(10)
s.send('Action: Logoff\r\n\r\n')
s.close()

My sip.conf file is as below:

[general]                
register => VOICEALL_USERNAME:VOICEALL_PASSWORD@sip.voiceall.net:5038

[voiceall]
canreinvite=no
context=mycontext
host=sip.voiceall.net
secret=VOICEALL_PASSWORD ;your password
type=peer
username=VOICEALL_USERNAME ;your account
disallow=all
allow=ulaw

fromuser=VOICEALL_USERNAME ;your account
trustrpid=yes
sendrpid=yes
insecure=invite
nat=yes

The extensions.conf file is as below:

[mycontext]

include => voiceall-outbound

[voiceall-outbound]
exten => _1NXXNXXXXXX,1,Dial(SIP/${EXTEN}@voiceall)
exten => _1NXXNXXXXXX,n,Playback(/home/Documents/demonew1)
exten => _1NXXNXXXXXX,n,Hangup()
exten => _NXXNXXXXXX,1,Dial(SIP/1${EXTEN}@voiceall)
exten => _NXXNXXXXXX,n,Playback(/home/Documents/demonew1)
exten => _NXXNXXXXXX,n,Hangup()
exten => _011.,1,Dial(SIP/${EXTEN}@voiceall)
exten => _011.,n,Playback(/home/Documents/demonew1)
exten => _011.,n,Hangup()

Please help me out, as i am new to asterisk. Any help will be appreciated. Thanks in advance.

1

1 Answers

2
votes

A few things to note before I comment on your originate command:

  1. A python wrapper library for AMI over TCP already exists: starpy. You may want to check that out, as it will reduce the amount of re-inventing the wheel you'll need to do in order to get yourself up and running.

  2. In general, you probably shouldn't use separate TCP sends for each parameter in an AMI action. Instead, you should send each message separately. In general, starpy handles this quite well by treating each AMI action as a dictionary of key/value pairs, which obviously maps quite well to AMI's syntax. If you decide not to use starpy, you may still want to see how they map generic dictionaries to AMI actions.

  3. Do you have the appropriate classes of authentication for your user in manager.conf? In order to originate a call, your user will need the originate class on the write authorization

In your origination, you're attempting to dial SIP/voiceall/919833000000. That implies that Asterisk will create a SIP channel and start its pbx_thread at context voiceall-outbound (because its the default context for the peer you specified), at the extension matching what you specified.

First, your extension that you're attempting to place is 919833000000. You don't have an extension that matches this - the closest you have is _NXXNXXXXXX. This specifies that its a pattern match, that the first character must be a number [2-9], the next two characters are numbers [0-9], the fourth character is a number [2-9], and the next six characters are numbers [0-9], for a total of 10 characters. The extension you're specifying is 12 characters. I would expect the originate to fail.

Let's assume that the originate did succeed, and you had a pattern match extension with the same applications as _NXXNXXXXXX. A channel that you're originating is going to be tied to something else - either another context,extension,priority in the dialplan, or an application. In your case, you've specified both: an application to connect to (Playback), with data you're passing to it, as well as a context and a priority (but no extension). That's not valid. From Asterisk's 'manager show command originate':

Exten Extension to use (requires 'Context' and 'Priority') Context Context to use (requires 'Exten' and 'Priority') Priority Priority to use (requires 'Exten' and 'Context')

In general, you don't combine Application/Data with Context,Exten,Priority. The Application/Data option exists when you want to only do very simple actions with what you dial - in your example, playing back a message fits the bill, but in general, I prefer to connect it to something else in the dialplan so I have more control. I'll assume you want to use Context,Exten,Priority - in which case, you could do something like the following:

[testing]
exten => internal_playback,1,NoOp()
same => n,Playback(/home/Documents/demonew1)
same => n,Hangup()

[voiceall-outbound]
exten => _9XXXXX000000,1,NoOp()
same => n,Dial(SIP/${EXTEN}@voiceall)
same => n,Hangup()

Your originate would then look something like this:

Action: Originate
Channel: local/internal_playback@testing
Context: voiceall-outbound
Exten: 919833000000
Priority: 1

You'll note that we're no longer using a SIP channel to call into the dialplan. You could still use a SIP channel in your originate - its really just a personal preference here. The Dial application will already create a SIP channel for us and bridge it to whatever we originate, so we just use a local channel. For our local channel, we connect one end to our internal playback extension that we use to play sound out to the dialed party, and the other end to our outbound extension.