如何将变量从控制器传递到机器人文件

时间:2019-08-28 16:00:27

标签: c# botframework

我正在尝试将控制器的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);



        }

我需要将chatBotID传递给bot文件以打印出用户ID。 我为asp.net core尝试了几种不同的解决方案,但似乎没有任何效果。任何想法

1 个答案:

答案 0 :(得分:2)

我假设您正在将机器人注册为临时机器人,并通过DI将其注入到webapi控制器中。

在这种情况下,您可以在Bot类中实现一个公共方法来获取机器人ID(例如SetBotId)。

然后在PostAsync函数中,可以在调用Bot.SetBotId(chatBotId)之前立即调用Adapter.ProcessAsync