簡體   English   中英

本地電腦 python Z319C3206A7F10C957C464FZ twilio API 通話后如何保存入站通話錄音

[英]How will I save inbound call recording after the call from twilio API in the local pc python flask

我使用示例代碼接收來自號碼的呼叫到 twilio 號碼。 現在我需要將錄音保存為 mp3。 我不明白該怎么做。 我試圖調用各種參數但失敗了。 我是 twilio 的新手。

> `from flask import Flask
from twilio.twiml.voice_response import VoiceResponse

app = Flask(__name__)


@app.route("/record", methods=['GET', 'POST'])
def record():
    """Returns TwiML which prompts the caller to record a message"""
    # Start our TwiML response
    response = VoiceResponse()

    # Use <Say> to give the caller some instructions
    response.say('Hello. Please leave a message after the beep.')

    # Use <Record> to record the caller's message
    response.record()

    # End the call with <Hangup>
    response.hangup()

    return str(response)

def record(response):
    # function to save file to .wav format
    

if __name__ == "__main__":
    app.run(debug = True)

我點擊了鏈接,但不明白如何將它與 flask 鏈接以保存文件。 https://www.twilio.com/docs/voice/api/recording?code-sample=code-filter-recordings-with-range-match&code-language=Python&code-sdk-version=6.x

Twilio 開發人員布道師在這里。

當您使用<Record>記錄用戶時,您可以提供 URL 作為recordingStatusCallback屬性 然后,當錄制准備好時,Twilio 將向該 URL 發送有關錄制的詳細信息的請求。

因此,您可以將您的 TwiML record更新為以下內容:

    # Use <Record> to record the caller's message
    response.record(
        recording_status_callback="/recording-complete",
        recording_status_callback_event="completed"
    )

然后,您將需要一個新的/recording-complete路由,您可以在其中接收回調並下載文件。 有一篇關於如何下載文件以響應 webhook的好帖子,但它涵蓋了 MMS 消息。 但是,我們可以從那里學到的東西來下載錄音。

首先,安裝並導入requests 還從 Flask 導入request

import requests
from flask import Flask, request

然后,創建/recording-complete端點。 我們將從請求中讀取記錄 URL。 您可以在文檔中查看所有請求參數 然后我們將使用錄音 SID 作為文件名打開一個文件,使用 requests 下載錄音並將錄音的內容寫入文件。 然后我們可以用一個空的<Response/>來響應。

@app.route("/recording-complete", methods=['GET', 'POST'])
def recording_complete():
    response = VoiceResponse()

    # The recording url will return a wav file by default, or an mp3 if you add .mp3
    recording_url = request.values['RecordingUrl'] + '.mp3'

    filename = request.values['RecordingSid'] + '.mp3'
    with open('{}/{}'.format("directory/to/download/to", filename), 'wb') as f:
        f.write(requests.get(recording_url).content)

    return str(resp)

讓我知道你是怎么做到的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM