在本地机器人中工作得很好,但在网络聊天 channel 上“向您的机器人发送此消息时出错:HTTP 状态代码网关超时”发生错误,但机器人在第二次响应后正确运行
Controller 代码
public async Task<HttpResponseMessage> Post([FromBody]Activity activity)
{
if (activity.Type == ActivityTypes.Message)
{
await Conversation.SendAsync(activity, () => new EchoDialog());
}
else if (message.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
IConversationUpdateActivity iConversationUpdated = message as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
ConnectorClient connector = new ConnectorClient(new System.Uri(message.ServiceUrl));
foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
{
// if the bot is added, then
if (member.Id == iConversationUpdated.Recipient.Id)
{
var reply = ((Activity)iConversationUpdated).CreateReply("Hi, Welcome to Systenics.");
await connector.Conversations.ReplyToActivityAsync(reply);
await Conversation.SendAsync(message, () => new EchoDialog());
}
}
}
}
else
{
await HandleSystemMessage(activity);
}
var response = Request.CreateResponse(HttpStatusCode.OK);
return response;
}
聊天对话框
public async Task StartAsync(IDialogContext context)
{
context.Wait(this.ShowOptions);
}
public virtual async Task ShowOptions(IDialogContext context, IAwaitable<IMessageActivity> activity)
{
var message = await activity;
var descriptions = new string[] { "Request a Quote", "More Information About Jobs" };
PromptDialog.Choice(
context: context,
resume: ChoiceReceivedAsync,
options: descriptions,
prompt: "Please select an option below:",
retry: "Selected option not available.",
promptStyle: PromptStyle.Auto
);
}
最佳答案
您似乎想捕获 ConversationUpdate
事件并创建并发送一条假消息来初始化对话框,以便通过 PromptDialog.Choice( )
方法。我建议您可以在发送初始化对话框之前指定事件的 Text
属性,然后您可以通过检查对话框中的消息事件 Text
属性来执行不同的操作。
根据您的场景,我创建了以下示例代码,该代码适用于我的 Web Chat,您可以引用。
在 MessagesController 中:
else if (activity.Type == ActivityTypes.ConversationUpdate)
{
// Handle conversation state changes, like members being added and removed
// Use Activity.MembersAdded and Activity.MembersRemoved and Activity.Action for info
// Not available in all channels
IConversationUpdateActivity iConversationUpdated = activity as IConversationUpdateActivity;
if (iConversationUpdated != null)
{
ConnectorClient connector = new ConnectorClient(new System.Uri(activity.ServiceUrl));
foreach (var member in iConversationUpdated.MembersAdded ?? System.Array.Empty<ChannelAccount>())
{
// if the bot is added, then
if (member.Id == iConversationUpdated.Recipient.Id)
{
var reply = ((Activity)iConversationUpdated).CreateReply("Hi, Welcome to Systenics.");
await connector.Conversations.ReplyToActivityAsync(reply);
//set value to Text property
activity.Text = "show choices";
await Conversation.SendAsync(activity, () => new EchoDialog());
}
}
}
}
在对话框中:
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var activity = await result as Activity;
var mes = activity.Text?.ToString();
if (mes == "show choices")
{
var descriptions = new string[] { "Request a Quote", "More Information About Jobs" };
PromptDialog.Choice(
context: context,
resume: ChoiceReceivedAsync,
options: descriptions,
prompt: "Please select an option below:",
retry: "Selected option not available.",
promptStyle: PromptStyle.Auto
);
}
else
{
if (mes == "More Information About Jobs")
{
var replymes = context.MakeMessage();
replymes.Text = "Click on this link to know more about jobs [https//www.abc.com](https//www.abc.com)";
await context.PostAsync(replymes);
}
else if (mes == "Request a Quote")
{
//your code logic here
}
else
{
await context.PostAsync($"You sent {activity.Text}");
}
context.Wait(MessageReceivedAsync);
}
}
测试结果:
关于c# - Microsoft bot 服务在本地运行良好,但在网络聊天 channel 中运行不佳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51997901/