node.js - 使用 twilio nodejs 创建调查
问题描述
我想要实现的是用 twilio 进行一项调查,但它没有像它假设的样板那样工作,但它是从 7 年前开始的。
https://github.com/TwilioDevEd/survey-node
它应该做的是响应呼入电话然后进行调查,我认为它应该使用递归函数,但作为7年的代码,整理起来有点复杂。
这是我的代码,但不起作用。
const VoiceResponse = require("twilio").twiml.VoiceResponse;
const dotenv = require("dotenv");
dotenv.config();
const _ = require("lodash");
const config = require("../auxiliary/config");
const { pool, CronJob, twilioClient } = require("../header");
const queryDB = require("../utils/query-db");
const survey = [
{
text: "Please tell us your age.",
type: "number"
},
{
text: "Have you ever jump-kicked a lemur?",
type: "number"
},
{
text: "Who is your favorite Teenage Mutant Ninja Turtle and why?",
type: "number"
}
];
const getAppointments = async () => {
let userData;
const query = `select c.start, p.Phone, p.Name, p.Lastname, u.PName, u.PLastname, u.phone as doctorPhone, u.id as doctorId, ivr.* from IVRTentativeAppointmentConfirmation ivr join Flow f on ivr.idFlow = f.idflow and f.state = 4 join Patients p on f.idPatient = p.id join Calendar c on c.idFlow = ivr.idFlow and c.timestamp < date(now() - interval 14 day) join Users u on u.id = c.idProvider where c.start > date(now() + interval 14 day) and isConfirmed =0 `;
userData = await queryDB({
pool,
query
});
userData = JSON.parse(JSON.stringify(userData));
const doctorList = _.groupBy(userData, "doctorId");
Object.values(doctorList).forEach(appointments => {
let listOfAppointments = [];
const doctorLastName = appointments[0].PLastname;
const appointmentsLength = appointments.length;
const doctorPhoneNumber = appointments[0].doctorPhone;
const params = {
to: doctorPhoneNumber,
from: config.twilio.mobile,
url: `https://handler.twilio.com/twiml/EH6694503RWcdndfe3Rd?doctorLastName=${doctorLastName}&appointmentsLength=${appointmentsLength}`
};
placeCall(params);
Object.values(appointments).forEach(val2 => {
return listOfAppointments.push(val2.start);
});
//get all info from list of patients
});
};
const placeCall = params => {
//Place a phone call, and respond with TwiML instructions from the given URL
twilioClient.calls
.create(params)
.then(call => {})
.catch(err => {
console.log(err);
});
};
exports.interview = (req, res) => {
const twiml = new VoiceResponse();
const questionIndex = 0;
const say = text => {
twiml.say({ voice: "alice" }, text);
};
const respond = () => {
res.type("text/xml");
res.send(twiml.toString());
};
const question = survey[questionIndex];
/* if (err || !surveyResponse) {
say('Terribly sorry, but an error has occurred. Goodbye.');
return respond();
} */
if (!question) {
say("Thank you for confirm your pending appointments. Goodbye");
return respond();
}
if (questionIndex === 0) {
say("Please listen carefully " + "to the following appointment dates");
}
say(question.text);
};
const advanceSurvey = (args, cb)=> {
const surveyData = args.survey;
let surveyResponse;
const processInput=()=> {
let responseLength = 5
const currentQuestion = surveyData[responseLength];
// if there's a problem with the input, we can re-ask the same question
const reask=()=> {
cb.call(surveyResponse, null, surveyResponse, responseLength);
}
// If we have no input, ask the current question again
if (input === undefined) return reask();
// Otherwise use the input to answer the current question
let questionResponse = {};
if (currentQuestion.type === 'number') {
// Try and cast to a Number
var num = Number(input);
if (isNaN(num)) {
// don't update the survey response, return the same question
return reask();
} else {
questionResponse.answer = num;
}
}
// Save type from question
questionResponse.type = currentQuestion.type;
surveyResponse.responses.push(questionResponse);
// If new responses length is the length of survey, mark as done
if (surveyResponse.responses.length === surveyData.length) {
surveyResponse.complete = true;
}
cb.call(surveyResponse, err, surveyResponse, responseLength+1);
}
};
getAppointments();
new CronJob(
"*/60 * * * *",
function() {
return getAppointments();
},
null,
true,
"UTC"
);
希望有人遇到同样的问题并可以指导我。
谢谢
解决方案
推荐阅读
- java - 使用 BouncyCastle 生成 PBE-MD5-DES 加密密钥以与 OpenSSL pkcs8 兼容
- java - 在 android、firebase 中获取聊天应用程序的正确时间戳
- string - 从 PDF 中提取特定的文本字符串
- node.js - docker compose环境变量在nodejs容器中未定义
- linux - 替换 Shell 脚本中特定范围的 xml 属性值
- c++ - 我把一个shared_ptr放到了map中,但是为什么程序结束前对象就被破坏了呢?
- laravel - Laravel-Excel 从 v2 升级到 v3。缺少 NewExcelFile 的问题
- javascript - 如何在 Javascript 中合并对象?
- java - 如何修复有关在 Java 中格式化日期的错误?
- asp.net - 下拉列表数据库