我正在尝试调用 agi 脚本,而对于我使用的外部程序,我使用了 python。 我使用 pyst2 python 库来支持 agi。 一切正常,但我使用 pyst2 的 set_variable 方法将一个从 python 程序返回的 python 变量设置为 asterisk cli。 首先查看日志,然后查看我的python脚本和拨号计划。
yash-VirtualBox*CLI> core set verbose 4
Console verbose is still 4.
-- Added contact 'sip:1102@192.168.1.3:43527;transport=UDP;rinstance=740fe480853796c5' to AOR '1102' with expiration of 60 seconds
== Contact 1102/sip:1102@192.168.1.3: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 脚本: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)
我的拨号方案: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,Playback(beep)
exten => 1102,NoOp(Answered,beep done)
exten => 1102,Record(/home/yash/Documents/data/record.wav,7)
exten => 1102,Playback(/home/yash/Documents/data/record)
exten => 1102,AGI(/home/yash/Documents/environments/STT_agi.py,)
exten => 1102,NoOp(${OUTPUT})
exten => 1102,NoOp(Record & Playback done)
exten => 1102,hangup()
注意:我为我的 python 脚本创建了一个虚拟环境,所有依赖项都存储在一个文件夹中 (/home/yash/Documents/environments/test_env)。
正如您在日志输出中看到的详细输出: