问题是使用.Net Core通过Bot Framework创建Bot应用程序。在.Net Framework中,我使用API来创建Bot App。在初始方法我传递字符串参数并从IFrame Url获取此值,但在.Net Core中我使用的是OnTurnAsync方法,并且无法覆盖此方法以将字符串参数作为“userName”传递。我在下面的.Net核心和.Net框架初始方法之间进行共享。
我通过IFrame调用bot应用程序,如; https://webchat.botframework.com/...&userName=test'style =“width:600px; height:600px;”>
那么如何将参数传递给OnTurnAsync方法呢?
.Net框架
public async Task<HttpResponseMessage> Post([FromBody]Activity activity, string userName)
{
if (activity.Type == ActivityTypes.Message)
{
var keyword = activity.Text.ToLower().ToEnglish();
var responseAttachment = KeywordHelper.GetAttachmentResult(keyword);
if (responseAttachment != null)
{
var answer = ((HeroCard)responseAttachment.Content).Title.ToString();
conversation.Response = answer;
this.conversationService.InsertToConversation(conversation);
var connector = new ConnectorClient(new Uri(activity.ServiceUrl));
var reply = activity.CreateReply();
reply.Attachments.Add(responseAttachment);
await connector.Conversations.ReplyToActivityAsync(reply);
activity.Type = ActivityTypes.Message;
}
}
}
.Net核心
public async Task OnTurnAsync(ITurnContext turnContext, CancellationToken cancellationToken = default(CancellationToken))
{
if (turnContext.Activity.Type == ActivityTypes.Message)
{
var userName = "userName";
var keyword = turnContext.Activity.Text.ToLower().ToEnglish();
var responseAttachment = KeywordHelper.GetAttachmentResult(keyword);
if (responseAttachment != null)
{
var answer = ((HeroCard)responseAttachment.Content).Title.ToString();
conversation.Response = answer;
this.conversationService.InsertToConversation(conversation);
var connector = new ConnectorClient(new Uri(turnContext.Activity.ServiceUrl));
var reply = turnContext.Activity.CreateReply();
}
}
}
你仍然可以使用带有BotBuilder V4的Web Api控制器,如下例所示:https://github.com/Microsoft/BotBuilder-Samples/blob/samples-work-in-progress/samples/csharp_dotnetcore/13.core-bot/Controllers/BotController.cs
[Route("api/messages")]
[ApiController]
public class BotController : ControllerBase
{
private readonly IBotFrameworkHttpAdapter Adapter;
private readonly IBot Bot;
public BotController(IBotFrameworkHttpAdapter adapter, IBot bot)
{
Adapter = adapter;
Bot = bot;
}
[HttpPost]
public async Task PostAsync()
{
// Delegate the processing of the HTTP POST to the adapter.
// The adapter will invoke the bot.
await Adapter.ProcessAsync(Request, Response, Bot);
}
}
但是,如果您将用户标识和用户名作为查询字符串参数传递给WebChat iframe,那么您可以从activity.From.Name
检索用户名