Python API in Dialplan

Hi,

I am calling a python scrip in dialplan however getting below error.
– Executing [6656531@phones:6] AGI(“SIP/serverAWS1-00000133”, “polly.py”) in new stack
– Launched AGI Script /var/lib/asterisk/agi-bin/polly.py
polly.py: Failed to execute ‘/var/lib/asterisk/agi-bin/polly.py’: Exec format error

Dialplan is below.

exten => 6656531,1,Answer()
same => n,playback(greet)
same => n,AGI(/var/www/html/tts/polly.py d “Hi Good morning”)
same => n,AGI(polly.py, d “Hi Good Morning”)
same => n,AGI(/var/www/html/tts/polly.py)
same => n,agi(polly.py)
same => n,agi,polly.py

The error is in your python script, not the dialplan.

“”"
*@author: Pardeep Kumar pardeep19@gmail.com
*@since: 27-12-2017 13:18
*@created By: IntelliJ IDEA
*@copyright: All right reserved.
"""
import datetime
import os

import sys
from boto3 import Session

speech_dir = ‘’

try:
print(“scrartestart”)
print (sys.argv[2])
leadid = sys.argv[1]
text = sys.argv[2]
region = 'ap-south-1’
accesskey = 'AKIAJMYZ5QDTX3UMUWYA’
secretkey = ‘wjOoRqJ2+V5togzAqnmm56oZFTWFxh6BSx87EmOL’

session = Session(region_name=region, aws_access_key_id=accesskey, aws_secret_access_key=secretkey)
polly = session.client("polly")

speech = "<speak>" + text + "</speak>"

response = polly.synthesize_speech(Text=speech, VoiceId="Raveena", OutputFormat="mp3", TextType="ssml")
print('=======response=========')
print(response)
stream = response.get("AudioStream")
if stream:
    data = stream.read()
    name = str(leadid) + '-' + ".wav"
    filename = os.path.join(speech_dir, name)
    filename = filename.replace(" ", "")
    print('========file name =========')
    print(filename)
    with open(filename, 'wb') as f:
        f.write(data)
        print("File Saved")
        f.close()

except Exception, e:
print "Unexpected error save_polly_speech:"
print (e)

if i run that scrip direct from cli its working properly…?

There is no “#!” line, as the first line, specifying the interpreter.

1 Like

Thanks a lot David, can you please help me with below points.

  1. Do i need to place polly.ph under /var/lib/asterisk/agi-bin only
  2. If I call the same from CLI: python polly.py ptst “Hi Swapnil very good morning” it works
  3. while executing the same through dialplan getting below error
    – Executing [6656531@phones:6] AGI(“SIP/serverAWS1-00000133”, “polly.py”) in new stack
    – Launched AGI Script /var/lib/asterisk/agi-bin/polly.py
    polly.py: Failed to execute ‘/var/lib/asterisk/agi-bin/polly.py’: Exec format error
  1. No
  2. You are running Python, not the script.
  3. Reason already given.
1 Like

See https://en.wikipedia.org/wiki/Shebang_(Unix)

1 Like