【问题标题】:Send param with POST to Botframework (and different channel)将带有 POST 的参数发送到 Botframework(和不同的通道)
【发布时间】:2019-11-21 17:12:45
【问题描述】:

我正在开发机器人项目,机器人将在不同的渠道(网络/信使和可能其他)上工作

我实际上是在主动消息,我们想向用户发送动态消息,例如“你从 XXX 时间开始不跟我说话”

所以我在 bot 中创建了一条新路由,用于发送带有对话引用的消息,目前它在模拟器/信使上运行良好,但我们尝试向此请求添加参数,但我们没有找到任何方法在机器人中获取参数。

server.post('/api/notify/:conversationID', async (req, res) => {

console.log(req)

if (req.params.conversationID){
    console.log(req.params.conversationID)
}


for (let conversationReference of Object.values(conversationReferences)) {

   if (typeof conversationReferences[req.params.conversationID] !== "undefined"){
        await adapter.continueConversation(conversationReferences[req.params.conversationID], async turnContext => {
            await turnContext.sendActivity(req.params.message);


        });
   }else {
    await adapter.continueConversation(conversationReference, async turnContext => {
        await turnContext.sendActivity(req.params.message);
    });
   }



}

res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.write('<html><body><h1>Test send.</h1></body></html>');
res.end();
});

我也尝试过使用 GET,并在 /api/notify/CONVID/MESSAGEtoUSER 等 URL 中发送参数 但是如果消息长度超过 70 个字符,则机器人自动返回方法不存在,就像长度“大”时一样,但理解为路由而不是参数......

有人知道我们如何获得参数吗?

谢谢!

编辑:

最后我找到了一种将参数作为 POST 调用传递的方法。

需要开启restify的bodyParser,添加这一行:

server.use(restify.plugins.bodyParser())

在 index.js 中

您现在可以获取 POST 路由调用的正文了! :-)

【问题讨论】:

    标签: botframework


    【解决方案1】:

    您可以通过channelData 属性在空活动中传递任何参数来实现此目的。因为活动在text 属性中包含一个空字符串,所以活动在传递给机器人时不会显示。

    在本例中,主动消息是从浏览器发起的。

    server.get('/api/notify/:userId', async (req, res) => {
      const { userId } = req.params;
      for (const conversationReference of Object.values(conversationReferences)) {
        await adapter.continueConversation(conversationReference, async turnContext => {
          var reply = { type: ActivityTypes.Message };
          reply.channelData = { userId };
          reply.text = '';
    
          await turnContext.sendActivity(reply);
        });
      }
    
      res.setHeader('Content-Type', 'text/html');
      res.writeHead(200);
      res.write('<html><body><h1>Proactive messages have been sent.</h1></body></html>');
      res.end();
    });
    

    通过channelData发送userId的主动消息

    机器人通过activity.channelData接收userId

    测试网络聊天还会在activity.channelData 中显示userId

    希望有帮助!

    【讨论】:

    • 您好,谢谢您的回复! :-) 这样,我面临着其他问题。我正在尝试将参数中的消息内容发送到。但是如果参数的长度太长,api返回我“RessourceNotFound”并使用类似路由示例的参数:我调用/api/notify/userId/Blablablablablabla ...(超过50/70个字符),它返回我“ /api/notify/userId/Blablablablablabla 不存在” 大小或参数有限制?这就是为什么我试图通过 post param aha 来实现
    • 为避免这种情况,请传入一个带有数据的 JSON 对象并在服务器端提取它。在 URL 中传递长消息不是最佳做法。
    猜你喜欢
    • 2018-04-13
    • 1970-01-01
    • 2015-07-23
    • 2015-12-23
    • 1970-01-01
    • 2013-12-07
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    相关资源
    最近更新 更多