[英]How to retrieve information from outbound Twilio call with Python and put it into google speech to text?
[英]How to retrieve information from outbound Twilio call with Python?
我是 Twilio 的新手,正在嘗試弄清楚如何從我使用 Python 3 成功進行的出站呼叫中檢索數據。我希望能夠檢索諸如從收件人那里按下了什么按鈕之類的內容。
在閱讀了一點 Twilio 文檔(然后有點迷失)后,我想我明白了 Twilio 的工作原理以及為什么我無法從電話中檢索數據。 我認為 Python 程序只是建立了從 Twilio 到電話號碼的連接。 收件人可以撥打任何號碼,我可以使用標簽獲取一些信息。 但是我如何將該信息定向到我的 Python 程序呢? 這個想法是讓 Twilio(以某種方式)將信息發送回我的 Python 程序,然后我可以采取行動(例如更新數據庫)。
我猜 Twilio 會將數據扔到其他地方,然后我的 Python 程序可以去檢索,但我不知道從哪里學習該技能。 我有 Python 3 的基本基礎,但在 Web 開發方面沒有很多。 只是一些基本的 HTML5 和 CSS3。
Twilio 開發人員布道者在這里。
您可能已經看過有關在入站呼叫中通過 Python中的鍵盤收集用戶輸入的文檔。
當您接到入站呼叫時,Twilio 會發出 webhook 請求以了解下一步要做什么,並且您使用 TwiML 進行響應,例如,當您想要獲取信息時使用<Gather>
。
當您進行出站呼叫時,您使用 REST API 發起呼叫,然后當呼叫連接時,Twilio 會向您的 URL 發出 Webhook 請求。 然后,您可以使用 TwiML 進行響應以告訴 Twilio 要做什么,並且您也可以在此階段使用<Gather>
進行響應。
首先,您購買 Twilio 電話號碼並使用Ngrok URL 對其進行配置:這是一個方便的工具,可通過公共 URL 將您的本地服務器打開到網絡。 當您撥打出站電話時,您會將這個 URL 傳遞給它: your-ngrok-url.ngrok.io/voice
。
from twilio.rest import Client
account_sid = 'your-account-sid'
auth_token = 'your-auth-token'
client = Client(account_sid, auth_token)
call = client.calls.create(
url='https://your-ngrok-url.ngrok.io/voice',
to='phone-number-to-call',
from_='your-twilio-number'
)
client.calls.create
的 URL 返回 TwiML,其中包含有關用戶接聽電話時應該發生的情況的說明。 讓我們創建一個 Flask 應用程序,其中包含在用戶接聽電話時運行的代碼。
from flask import Flask, request
from twilio.twiml.voice_response import VoiceResponse, Gather
app = Flask(__name__)
@app.route("/voice", methods=['GET', 'POST'])
def voice():
# Start a TwiML response
resp = VoiceResponse()
您將通過帶有 TwiML Gather動詞的鍵盤接收用戶輸入,該動詞用於在電話通話期間收集數字或轉錄語音。 Action 屬性將絕對或相對 URL 作為值,一旦調用者完成輸入數字(或達到超時),Twilio 就會向其發出 HTTP 請求。 該請求包括用戶的數據和 Twilio 的標准請求參數。
如果您從呼叫者那里收集數字,Twilio 會包含Digits
參數,其中包含呼叫者輸入的數字。
gather = Gather(num_digits=1, action='/gather')
gather.say('For sales, press 1. For support, press 2.')
resp.append(gather)
如果收件人沒有選擇選項,讓我們將它們循環回到開頭,以便他們再次聽到方向。
resp.redirect('/voice')
return str(resp)
但是,如果他們確實選擇了一個選項並在鍵盤中輸入了一個數字,Twilio 將使用他們輸入的數字向托管您的 TwiML 的 URL 發送一個 POST 請求。 這就是您通過按鈕從接收者那里獲得用戶輸入並將其引導回您的 Python 程序的方式:使用request.values['Digits']
。 基於該值(在choice
變量中,您可以相應地更新數據庫或其他內容,如下面的條件所示。
@app.route('/gather', methods=['GET', 'POST'])
def gather():
"""Processes results from the <Gather> prompt in /voice"""
# Start TwiML response
resp = VoiceResponse()
# If Twilio's request to our app included already gathered digits,
# process them
if 'Digits' in request.values:
# Get which digit the caller chose
choice = request.values['Digits']
# <Say> a different message depending on the caller's choice
if choice == '1':
resp.say('You selected sales. Good for you!')
return str(resp)
elif choice == '2':
resp.say('You need support. We will help!')
return str(resp)
else:
# If the caller didn't choose 1 or 2, apologize and ask them again
resp.say("Sorry, I don't understand that choice.")
# If the user didn't choose 1 or 2 (or anything), send them back to /voice
resp.redirect('/voice')
return str(resp)
希望這可以幫助!
@lizziepika - 非常有指導意義 - 非常感謝!
我正在嘗試做一個實時轉錄系統,該系統在 twilio 號碼和用戶之間的 twilio 對話中使用語音到文本和文本到語音。
input=speech
twiml gather
並獲取您在回答中提到的request.values
,則需要給出 2 秒的間隔來識別用戶已停止說話並等待 twilio 響應。 問題在於,只有在這 2 秒后,系統才能開始處理來自用戶的響應。 我相信這是一個非常延遲的響應。這是我應該從上面選擇的建議選項,以便在 twilio 和用戶之間進行接近正常的對話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.