聊天机器人在微软团队中不发回消息

Chatbot not sending message back in Microsoft Teams(聊天机器人在微软团队中不发回消息)

本文介绍了聊天机器人在微软团队中不发回消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于一个蓝色聊天机器人,我希望它在回答后问我一个简单的问题,这样我就可以例如给出反馈。 我正在使用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;
    }

appinessapi

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

问题是,它在使用Azure门户中的网络聊天进行测试时有效,但在Microsoft团队中对其问题的回复不起作用。

示例: Works in Webchat:

我:法兰克福的天气

机器人:"天冷……管它呢"

机器人:你快乐吗?

我:点击"是/否"

Bot:发送笑话或笑脸

Doesn't work in Microsoft Teams

一切正常,直到我点击"是/否",然后它只是尝试做一些事情("正在打字..."出现了,但之后,什么都没有发生。

编辑

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

编辑%2

HeroCard现在如下所示:

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

推荐答案

PostBack不受微软团队支持。请检查Microsoft Teams中的supported button activities列表。

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

这篇关于聊天机器人在微软团队中不发回消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:聊天机器人在微软团队中不发回消息

基础教程推荐