botFramework:在bot框架下禁用Web聊天中的卡按钮

-2 c# bots web-chat botframework

我想在bot框架下禁用网络聊天中的卡片按钮.例如,在一张卡片我有多个按钮,我想根据一些数据值禁用一些按钮.我怎样才能做到这一点?

Cor*_*ina 5

在将来,我建议缩小您的要求,使其不那么广泛,并包含与您要实现的目标相关的详细信息.尽管如此,我已经填补了一些空白并做了一些我希望会有所帮助的事情.:)

我有一个工作示例机器人,执行以下操作:

  • 每个响应显示一张英雄卡片
  • 如果用户说'hi'或'hello',卡上会显示两个按钮
  • 如果用户没有问候机器人,只会显示一个按钮.
  • 告诉用户他们是否与机器人打交道(SendGreeting,true或false)

我在UserData,SendGreeting中保存了一个布尔值,通知机器人是否显示第二个按钮.在控制器中,我有以下代码:

  • 实例化UserData,
  • 确定SentGreeting是true还是false
  • 然后将该布尔值保存到UserData.SentGreeting.

             // create connector service
            ConnectorClient connector = new ConnectorClient(new Uri(activity.ServiceUrl));
            // get state client object
            StateClient stateClient = activity.GetStateClient();
            // retrieve user data
            BotData userData = await stateClient.BotState.GetUserDataAsync(activity.ChannelId, activity.From.Id);
    
            bool SentGreeting;
            if (activity.Text == "hello" || activity.Text == "hi")
            {
                SentGreeting = true;
            } else
            {
                SentGreeting = false;
            }
            userData.SetProperty<bool>("SentGreeting", SentGreeting);
    
            // save state
            await stateClient.BotState.SetUserDataAsync(activity.ChannelId, activity.From.Id, userData);
    
            // initiate CardDialog
            await Conversation.SendAsync(activity, () => new CardDialog());
    
    Run Code Online (Sandbox Code Playgroud)

在对话框中,有以下代码:

  • 从UserData检索SentGreeting
  • 创建英雄卡
  • 只有在SentGreeting为true时,才会创建秘密按钮并将其添加到按钮列表中

            // create sentGreeting
        bool sentGreeting;
        // assign value from UserData.SentGreeting
        context.UserData.TryGetValue<bool>("SentGreeting", out sentGreeting);
    
        // create standard button list (for those who do not greet the bot)
        var GenericButtonList = new List<CardAction>
        {
            new CardAction(ActionTypes.OpenUrl, "Bot Framework", value: "https://docs.botframework.com/en-us/"),
        };
    
        // create the hero card
        var Card = new HeroCard()
        {
            Title = "Select an Action",
            Subtitle = "Choose from available actions",
            Text = "If you were polite and said \'hi\' or \'hello\' to this bot, you will see two buttons below.",
            Images = new List<CardImage> { new CardImage("https://cloud.githubusercontent.com/assets/14900841/23733811/c618e402-042f-11e7-9b8e-6262d9f2d898.JPG") },
            Buttons = GenericButtonList
        };
        // create secret button that only users who said hi can see
        CardAction secretButton = new CardAction();
        secretButton.Title = "Very Polite";
        secretButton.Type = "openUrl";
        secretButton.Value = "https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/demo-ContosoFlowers/ContosoFlowers.Services";
    
        if (sentGreeting)
        {
            Card.Buttons.Add(secretButton);
        }
    
    Run Code Online (Sandbox Code Playgroud)

链接到这个机器人的GitHub仓库