无法在Twilio WebSocket Python直播通话中调用Response.Say

2 投票
1 回答
38 浏览
提问于 2025-04-14 18:35

在这个 if AcceptWaveform 的地方,也就是打印语句发生的地方,我想在底部调用一个样本消息,这个消息会在通话时被说出来。

但是没有消息被调用。

不过在通话开始时,Response Say 的消息是有效的。

def stream(ws):
    rec = KaldiRecognizer(model, 16000)
    response = VoiceResponse()
    while True:
        message = ws.receive()
        packet = json.loads(message)
        if packet['event'] == 'start':
            print('Streaming is starting')
        elif packet['event'] == 'stop':
            print('\nStreaming has stopped')
        elif packet['event'] == 'media':
            audio = base64.b64decode(packet['media']['payload'])
            audio = audioop.ulaw2lin(audio, 2)
            audio = audioop.ratecv(audio, 2, 1, 8000, 16000, None)[0]
            if rec.AcceptWaveform(audio):
                r = json.loads(rec.Result())
                print(CL + r['text'] + '\n', end='', flush=True)
                response.say('Sample response message')
            else:
                r = json.loads(rec.PartialResult())
                print(CL + r['partial'] + BS * len(r['partial']), end='', flush=True)```



Expectation was that the caller should hear sample response message during call

1 个回答

1

看起来你想要对接收到的流数据返回一个TwiML响应。但这样做是行不通的,因为这只是通话的流数据。相反,你应该尝试去修改正在进行的原始通话

// Download the helper library from https://www.twilio.com/docs/node/install
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const client = require('twilio')(accountSid, authToken);

client.calls('CAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX')
      .update({twiml: '<Response><Say>Ahoy there</Say></Response>'})
      .then(call => console.log(call.to));

撰写回答