对于一个天蓝色的聊天机器人,我希望它在回答后问我一个简单的问题,所以我可以作为回报给出反馈。
我正在使用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;
}
happinessapi
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进行测试时有效,但对其问题的回复并不适用于微软团队。
样品:
Works in Webchat:
我:法兰克福的天气
Bot:“这很冷......无论如何”
Bot:你开心吗?我:点击“是/否”
Doesn't work in Microsoft Teams
一切顺利,直到我点击“是/否”,然后它只是做某事(“正在打字......”出现但在此之后,没有任何反应。
修改的
我在使用Microsoft Teams中的Chatbot时看到,当我点击herocard时,会在聊天中写入一条消息,但不应该,因为它已设置为ActionTypes.Postback
编辑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();
}
}
答案 0 :(得分:1)
PostBack
。请查看Microsoft团队中的supported button activities列表。
我们建议您使用messageBack
,因为您可以创建完全自定义的操作。