I am trying to call agi script and for an external program I used python.
I used pyst2 python library for agi support.
Everything works fine but I set one python variable which I returned from python program to asterisk cli using set_variable method of pyst2.
First look at logs then check my python script and dialplan.
yash-VirtualBox*CLI> core set verbose 4
Console verbose is still 4.
-- Added contact 'sip:[email protected]:43527;transport=UDP;rinstance=740fe480853796c5' to AOR '1102' with expiration of 60 seconds
== Contact 1102/sip:[email protected]:43527;transport=UDP;rinstance=740fe480853796c5 has been deleted
== Endpoint 1102 is now Unreachable
== Endpoint 1102 is now Reachable
-- Executing [1102@Long-Distance:1] Answer("PJSIP/1101-00000000", "") in new stack
> 0x7f874c03ced0 -- Strict RTP learning after remote address set to: 103.241.224.35:6334
> 0x7f874c03ced0 -- Strict RTP qualifying stream type: audio
> 0x7f874c03ced0 -- Strict RTP switching source address to 192.168.1.2:7078
-- Executing [1102@Long-Distance:2] Playback("PJSIP/1101-00000000", "hello, how can I help you?") in new stack
-- <PJSIP/1101-00000000> Playing 'hello.gsm' (language 'en')
-- Executing [1102@Long-Distance:3] Playback("PJSIP/1101-00000000", "beep") in new stack
-- <PJSIP/1101-00000000> Playing 'beep.gsm' (language 'en')
-- Executing [1102@Long-Distance:4] NoOp("PJSIP/1101-00000000", "Answered,Playback,beep done") in new stack
-- Executing [1102@Long-Distance:5] Record("PJSIP/1101-00000000", "/home/yash/Documents/data/record.wav,,7") in new stack
-- <PJSIP/1101-00000000> Playing 'beep.gsm' (language 'en')
> 0x7f874c03ced0 -- Strict RTP learning complete - Locking on source address 192.168.1.2:7078
-- Executing [1102@Long-Distance:6] Playback("PJSIP/1101-00000000", "/home/yash/Documents/data/record") in new stack
-- <PJSIP/1101-00000000> Playing '/home/yash/Documents/data/record.slin' (language 'en')
-- Executing [1102@Long-Distance:7] AGI("PJSIP/1101-00000000", "/home/yash/Documents/environments/STT_agi.py, , ") in new stack
-- Launched AGI Script /home/yash/Documents/environments/STT_agi.py
<PJSIP/1101-00000000>AGI Tx >> agi_request: /home/yash/Documents/environments/STT_agi.py
<PJSIP/1101-00000000>AGI Tx >> agi_channel: PJSIP/1101-00000000
<PJSIP/1101-00000000>AGI Tx >> agi_language: en
<PJSIP/1101-00000000>AGI Tx >> agi_type: PJSIP
<PJSIP/1101-00000000>AGI Tx >> agi_uniqueid: 1611744743.0
<PJSIP/1101-00000000>AGI Tx >> agi_version: 16.16.0
<PJSIP/1101-00000000>AGI Tx >> agi_callerid: 1101
<PJSIP/1101-00000000>AGI Tx >> agi_calleridname: Maria Berny
<PJSIP/1101-00000000>AGI Tx >> agi_callingpres: 0
<PJSIP/1101-00000000>AGI Tx >> agi_callingani2: 0
<PJSIP/1101-00000000>AGI Tx >> agi_callington: 0
<PJSIP/1101-00000000>AGI Tx >> agi_callingtns: 0
<PJSIP/1101-00000000>AGI Tx >> agi_dnid: 1102
<PJSIP/1101-00000000>AGI Tx >> agi_rdnis: unknown
<PJSIP/1101-00000000>AGI Tx >> agi_context: Long-Distance
<PJSIP/1101-00000000>AGI Tx >> agi_extension: 1102
<PJSIP/1101-00000000>AGI Tx >> agi_priority: 7
<PJSIP/1101-00000000>AGI Tx >> agi_enhanced: 0.0
<PJSIP/1101-00000000>AGI Tx >> agi_accountcode:
<PJSIP/1101-00000000>AGI Tx >> agi_threadid: 140221026883328
<PJSIP/1101-00000000>AGI Tx >> agi_arg_1:
<PJSIP/1101-00000000>AGI Tx >> agi_arg_2:
<PJSIP/1101-00000000>AGI Tx >>
<PJSIP/1101-00000000>AGI Rx << VERBOSE "python agi started" 1
/home/yash/Documents/environments/STT_agi.py, , : python agi started
<PJSIP/1101-00000000>AGI Tx >> 200 result=1
-- <PJSIP/1101-00000000>AGI Script /home/yash/Documents/environments/STT_agi.py completed, returning 0
-- Executing [1102@Long-Distance:8] NoOp("PJSIP/1101-00000000", "") in new stack
-- Executing [1102@Long-Distance:9] NoOp("PJSIP/1101-00000000", "Record & Playback done") in new stack
-- Executing [1102@Long-Distance:10] Hangup("PJSIP/1101-00000000", "") in new stack
== Spawn extension (Long-Distance, 1102, 10) exited non-zero on 'PJSIP/1101-00000000'
python script: STT_agi.py
#!/home/yash/Documents/environments/test_env/bin/python
import io
import os
import json
from asterisk.agi import *
agi = AGI()
agi.verbose("python agi started")
# Imports the Google Cloud client library
from google.cloud import speech
#from google.cloud.speech import enums
#from google.cloud.speech import types
from google.oauth2 import service_account
# Instantiates a client
client = speech.SpeechClient()
# The name of the audio file to transcribe
'''file_name = os.path.join(
os.path.dirname(__file__),
'resources',
'audio.raw')'''
file_name = '//home//yash//Documents//data//record.wav'
# Loads the audio into memory
with io.open(file_name, 'rb') as audio_file:
content = audio_file.read()
audio = speech.RecognitionAudio(content=content)
config = speech.RecognitionConfig(
encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,
sample_rate_hertz=8000,
language_code='en-IN')
# Detects speech in the audio file
response = client.recognize(request={"config":config, "audio":audio})
result_json = response.__class__.to_json(response)
result_dict = json.loads(result_json)
#print(type(response))
#print(response)
'''
for result in response.results:
print('Transcript: {}'.format(result.alternatives[0].transcript))'''
output = result_dict.get('results')[0].get('alternatives')[0].get('transcript')
agi.set_variable("OUTPUT", output)
#print(type(output))
#print(output)
# Instantiates a client
my_credentials = service_account.Credentials.from_service_account_file('//home//yash//Documents//voice-bot-302507-3b6ca3522f51.json')
client = speech.SpeechClient(credentials=my_credentials)
my dialplan: extensions.conf
[globals]
; General internal dialing options used in context Dial-Users.
; Only the timeout is defined here. See the Dial app documentation for
; additional options.
INTERNAL_DIAL_OPT=,30
[Hints]
; Allow dynamic hint creation for every extension.
exten = _11XX,hint,PJSIP/${EXTEN}
[Long-Distance]
exten => 1102,1,Answer()
exten => 1102,n,Playback(hello, how can I help you?)
exten => 1102,n,Playback(beep)
exten => 1102,n,NoOp(Answered,Playback,beep done)
exten => 1102,n,Record(/home/yash/Documents/data/record.wav,,7)
exten => 1102,n,Playback(/home/yash/Documents/data/record)
exten => 1102,n,AGI(/home/yash/Documents/environments/STT_agi.py, , )
exten => 1102,n,NoOp(${OUTPUT})
exten => 1102,n,NoOp(Record & Playback done)
exten => 1102,n,hangup()
Note: I have created a virtual environment for my python script and all dependencies are store in one folder (/home/yash/Documents/environments/test_env).
As you can see in logs output verbose output : <PJSIP/1101-00000000>AGI Rx << VERBOSE "python agi started" 1 which obviously print using the help of pyst2 python asterisk library. but why my variable output is not returned from python program to asterisk cli.
question from:
https://stackoverflow.com/questions/65917969/why-python-variable-is-not-return-print-on-asterisk-cli