简体   繁体   English

在 Microsoft Bot Framework 的会话中保存用户信息

[英]Saving the User Info in session in Microsoft Bot Framework

I have developed chat bot with C# Need to show different greeting message , on User using the chatbot for second time with in a day.我用 C# 开发了聊天机器人需要在一天内第二次使用聊天机器人的用户上显示不同的问候消息 If user is using the chatbot for first time in a day, need to show "Hi, how can I help You" greeting message.如果用户在一天内第一次使用聊天机器人,需要显示“嗨,有什么可以帮助你”的问候信息。 second time on using chatbot need to show "Welcome back 'Username', how can I help you" message.第二次使用聊天机器人时需要显示“欢迎回来'用户名',我有什么可以帮助你的”消息。

How to achieve this?如何实现这一目标?

By default Bots are stateless.默认情况下,机器人是无状态的。 The state and storage features of the Bot Framework SDK allow you to manage the state of your bot. Bot Framework SDK 的状态和存储功能允许您管理机器人的状态。 There is documentation available in this context - Save user and conversation data在此上下文中有可用的文档 - 保存用户和对话数据

Here is some code that meets your requirement.这是一些符合您要求的代码。

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{
    // Get the state properties from the turn context.

    var conversationStateAccessors =  _conversationState.CreateProperty<ConversationData>(nameof(ConversationData));
    var conversationData = await conversationStateAccessors.GetAsync(turnContext, () => new ConversationData());

    var userStateAccessors = _userState.CreateProperty<UserProfile>(nameof(UserProfile));
    var userProfile = await userStateAccessors.GetAsync(turnContext, () => new UserProfile());

    if (string.IsNullOrEmpty(userProfile.Name))
    {
        // First time around this is set to false, so we will prompt user for name.
        if (conversationData.PromptedUserForName)
        {
            // Set the name to what the user provided.
            userProfile.Name = turnContext.Activity.Text?.Trim();

            // Acknowledge that we got their name.
            await turnContext.SendActivityAsync($"Thanks {userProfile.Name}. To see conversation data, type anything.");

            // Reset the flag to allow the bot to go through the cycle again.
            conversationData.PromptedUserForName = false;
        }
        else
        {
            // Prompt the user for their name.
            await turnContext.SendActivityAsync($"What is your name?");

            // Set the flag to true, so we don't prompt in the next turn.
            conversationData.PromptedUserForName = true;
        }
    }
    else
    {
        // Add message details to the conversation data.
        // Convert saved Timestamp to local DateTimeOffset, then to string for display.
        var messageTimeOffset = (DateTimeOffset) turnContext.Activity.Timestamp;
        var localMessageTime = messageTimeOffset.ToLocalTime();
        conversationData.Timestamp = localMessageTime.ToString();
        conversationData.ChannelId = turnContext.Activity.ChannelId.ToString();

        // Display state data.
        await turnContext.SendActivityAsync($"{userProfile.Name} sent: {turnContext.Activity.Text}");
        await turnContext.SendActivityAsync($"Message received at: {conversationData.Timestamp}");
        await turnContext.SendActivityAsync($"Message received from: {conversationData.ChannelId}");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM