使用C#,Microsoft.Bot.Builder 3.5.5
聊天机器人工作正常,但是当从持久性菜单中选择按钮回发时,聊天机器人会在失败之前显示20秒内的输入动画并且没有消息。键入确切的消息工作正常,实际上任何消息都应该有响应。 我还调查了dev.botframework.com上的频道问题,并且没有任何相关信息显示出来。
编辑:我甚至没有使用任何逻辑来显示输入消息。它只显示菜单。
答案 0 :(得分:1)
让我们假设当您创建持久性菜单时,您已经向Facebook提出了与此类似的请求:
{
"persistent_menu":[
{
"locale":"default",
"composer_input_disabled":true,
"call_to_actions":[
{
"title":"My Account",
"type":"nested",
"call_to_actions":[
{
"title":"Pay Bill",
"type":"postback",
"payload":"PAYBILL_PAYLOAD"
},
{
"title":"History",
"type":"postback",
"payload":"HISTORY_PAYLOAD"
},
{
"title":"Contact Info",
"type":"postback",
"payload":"CONTACT_INFO_PAYLOAD"
}
]
},
{
"type":"web_url",
"title":"Latest News",
"url":"http://petershats.parseapp.com/hat-news",
"webview_height_ratio":"full"
}
]
},
{
"locale":"zh_CN",
"composer_input_disabled":false
}
]
}
我从官方docs获得了这个Json。
因此,当用户使用"type":"postback"
点击此持久性菜单的任何项目时,您收到的message.Text等于定义的payload
。
处理回发的代码应该是这样的:
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var msg = await argument;
switch (s)
{
case "CONTACT_INFO_PAYLOAD" :
YourMethod();
break;
}
希望有所帮助:)
答案 1 :(得分:0)
您可能想尝试使用建议的操作而不是持久性菜单。您可以找到有关suggested actions here的信息。您还应该升级到最新版本的botbuilder 3.8.1
这是一个简单的实现:
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
namespace SuggestedActions.Dialogs
{
[Serializable]
public class RootDialog : IDialog<object>
{
public Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
return Task.CompletedTask;
}
private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
{
var activity = await result as Activity;
var reply = activity.CreateReply("I have colors in mind, but need your help to choose the best one.");
reply.Type = ActivityTypes.Message;
reply.TextFormat = TextFormatTypes.Plain;
reply.SuggestedActions = new Microsoft.Bot.Connector.SuggestedActions()
{
Actions = new List<CardAction>()
{
new CardAction(){ Title = "Blue", Type=ActionTypes.ImBack, Value="Blue" },
new CardAction(){ Title = "Red", Type=ActionTypes.ImBack, Value="Red" },
new CardAction(){ Title = "Green", Type=ActionTypes.ImBack, Value="Green" }
}
};
// return our reply to the user
await context.PostAsync(reply);
context.Wait(MessageReceivedAsync);
}
}
}