聊天机器人不在 Microsoft Teams 中发回消息
Chatbot not sending message back in Microsoft Teams
对于 azure 聊天机器人,我希望它在回答后问我一个简单的问题,这样我就可以在 return 中提供反馈。
我正在使用 HeroCard
class.
对话框
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 进行测试时有效,但对其问题的回复在 Microsoft 团队中没有。
样本:
Works in Webchat:
我:法兰克福的天气
机器人:"It's cold.... whatever"
机器人:你开心吗?
我:点击"yes/no"
Bot:发送笑话或笑脸
Doesn't work in Microsoft Teams
一切正常,直到我单击 "yes/no",然后它尝试做一些事情("is typing..." 出现,但在那之后,没有任何反应。
编辑
我在 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();
}
}
Microsoft Teams 不支持 PostBack
。请查看 Microsoft Teams 中 supported button activities 的列表。
我们建议您使用 messageBack
,因为您可以创建完全自定义的操作。
对于 azure 聊天机器人,我希望它在回答后问我一个简单的问题,这样我就可以在 return 中提供反馈。
我正在使用 HeroCard
class.
对话框
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 进行测试时有效,但对其问题的回复在 Microsoft 团队中没有。
样本:
Works in Webchat:
我:法兰克福的天气
机器人:"It's cold.... whatever"
机器人:你开心吗?
我:点击"yes/no"
Bot:发送笑话或笑脸
Doesn't work in Microsoft Teams
一切正常,直到我单击 "yes/no",然后它尝试做一些事情("is typing..." 出现,但在那之后,没有任何反应。
编辑
我在 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();
}
}
PostBack
。请查看 Microsoft Teams 中 supported button activities 的列表。
我们建议您使用 messageBack
,因为您可以创建完全自定义的操作。