【问题标题】:MS Bot Builder: how to set session data to proactive message?MS Bot Builder:如何将会话数据设置为主动消息?
【发布时间】:2016-10-20 08:12:49
【问题描述】:

我首先通过 OAuthCallback 方法中的短信通道向用户发送主动消息

 var connector = new ConnectorClient();
 Message message = new Message();
 message.From = new ChannelAccount { Id = Constants.botId, Address = "+12312311", ChannelId = "sms", IsBot = true };
 message.To = new ChannelAccount { Id = newUserId, Address = "+18768763", ChannelId = "sms", IsBot = false };
 message.Text = $"How are you doing? ";
 message.Language = "en";
 connector.Messages.SendMessage(message);

 IBotData myDataBag = new JObjectBotData(message);

 myDataBag.UserData.SetValue("Username", "Bob");
 myDataBag.PerUserInConversationData.SetValue("Newuser", "yes");

然后在我的主 Dialog.cs 中尝试访问它

public static readonly IDialog<string> dialog = Chain
    .PostToChain()            
    .Switch(new Case<Message, IDialog<string>>((msg) =>
    {
        var regex = new Regex("hello$", RegexOptions.IgnoreCase);
        return regex.IsMatch(msg.Text);
    },
    (ctx, msg) =>
    {
        // Clearing user related data upon logout
        string isnewuser = ctx.PerUserInConversationData.TryGetValue("Newuser");
        string username = ctx.UserData.TryGetValue("Username");
        return Chain.Return($"Welcome {username}");
    }))
    .Unwrap()
    .PostToUser();

我在手机上收到消息。但是,我无法取回保存在 OAuthCallback 中的用户名和 newuser 会话数据。

我怀疑这是因为主动消息没有设置 conversationId。而且conversationId 必须有所不同。

那么我怎样才能让它在未来的对话中将会话数据设置为我的主动消息?

【问题讨论】:

  • 不是解决方案,但是如果我没记错的话,在发送主动消息时不需要设置conversationId。框架将检查是否存在。如果 conversationId 存在,它会将该消息发送到该对话,如果不存在,则开始一个新对话。 source 另外,您是通过模拟器执行此操作,还是在 Azure 中有您的机器人?我正在尝试通过模拟器发送主动消息并收到 500 状态代码。
  • @pl0x 它在模拟器中不起作用。我更想知道如何在由主动消息启动的 convo 上设置会话数据,这在这种情况下似乎不起作用。
  • 一旦我在 Azure 上安装了我的机器人,如果我知道如何设置会话数据,我会通知你。
  • 我们现在有一个sample 用于创建新对话。
  • 我做过一次,看这里github.com/DanielHWe/NotifyBot

标签: c# botframework


【解决方案1】:

在主动的场景中,当用户回复你的消息时,频道的对话ID会发生变化,就像一个新的会话,我们使用频道数据做这种类型的功能,但是这个解决方案只针对小数据,你也有使用机器人框架用于保存对话上下文的相同表存储创建持久会话的选项,在此解决方案中,您可以创建另一个表来存储序列化的数据,最后一个是使用分布式缓存的持久会话,例如Redis,但这种类型的服务很昂贵,因此您必须分析哪种类型的解决方案适合您的解决方案,但首先,您应该尝试使用 Channel Data 属性,如果有效,您可以分析另一种方法

希望对你有所帮助

【讨论】:

    【解决方案2】:

    不确定四年后这是否仍然相关,但我想我在 Access UserProfile from NotifyBot 中发现了这一点。看看吧。

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-13
      • 2017-06-09
      • 1970-01-01
      相关资源
      最近更新 更多