Reputation: 13
The problem is about creating Bot app via Bot Framework using .Net Core. In .Net Framework I have used an API to create Bot App. At initial method I'm passing string parameter and getting this value from IFrame Url, but in .Net Core I'm using OnTurnAsync method and can not override this method to pass string parameter as "userName". I'm sharing between .Net core and .Net framework initial methods below.
I'm calling bot app via IFrame such as ; https://webchat.botframework.com/...&userName=test' style="width:600px; height:600px;">
So how can I pass parameter to OnTurnAsync method ?
.Net Framework
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 Core
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();
}
}
}
Upvotes: 1
Views: 358
Reputation: 8292
You can still use a Web Api Controller with BotBuilder V4, as demonstrated in this example: 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);
}
}
However, if you are passing the userid and username as query string parameters to WebChat iframe then you can retrieve the username from activity.From.Name
Upvotes: 1