NodeJS 使用TWILIO在同一个呼叫中使用聚集动词获得多个语音响应

flmtquvp  于 2024-01-07  发布在  Node.js
关注(0)|答案(1)|浏览(192)

概述

我正试图使多个问题的回答将通过语音提供的调查,并已被转录为文本,并发送到一个webhook,为以后的处理.

问题

所有3个问题都被复制,但是一旦其中一个被回答,调用就结束了,只有1个响应被发送到webhook,而不是所有3个
我在JS中尝试了以下代码

  1. const audioFiles = [
  2. { url: 'https://twilioaudiohcp.s3.us-east-2.amazonaws.com/1.mp3', question: 'Q1' },
  3. { url: 'https://twilioaudiohcp.s3.us-east-2.amazonaws.com/2.mp3', question: 'Q2' },
  4. { url: 'https://twilioaudiohcp.s3.us-east-2.amazonaws.com/3.mp3', question: 'Q3' },
  5. ];
  6. const webhookEndpoint = 'webhook';
  7. const phoneNumberList = JSON.parse(fs.readFileSync('test.json', 'utf8')).map(item => item.celular);
  8. const makeCalls = async () => {
  9. let callCount = 0;
  10. for (const phoneNumber of phoneNumberList) {
  11. if (callCount >= 40) {
  12. await new Promise(resolve => setTimeout(resolve, 60000));
  13. callCount = 0;
  14. }
  15. const response = new twilio.twiml.VoiceResponse();
  16. for (const audioFile of audioFiles) {
  17. // Play the audio
  18. response.play(audioFile.url);
  19. response.say(audioFile.question, { voice: 'alice', language: 'es-MX' });
  20. response.gather({
  21. input: 'speech',
  22. language: 'es-MX',
  23. action: webhookEndpoint,
  24. speechtimeout: 1,
  25. method: 'POST',
  26. });
  27. response.pause({ length: 1 });
  28. }
  29. const twiml = response.toString();
  30. await client.calls.create({
  31. twiml: twiml,
  32. from: fromPhoneNumber,
  33. to: `+52${phoneNumber}`,
  34. record: true,
  35. transcribe: true,
  36. language: 'es-MX',
  37. })
  38. .then(call => {
  39. console.log('Call initiated:', call.sid);
  40. callCount++;
  41. })
  42. .catch(error => {
  43. console.error('Failed to make the call:', error);
  44. });
  45. }
  46. };
  47. makeCalls()
  48. .then(() => console.log('All calls completed.'))
  49. .catch(error => console.error('An error occurred:', error));

字符串

4szc88ey

4szc88ey1#

为了实现一个多问题的调查,其中每个回答都被转录并发送到您的webhook,您需要将问题链接起来,以便顺序提问,而不是在同一个调用中同时提问。
在您当前的设置中,所有问题都在一个TwiML响应中被询问,因此,当第一个<Gather>接收到输入时,它会处理该输入并结束,而无需等待其他响应。
要解决这个问题,请尝试使用<Gather>动词的action属性,该属性允许您指定一个URL,一旦收到<Gather>输入,Twilio将向该URL发送数据。然后您可以使用TwiML响应该webhook以询问下一个问题。
下面是一个概念性的例子,说明如何设置TwiML应用程序,使其一次只问一个问题:
1.询问第一个问题,并使用action URL指向一个路由,您将在该路由中处理响应并询问后续问题。
1.当呼叫者回答第一个问题时,Twilio会将收集到的语音输入发送到action URL。在服务器端的该路由中,您将处理第一个答案,然后返回TwiML来询问第二个问题。
1.对调查中的每个问题重复此过程。

相关问题