2017-03-02 64 views

回答

5

未来我会建议缩小您的请求的范围,减少涉及您想要实现的内容的细节。尽管如此,我填补了一些空白,并且做了一些我希望会有所帮助的事情。 :)

我有一个working example bot,做以下操作:

  • 显示英雄卡,每个响应
  • 如果用户说“嗨”或“你好”,两​​个按钮将显示卡上
  • 如果用户不喜欢机器人,只会显示一个按钮。
  • 告诉用户他们是否迎接机器人(SendGreeting,真或假)

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

  • 实例化的UserData,
  • 确定SentGreeting是否应当是真或假
  • 然后保存该布尔到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()); 
    

在对话框中,有下列代码的作用:

  • 从的UserData检索SentGreeting
  • 创建英雄卡
  • 创建秘密按钮并将其添加到仅当SentGreeting为真时按钮列表

     // 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); 
        } 
    

链接到GitHub repo这个机器人

+0

我不想根据数据输入切换按钮(显示/隐藏)的可见性。我想根据输入数据启用/禁用按钮。该按钮应始终存在,应根据数据值启用或禁用该按钮。 –

1

不能启用/禁用使用外的现成的网上聊天。尽管如此,您也许可以构建自定义WebChat并添加该功能。

所有的魔法都发生在Attachment.tsx文件中。有一个buttons函数,例如,在呈现HeroCard时会调用该函数。该函数创建一个按钮并应用CSS类。你可以根据你的按钮的某个值动态地改变这个类。

顺便说一句,如果你设法做到这一点,国际海事组织,它可能是WebChat组件的良好贡献。