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

Twilio Functions中client未初始化,创建WhatsApp群组失败报错

问题

参考TwilioDevEd的WhatsApp群组消息示例项目,在Twilio Functions中运行createConversation.js代码时,访问函数URL出现以下错误:

UnhandledPromiseRejectionWarning: Unhandled promise rejection: TypeError: Cannot read property 'conversations' of undefined

推测是示例代码第5行创建client失败导致,但不确定具体原因。

运行的代码

exports.handler = async function(context, event, callback) {
  const numbers = ['YOUR_PHONE_NUMBER', 'YOUR_FRIENDS_PHONE_NUMBER'];
  const client = context.getTwilioClient();

  const conversation = await client.conversations.conversations.create();

  numbers.forEach(async (number) => {

    const newParticipant = await client.conversations.conversations(conversation.sid).participants.create({
      'messagingBinding.address': `whatsapp:${number}`,
      'messagingBinding.proxyAddress': `whatsapp:${process.env.WHATSAPP_NUMBER}`
    });

    const message = await client.messages.create({
      'to': `whatsapp:${number}`,
      'from': `whatsapp:${process.env.WHATSAPP_NUMBER}`,
      'body': ' Sam has invited you to join a group conversation. *Please tap the button below to confirm your participation.*'
    });
  });

  return callback(null, conversation.sid);
};

已确认的配置

  • 环境变量中已配置TWILIO_SERVICE_SID和SYNC_MAP_SID,格式正确(ISXXX和MPXXX)
  • 已勾选“Add my Twilio Credentials (ACCOUNT_SID) and (AUTH_TOKEN) to ENV”选项
  • 未修改默认依赖项
  • 已将numbers数组替换为'+14151234567'格式的手机号

错误成因与解决方法

核心原因

Twilio Functions默认的Twilio Node.js SDK版本可能过低,不包含conversations模块,导致client.conversations未定义。本地环境能运行是因为本地安装的SDK版本支持Conversations API,而Functions默认依赖的版本不兼容。

解决步骤

  1. 升级Twilio SDK依赖
    在Twilio Functions的函数设置页面,找到「Dependencies」区域,添加指定版本的Twilio SDK(建议用支持Conversations API的最新稳定版):

    {
      "twilio": "^4.0.0"
    }
    

    同时调整代码的API调用格式(v4版本SDK的路径变化):

    • 原client.conversations.conversations.create()改为client.conversations.v1.conversations.create()
    • 原client.conversations.conversations(conversation.sid).participants.create()改为client.conversations.v1.conversations(conversation.sid).participants.create()
  2. 修复异步循环问题
    原代码用forEach搭配async/await会导致循环内Promise无法被正确等待,引发未处理的Promise拒绝。替换为for...of循环:

    for (const number of numbers) {
      const newParticipant = await client.conversations.v1.conversations(conversation.sid).participants.create({
        'messagingBinding.address': `whatsapp:${number}`,
        'messagingBinding.proxyAddress': `whatsapp:${process.env.WHATSAPP_NUMBER}`
      });
    
      const message = await client.messages.create({
        'to': `whatsapp:${number}`,
        'from': `whatsapp:${process.env.WHATSAPP_NUMBER}`,
        'body': ' Sam has invited you to join a group conversation. *Please tap the button below to confirm your participation.*'
      });
    }
    
  3. 补充缺失的环境变量
    代码中使用了process.env.WHATSAPP_NUMBER,但未在已配置列表中,需在Twilio Functions的环境变量中添加该变量,值为你的Twilio WhatsApp号码(格式+1XXXXXXXXX)。

  4. 添加错误捕获逻辑
    为避免未处理的Promise拒绝,用try/catch包裹核心逻辑:

    exports.handler = async function(context, event, callback) {
      try {
        const numbers = ['YOUR_PHONE_NUMBER', 'YOUR_FRIENDS_PHONE_NUMBER'];
        const client = context.getTwilioClient();
    
        const conversation = await client.conversations.v1.conversations.create();
    
        for (const number of numbers) {
          const newParticipant = await client.conversations.v1.conversations(conversation.sid).participants.create({
            'messagingBinding.address': `whatsapp:${number}`,
            'messagingBinding.proxyAddress': `whatsapp:${process.env.WHATSAPP_NUMBER}`
          });
    
          const message = await client.messages.create({
            'to': `whatsapp:${number}`,
            'from': `whatsapp:${process.env.WHATSAPP_NUMBER}`,
            'body': ' Sam has invited you to join a group conversation. *Please tap the button below to confirm your participation.*'
          });
        }
    
        return callback(null, conversation.sid);
      } catch (error) {
        return callback(error);
      }
    };
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 21:10:30