我首先通过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");
Run Code Online (Sandbox Code Playgroud)
然后在我的主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();
Run Code Online (Sandbox Code Playgroud)
我在手机上收到了这条消息.但是,我无法取回OAuthCallback中保存的用户名和新用户会话数据.
我怀疑这种情况正在发生,因为主动消息没有设置conversationId.而且对话必须以某种方式有所不同.
那么如何在未来的对话中将会话数据设置为我的主动消息呢?
小智 1
在主动式场景中,当用户回复您的消息时,频道的对话 ID 会发生变化,就像一个新会话,我们使用频道数据来实现此类功能,但此解决方案仅适用于小数据,您也可以选择创建使用与机器人框架用于保存对话上下文相同的表存储的持久会话,在此解决方案中,您可以创建另一个表来存储序列化的数据,最后一个是使用 Redis 等分布式缓存的持久会话,但是此类服务价格昂贵,因此您必须分析哪种类型的解决方案最适合您的解决方案,但作为开始,您应该尝试使用通道数据属性,如果它有效,您可以分析另一种方法
我希望我对你有所帮助