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

如何通过NodeJS的whatsapp-web.js向Botpress API发送自定义变量

问题

使用NodeJS的whatsapp-web.js库与Botpress发起对话时,希望初始发送对话发起者的手机号。尝试了以下代码:

client.on('message', message => {

    console.log('number', message.from);
    if( message.from == '559295105867@c.us' ){
        const number = message.from.split('@')
        const from = number[0]
        axios({
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            data: {
                type: "text",
                text: message.body,
                payload: {from}
            },
            url: "http://127.0.0.1:3000/api/v1/bots/bot-zdg/converse/552298120000"
        }).then(async res => {
            const {responses} = res.data

            for (let index = 0; index < responses.length; index++) {
                const {text, choices} = responses[index]
                let choice = ''
                if( choices ){

                    const rows = choices.map(c => `> *${c.value}*: ${c.title}`)
                    choice = rows.join('\n')
                }
                const msg = `${text}\n${choice}`
                client.sendMessage(message.from, msg)
                await delay(message.from)   
            }
        }).catch(e =>{
            console.log('error',e.response.data);
            console.log('error',e.message);
        })
    }

});

但收到错误:

error {
  statusCode: 400,
  type: 'StandardError',
  message: 'Invalid payload: "payload" is not allowed',
  details: '',
  docs: 'https://botpress.com/docs',
  showStackInDev: true,
  stack: 'Error: Invalid payload: "payload" is not allowed\n' +
    '    at /snapshot/v12/packages/bp/dist/core/converse/converse-router.js',
  full: 'Invalid payload: "payload" is not allowed'
}
error Request failed with status code 400

已知Botpress的标准Payload格式为:

{
  type: "text",
  text: "mensagem"
}

想知道是否有方法发送额外变量供Botpress读取?

解决方案

Botpress的converse API支持通过metadata字段传递自定义额外参数,这是官方允许的扩展方式,不会触发格式错误。

修改请求数据部分,把原来的payload替换成metadata:

data: {
    type: "text",
    text: message.body,
    metadata: { from: from } // 用metadata传递手机号
}

在Botpress端,可通过以下方式读取该参数:

  • 在流程钩子(Hook)中,通过event.metadata.from获取手机号
  • 在自定义代码动作里,直接访问事件对象的metadata属性

另外,也可以将手机号嵌入到请求URL的对话ID部分(即当前URL中的552298120000),但这种方式灵活性较低,仅适合固定标识场景,metadata是更通用的自定义参数传递方案。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 15:12:35