【问题标题】:Chatbot not sending message back in Microsoft Teams聊天机器人未在 Microsoft Teams 中发回消息
【发布时间】:2018-01-18 17:05:14
【问题描述】:

对于一个 azure 聊天机器人,我希望它在回答后问我一个简单的问题,例如,我可以提供反馈作为回报。 我正在使用HeroCard 类。

对话框

private async Task ShowWeatherResult(IDialogContext context, LuisResult result)
{
    bool found = false;
    foreach (var entity in result.Entities)
    {
        if (entity.Type.Equals(Entity_Location))
        {
            WeatherAPI weather = new WeatherAPI(entity.Entity);
            found = true;
            await context.PostAsync(weather.ForecastReport());
            await Task.Delay(500);

            // ask for happiness
            Attachment attachment = new Attachment()
            {
                ContentType = HeroCard.ContentType,
                Content = CardsBuilder.CreateHappinessCard()
            };
            var reply = context.MakeMessage();
            reply.Attachments.Add(attachment);
            await context.PostAsync(reply, CancellationToken.None);

            context.Wait(MessageReceivedAsync);
        }
    }
    if (!found)
    {
        await context.PostAsync($"I don't speak human fluently, try another question asking for a specific city!");
        context.Wait(MessageReceived);
    }
}


public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
    var message = await result;

    if (message.Text != null)
    {
        //
        var happiness = new HappinessAPI();

        // Got an Action Submit
        string value = message.Text;
        //string submitType = value.Type.ToString();
        switch (value)
        {
            case "ShowGif":
                await context.PostAsync(happiness.ShowGif(context), CancellationToken.None);
                await Task.Delay(500);
                break;
            case "HappinessSearch":
                await context.PostAsync(happiness.GetJoke(context), CancellationToken.None);
                await Task.Delay(500);
                break;
            default:
                break;
        }
    }

    context.Wait(MessageReceived);
}

HerdoCard

    internal static HeroCard CreateHappinessCard()
    {
        HeroCard card = new HeroCard()
        {
            Title = "Hi!",
            Text = "Are you happy?",
            Buttons = new List<CardAction>()
            {
                new CardAction()
                {
                    Title = "Yes",
                    Text = "Yes",
                    DisplayText = "Yes",
                    Type = ActionTypes.PostBack,
                    Value = "ShowGif"
                },
                new CardAction()
                {
                    Title = "Meh...",
                    Text ="No",
                    DisplayText = "Meh...",
                    Type = ActionTypes.PostBack,
                    Value = "HappinessSearch"
                }
            }

        };

        return card;
    }

幸福API

public class HappinessAPI
{
    internal IMessageActivity ShowGif(IDialogContext context)
    {
        Attachment attachment = new Attachment()
        {
            ContentType = HeroCard.ContentType,
            Content = new HeroCard()
            {
                Images = new List<CardImage>()
                {
                    new CardImage("https://upload.wikimedia.org/wikipedia/commons/thumb/8/85/Smiley.svg/220px-Smiley.svg.png")
                }
            }
        };
        var reply = context.MakeMessage();
        reply.Attachments.Add(attachment);

        return reply;
    }

    internal IMessageActivity GetJoke(IDialogContext context)
    {
        var request = WebRequest.Create("http://api.icndb.com/jokes/random");
        request.ContentType = "application/json; charset=utf-8";
        string text;
        var response = (HttpWebResponse)request.GetResponse();

        using (var sr = new StreamReader(response.GetResponseStream()))
        {
            text = sr.ReadToEnd();
        }
        var reply = context.MakeMessage();
        reply.Text = (string)(JObject.Parse(text))["value"]["joke"];

        return reply;
    }
}

问题是,在 AzurePortal 中使用 WebChat 进行测试时它可以工作,但对其问题的回复在 Microsoft 团队中却没有。

示例: Works in Webchat:

我:法兰克福的天气

机器人:“很冷……随便”

机器人:你快乐吗?

我:点击“是/否”

机器人:发送笑话或笑脸

Doesn't work in Microsoft Teams

一切正常,直到我点击“是/否”,然后它只是尝试做某事(“正在输入...”出现,但在那之后,什么也没有发生。

编辑

我在 Microsoft Teams 中使用聊天机器人时看到,当我单击 herocard 时会在聊天中写入一条消息,但它不应该写入,因为它被设置为 ActionTypes.Postback

编辑 2

英雄卡现在看起来像这样:

    internal static HeroCard CreateHappinessCard()
    {
        HeroCard card = new HeroCard()
        {
            Title = "Hi!",
            Text = "Are you happy?",
            Buttons = new List<CardAction>()
            {
                new CardAction()
                {
                    Title = "Yes",
                    Text = "ShowGif",
                    //DisplayText = null,
                    Type = ActionTypes.MessageBack,
                    Value= "{\"action\": \"ShowGif\" }"
                },
                new CardAction()
                {
                    Title = "Meh...",
                    Text ="HappinessSearch",
                    //DisplayText = null,
                    Type = ActionTypes.MessageBack,
                    Value = "{\"action\": \"HappinessSearch\" }"
                }
            }

        };

        return card;
    }
}

但仍然无法正常工作。没有消息被发送回机器人。如果我使用 imBack 输入它,但该消息出现在聊天中,我不想要什么,messageBack 应该可以工作。

编辑 3 遵循提供的代码

对话框

    private async Task ShowLuisResult(IDialogContext context, LuisResult result)
    {
        await context.PostAsync($"You have reached {result.Intents[0].Intent}. You said: {result.Query}");

        context.Call(new HeroCardDialog(), MessageReceivedAsync);
    }

    public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
        var message = await result;

        if (message != null)
        {
            //

        }

        //context.Wait(MessageReceived);
        context.Done<object>(null);
    }

HeroCardDialog

public class HeroCardDialog : IDialog<object>
{
    public async Task StartAsync(IDialogContext context)
    {
        if (context == null)
        {
            throw new ArgumentNullException(nameof(context));
        }

        //Set the Last Dialog in Conversation Data
        context.UserData.SetValue("HeroCardId", "HerdoCard Dialog");

        var message = context.MakeMessage();
        var attachment = GetHeroCard();

        message.Attachments.Add(attachment);

        await context.PostAsync((message));

        context.Done<object>(null);
    }

    private static Attachment GetHeroCard()
    {
        var heroCard = new HeroCard()
        {
            Title = "Hi!",
            Text = "Are you happy?",
            Buttons = new List<CardAction>()
                {
                    new CardAction()
                    {
                        Title = "Yes",
                        Text = "ShowGif",
                        DisplayText = null,
                        Type = ActionTypes.MessageBack,
                        Value= "{\"msgback\" : \"ShowGif\"}"
                    },
                    new CardAction()
                    {
                        Title = "Meh...",
                        Text ="HappinessSearch",
                        DisplayText = null,
                        Type = ActionTypes.MessageBack,
                        Value= "{\"msgback\" : \"HappinessSearch\"}"
                    }
                }

        };

        return heroCard.ToAttachment();
    }
}

【问题讨论】:

    标签: c# azure chatbot microsoft-teams


    【解决方案1】:

    PostBack 不受 Microsoft Teams 支持。请查看 Microsoft Teams 中的supported button activities 列表。

    我们建议您使用messageBack,因为您可以创建完全自定义的操作。

    【讨论】:

    • 我已将代码修改为Type = ActionTypes.MessageBack,,但仍然无法正常工作。现在什么也没有发生。聊天机器人对我的点击完全没有反应。未调用方法“MessageReceivedAsync”。能否提供样品?
    • 我看了一会儿你的代码没有发现任何明显的问题,但这里有一个有效的例子:github.com/OfficeDev/microsoft-teams-sample-complete-csharp/…
    • @BillBliss-MSFT 感谢您的评论和链接。我正在检查代码,但我仍然不知道如何获取对话框的答案。我用新代码更新了我的问题。
    猜你喜欢
    • 2018-04-07
    • 2020-10-17
    • 2018-08-20
    • 1970-01-01
    • 2020-02-21
    • 2020-08-27
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多