如何在一次通话中多次使用<Say>?

问题描述 投票:0回答:1

如何在一次通话中多次使用

<Say>
?我为我的问题想出了一个例子:

exports.handler = async function (context, event, callback) {
  try {
    const twiml = new Twilio.twiml.VoiceResponse();
    const client = context.getTwilioClient();
    console.log("The call is " + JSON.stringify(event.CallSid));
    var i = 0;
    const sentences = ['Ahoy there', 'How are you today?', 'Nice to meet you', 'Hope you have a great day', 'Goodbye'];

    const intervalId = setInterval(async () => {
      if (i < sentences.length) {
        const resp = await client.calls(event.CallSid).update({
          twiml: `<Response><Say>${sentences[i]}</Say></Response>`
        });
        i += 1;
      } else {
        clearInterval(intervalId);
        return callback(null, twiml);
      }
    }, 3000); // update every 3 seconds
  } catch (err) {
    console.error(err);
  }
};

这里有一组四句话,我想每三秒钟分别说一次。我正在关注更新文档这里

但是,这似乎不起作用。它只会导致 Twilio 超时。有没有一种方法可以做到这一点,而不必每次都递归地返回回调并重定向?

twilio twilio-api twilio-twiml
1个回答
0
投票

尝试构建包含所有

<Say>
的单个 TwiML 指令,并使用
<Pause>
引入句子之间的延迟。

exports.handler = async function (context, event, callback) {
 try {
    const twiml = new Twilio.twiml.VoiceResponse();
    const client = context.getTwilioClient();
    console.log("The call is " + JSON.stringify(event.CallSid));
    const sentences = ['Ahoy there', 'How are you today?', 'Nice to meet you', 'Hope you have a great day', 'Goodbye'];

    // Create a single TwiML instruction with all the <Say> tags and <Pause> tags
    let twimlInstruction = '<Response>';
    for (let sentence of sentences) {
      twimlInstruction += `<Say>${sentence}</Say><Pause length="3"/>`;
    }
    twimlInstruction += '</Response>';

    const resp = await client.calls(event.CallSid).update({
      twiml: twimlInstruction
    });
    return callback(null, twiml);
 } catch (err) {
    console.error(err);
 }
};
© www.soinside.com 2019 - 2024. All rights reserved.