也许你可以在这里帮助我,我正在用typescript和nodejs express制作我的API,我正在使用 meta实现whatsapp api云应用。
我按照官方文档的要求配置了一个webhook,我不打算深入了解文档,因为关于它有很多要说的,但我将向您展示我在代码中所做的工作
我实际上是用 metaAPI对webhook请求进行授权的,我有一个端点,它是一条消息,监听我的服务器和whatsapp之间的通信。一切都很好,从我的服务器向whatsapp发送消息,也从whatsapp向我的服务器发送消息。但是当我想模拟一个bot进行条件交互响应时,我不能这样做。用户在聊天中按下按钮后,我无法使服务器响应
它给了我一个有点模糊的打字错误
error TypeError: (0 , sendWhatsapp_1.Process) is not a function
at /app/dist/src/controller/whatsapp_test.js:56:44
at Generator.next (<anonymous>)
at fulfilled (/app/dist/src/controller/whatsapp_test.js:5:58)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
我调查并阅读了一些关于它可能是什么的资料,我读到它可能是一个创建循环依赖关系的问题。
总结一下我的代码的功能如下:
我有自己的控制器调用webhook,这里可以这么说,我异步解析GetText()函数,该函数负责在用户单击按钮时侦听用户触发的操作
export const receiveMessage = async(req: Request, res: Response) => {
try{
console.log("start");
let input = (req.body["input"])[0];
console.log('input', input);
let changes = (input["changes"])[0];
console.log("changes", changes);
let value = changes["value"];
console.log("value", value);
let messageObject = value["messages"];
console.log("messageObject 1", messageObject);
if (typeof messageObject !== "undefined") {
let messages = messageObject[0];
let cell = messages["from"];
console.log('messages', input);
console.log('number', cell);
console.log("stop");
console.log('messageobject 2', messages);
let text = await GetTextUser(messages, cell);
console.log("Interactive text", text);
if(text !== "") {
Process (text, cell);
}
} else {
console.log('no message');
}
res.send("RECEIVED_EVENT");
} catch(e) {
console.log('error', e);
res.send("RECEIVED_EVENT");
}
}
如果文本包含某些参数,则使用nodemailer发送电子邮件并将其存储在数据库中。
async function GetTextUser(messages: any, cell: any) {
let text: string = "";
let typeMessage = messages["type"];
if (messageType === "text") {
text = (messages["text"])["body"];
} else if (messageType === "interactive") {
let interactiveObject = messages["interactive"];
let interactiveType = interactiveObject["type"];
console.log('interactiveObject', interactiveObject);
if (interactiveType === "response_button") {
text = (interactiveObject["button_reply"])["title"];
} else if (interactiveType === "answer_list") {
text = (interactiveObject["button_reply"])["title"];
} else {
console.log("no message");
}
} else if (writeMessage === "button") {
let buttonObject = messages["button"];
console.log("It is not the user's invoice");
text = buttonObject["text"];
console.log('text include', text.includes('Help, this is not my invoice.'));
console.log('text', text);
if (text.includes('Help, it's not my invoice.')) {
let supabase = supabaseinstance();
const contextId = (messages["context"])["id"];
console.log('contextId', contextId);
const { data: historyMsgWsp, error } = await supabase.from("history_mgs_whatsapp").select('*').eq("wa_id", contextId as string);
if (error) {
res.json (error)
} else {
console.log('historyMsgWsp DB', historyMsgWsp);
const typeHistoryMesg: TypeHistoryWspTest[] = historyMsgWsp;
const emailService = new EmailService();
let ticket = typeHistoryMesg.map(dataFromDB => {
let saludo_inicial = "Dear SAC Collaborator,";
let main_paragraph = `A support ticket has been generated and assigned from commerce ${dataFromDB.commerce_name}. Contact the client: ${dataFromDB.client_name} as soon as possible`;
let main_paragraph2 = "Request Details";
let main_paragraph3 = [`<strong>Customer Contact Cell:</strong> +${dataFromDB.client_cellphone}`, `<strong>Commerce Name:</strong> ${dataFromDB.commerce_name}`,
`<strong>NoInvoice:</strong> ${dataFromDB.invoice_code}`, `<strong>Date of message sent:</strong> ${dataFromDB.date_sending_message}`];
constant data = {
id: dataFromDB.id,
wa_id: dataFromDB.wa_id,
opening_p: opening_greeting,
main_paragraph: main_paragraph,
main_paragraph2: main_paragraph2,
main_paragraph3: main_paragraph3,
subjectData: `Problems with Help is not my invoice`,
fileUrl: "link"
}
return emailService.prepareEmail("TEST_TICKET", data, "mail");
});
console.log('email left', ticket);
}
}
} else {
console.log("no message");
}
return text;
}
返回receiveMessage控制器接收的文本,解析后将其传递给process方法
let text = await GetTextUser(messages, cell);
console.log("Interactive text", text);
if(text !== "") {
Process (text, cell);
}
错误进入Process方法,该方法负责在我们从GetTextUser()获得响应后向用户发送响应。在process方法中,依次处理消息类型,我们将在https库的帮助下将其发送给用户。
export function Process(textUser: any, cellPhone: any) {
console.log('Process user text', textUser);
console.log('Process mobile phone', cellPhone);
textUser = textUser.toLowerCase();
let models: any = [];
if (textUser.includes('Help, it's not my invoice.')) {
console.log("Help, it's not my invoice.");
let model = MessageText("We have generated a support ticket to help you, we will contact you as soon as possible", cell);
models.push(model);
} else if (textUser.includes("hello") || textUser.includes("help") || textUser.includes("help me") || textUser.includes("please") || textUser.includes(" please")) {
console.log("greeting");
let model = MessageText("Hi, nice to say hello. 👋 We won't be able to answer any questions or clarifications about claims here. Please contact email for any help.", cell phone);
models.push(model);
} else if (textUser.includes("thank you")) {
// gratitude
console.log("thank you");
let model = MessageText("Thank you for writing me. 😉😎", cell);
models.push(model);
} else if (textUser.includes("bye") ||
textUser.includes("bye") ||
textUser.includes("bye") ||
textUser.includes("I'm leaving")
) {
console.log("fire");
// desperate
let model = MessageText("Watch out. 😊", cell);
models.push(model);
}
else {
console.log("I don't understand");
// I don't understand
let model = MessageText("I don't understand what you're saying. We won't be able to answer questions or clarifications about claims here. Contact info@cit.hn for any help.", cell phone);
models.push(model);
}
models.forEach(model => {
console.log('what is the model', model);
SendMessageWhatsApp(model);
});
}
代码在某种程度上是连续的和有条件的,所以我从我的服务器发送一条消息到whatsapp,用户按下一个按钮,当它是带有条件文本的按钮时,发送邮件的服务被触发。在此之后,文本的返回立即传递给Process方法,该方法处理GetTextUser返回的字符串。它会根据文本内容进行评估,并根据文本将响应发送回whatsapp
我试了一整天,我不知道我做错了什么
1条答案
按热度按时间tkclm6bt1#
好吧,这个“问题”比我想象的要容易解决,最后只是需要导出Process方法,在这里找到了解决方案和说明Solution