node.js - 当 `session` 对象不可用时,在 Bot Framework 中读取/写入机器人用户数据

标签 node.js bots botframework

我想在 Microsoft Bot Framework 提供的机器人用户数据存储中存储一个简单的键值对。

通常很容易做到:

session.userData.key = value;

但是,当 session 对象不可用时,我想在处理所有传入消息的事件处理程序中执行此操作:

bot.on('incoming', incoming => {
  // Check whether user asked to switch on debug mode.
  if (incoming.text === 'debug on') {
    console.log('Enabling debug mode.');
    // TODO FIXME - we need to save the result to bot memory.
  }
});

是否有其他方法可以从主 app.js 文件或事件处理程序保存到机器人内存?

最佳答案

我找到了解决方案,那就是使用UniversalBot.loadSession():

bot.on('incoming', incoming => {
  // Load the user session so that we can save values to userData.
  bot.loadSession(incoming.address, (error, session) => {
    if (!error) {
      // work with the session object as usual
    }
  });
});

加载 session 对象后,您可以照常使用 session.send()session.userData

关于node.js - 当 `session` 对象不可用时,在 Bot Framework 中读取/写入机器人用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46784862/

相关文章:

python - discord.py bot 找到要删除的 channel 消息,但显示其客户端 ID 与我的相同

c# - 在bot框架v4中,如何实现带有评论框和提交按钮的评分卡

c# - 从 Azure 应用服务到 Azure VM 上托管的 API 的 API 调用 --错误 尝试以访问权限禁止的方式访问套接字

mysql - Node/Bluebird/MySQL 事务

node.js - Babel 6.x + Webpack 2 + React 不支持装饰器

c# - 如何使用 C# 在 Web 嵌入式 bot 中的 bot 框架中发送附件

bots - 如何将 Telegram channel 的成员转移到另一个 Telegram channel

c# - 机器人框架: 'State size exceeded configured limit.'

javascript - 如何在 Angular 2/4+ 和 Socket.io 中关闭 websocket

multithreading - 掌握 Node JS 的多线程替代方案