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

从Botframework支持进行编辑:请不要使用下面的代码块。它仅在模拟器中有效。而是使用:

if (string.IsNullOrWhiteSpace(activity.Text) && activity.Value != null)
{
    activity.Text = JsonConvert.SerializeObject(activity.Value);
}


我的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;

最佳答案

这是因为您的行: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 (string.IsNullOrWhiteSpace(activity.Text))
            {
                activity.Text = JsonConvert.SerializeObject(activity.Value);
            }
    [...]



这里是一些也使用瀑布对话框的好样本的链接,因此您可以看到它们如何设置其<bot>.cs类(请注意,它们没有从自适应卡中获取输入...这只是为了帮助您进行设置您的瀑布对话框和OnTurnAsync):


MultiTurnPromptsBot
CardsBot
BasicBot

关于c# - 如何在OnTurnAsync-Method中处理来自AdaptiveCard的回发,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54810429/

10-10 00:41