C# 如何将变量从控制器传递到bots文件

C# 如何将变量从控制器传递到bots文件,c#,botframework,C#,Botframework,我正在尝试将id值从控制器传递到bot文件。 例如 messageActivityAsync上的受保护覆盖异步任务(iTurContext turnContext,CancellationToken CancellationToken) { 等待turnContext.SendActivityAsync(ActivityTypes.Typing); 睡眠(2500); 如果(turnContext.Activity.Text==“红色”) { 等待turnContext.SendActivity

我正在尝试将id值从控制器传递到bot文件。 例如

messageActivityAsync上的受保护覆盖异步任务(iTurContext turnContext,CancellationToken CancellationToken)
{
等待turnContext.SendActivityAsync(ActivityTypes.Typing);
睡眠(2500);
如果(turnContext.Activity.Text==“红色”)
{
等待turnContext.SendActivityAsync(“嘿”);
} 
var reply=MessageFactory.Text(“还有什么我能帮你的吗?”);
reply.SuggestedActions=新的SuggestedActions()
{
操作=新列表()
{
new CardAction(){Title=“Red”,Type=ActionTypes.ImBack,Value=“Red”},
new CardAction(){Title=“Yellow”,Type=ActionTypes.ImBack,Value=“Yellow”},
new CardAction(){Title=“Blue”,Type=ActionTypes.ImBack,Value=“Blue”},
},
};
等待turnContext.SendActivityAsync(回复、取消令牌);
}
我需要将chatBotID传递到bot文件以打印出用户id。
我尝试了几种不同的asp.net核心解决方案,但似乎都不起作用。任何想法

我假设您正在将bot注册为transient,并通过DI将其注入webapi控制器

如果是这种情况,您可以在Bot类中实现一个公共方法来获取Bot id(例如,
SetBotId


然后在
PostAsync
函数中,如果您没有太多的“机器人ID”需要管理,并且它们的行为不同,则可以在调用
Adapter.ProcessAsync

之前调用
Bot.SetBotId(chatBotId)
,我强烈建议您创建不同的机器人类,并将它们连接到不同的控制器上。这只是一个设计考虑。如果你的机器人行为相同,这种方法是可以的。但是,如果bot1是纯QnA,则bot2根据意图采取行动并使用多回合对话,那么将这些对话放入同一个bot并执行if/else不是一个好主意。在这种情况下,您可以创建多个bot,每个bot具有不同的行为和它们自己的接口(继承自IBot)。在DI-AddTransient(IBotQnA,BotQnA)和AddTransient(IBotLuis,BotLuis)中注册它们。现在您可以将它们注入到它们自己的控制器中,它们的逻辑完全分离。您的目标是哪个通道?你能解释一下为什么你需要一个机器人ID,而不是活动的
from
字段中已经存在的ID吗?
[Route("api/chatbot/{chatBotID}")]
        [HttpPost]
        public async Task PostAsync(int chatBotID)
        {

            // Delegate the processing of the HTTP POST to the adapter.
            // The adapter will invoke the bot.
            await Adapter.ProcessAsync(Request, Response, Bot);

        }
      protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
        {


            await turnContext.SendActivityAsync(ActivityTypes.Typing);
            Thread.Sleep(2500);

            if (turnContext.Activity.Text == "Red")
            {
                await turnContext.SendActivityAsync("Hey");
            } 

            var reply = MessageFactory.Text("Is there anything else I can help you?");

            reply.SuggestedActions = new SuggestedActions()
            {
                Actions = new List<CardAction>()
            {
                new CardAction() { Title = "Red", Type = ActionTypes.ImBack, Value = "Red" },
                new CardAction() { Title = "Yellow", Type = ActionTypes.ImBack, Value = "Yellow" },
                new CardAction() { Title = "Blue", Type = ActionTypes.ImBack, Value = "Blue" },
            },
            };
            await turnContext.SendActivityAsync(reply, cancellationToken);



        }