如何在OnTurnAsync-Method中处理来自AdaptiveCard的回发

时间:2019-02-21 15:17:04

标签: c# botframework bots adaptive-cards

我正在编程一个使用自适应Cards ChoiceSet的机器人。我试图让用户选择。现在,由于结果未显示在聊天中,因此如果我收到的消息是OnTurnAsync,则必须签入postback-方法。 我该怎么做呢? 这就是我尝试的方式-> Null ReferenceException  if (dc.Context.Activity.GetType().GetProperty("ChannelData") != null)

我的OnTurnAsync方法:

public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        DialogContext dc = null;

        switch (turnContext.Activity.Type)
        {
            case ActivityTypes.Message:
                if (dc.Context.Activity.GetType().GetProperty("ChannelData") != null)
                {
                    var channelData = JObject.Parse(dc.Context.Activity.ChannelData.ToString());
                    if (channelData.ContainsKey("postback"))
                    {
                        var postbackActivit = dc.Context.Activity;
                        postbackActivit.Text = postbackActivit.Value.ToString();
                        await dc.Context.SendActivityAsync(postbackActivit);
                    }
                }

                await ProcessInputAsync(turnContext, cancellationToken);
                break;

1 个答案:

答案 0 :(得分:3)

因为您的行:DialogContext dc = null

应为:var dc = await Dialogs.CreateContextAsync(turnContext);

请注意,Dialogs可能需要替换为您定义DialogSet的任何内容。可以使用几种不同的方法,但这是我用来测试上一期课程的<myBot>.cs类的上半部分:

public class QuickTestBot_CSharpBot : IBot
    {
        private readonly IStatePropertyAccessor<DialogState> _dialogStateAccessor;
        private readonly ConversationState _conversationState;
        public QuickTestBot_CSharpBot(ConversationState conversationState)
        {
            _conversationState = conversationState ?? throw new ArgumentNullException(nameof(conversationState));
            _dialogStateAccessor = _conversationState.CreateProperty<DialogState>(nameof(DialogState));

            Dialogs = new DialogSet(_dialogStateAccessor);
            Dialogs.Add(new QuickDialog());

        }

        private DialogSet Dialogs { get; set; }

        public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
        {
            var activity = turnContext.Activity;

            var dc = await Dialogs.CreateContextAsync(turnContext);

            if (activity.Type == ActivityTypes.Message)
            {
                // Ensure that message is a postBack (like a submission from Adaptive Cards
                var channelData = JObject.Parse(dc.Context.Activity.ChannelData.ToString());
                if (channelData.ContainsKey("postBack"))
                {
                    var postbackActivity = dc.Context.Activity;
                    // Convert the user's Adaptive Card input into the input of a Text Prompt
                    // Must be sent as a string
                    postbackActivity.Text = postbackActivity.Value.ToString();
                    await dc.Context.SendActivityAsync(postbackActivity);
                }
            }
            ...

以下是一些也使用瀑布对话框的优秀示例的链接,因此您可以看到它们如何设置其<bot>.cs类(请注意,它们没有从自适应卡中获取输入...这仅仅是帮助您设置瀑布对话框和OnTurnAsync):

  1. MultiTurnPromptsBot
  2. CardsBot
  3. BasicBot