You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用TypeScript+Node.js对接WhatsApp云API时Process函数调用报错排查

WhatsApp云API 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)

核心代码片段

receiveMessage控制器函数

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");
      }
}

GetTextUser消息提取函数

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;
}

Process回复生成函数

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);
        });

}

排查步骤与解决方案

1. 校验导入导出匹配性

报错提示sendWhatsapp_1.Process不是函数,先确认receiveMessage所在文件对Process的导入逻辑:

  • 若Process是命名导出(如代码中export function Process),必须用import { Process } from './sendWhatsapp'导入,不能使用默认导入语法;
  • 检查导入路径的拼写和层级是否正确,避免路径错误导致模块未正确加载。

2. 排查循环依赖

如果receiveMessage所在控制器文件与Process所在的sendWhatsapp文件存在互相导入的情况,会导致模块加载时Process未完成初始化:

  • 拆分公共逻辑到独立文件,消除双向导入;
  • 改用动态导入延迟加载Process,比如将调用代码改为:
    import('./sendWhatsapp').then(({ Process }) => Process(text, cell));
    

3. 修复代码语法错误

代码中存在多处语法和变量拼写错误,会导致TypeScript编译异常,间接引发Process导出失败:

  • GetTextUser函数:
    • 将messageType、writeMessage统一改为定义好的typeMessage;
    • constant data改为const data;
    • opening_greeting替换为已定义的saludo_inicial;
    • 移除函数内未定义的res.json(error),改为抛出异常或其他错误处理逻辑;
    • 将text.includes('Help, it's not my invoice.')改为text.includes("Help, it's not my invoice.")解决引号冲突。
  • Process函数:
    • 将函数内未定义的cell、cell phone统一替换为参数cellPhone;
    • 修复textUser.includes('Help, it's not my invoice.')的引号冲突问题。

4. 检查编译后文件

查看报错指向的/app/dist/src/controller/whatsapp_test.js第56行,确认编译后的Process调用代码是否正常,是否存在导出被覆盖或绑定异常的情况。

内容的提问来源于stack exchange,提问作者Izlia

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 23:18:33