簡體   English   中英

如何將變量從控制器傳遞到bots文件

[英]How to pass variable from the controller to bots file

我正在嘗試將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嘗試了幾種不同的解決方案,但似乎沒有任何效果。 有任何想法嗎

我假設您將機器人注冊為臨時機器人,並通過DI將其注入到webapi控制器中。

在這種情況下,您可以在Bot類中實現一個公共方法以獲取機器人ID(例如SetBotId )。

然后在PostAsync功能,可以調用Bot.SetBotId(chatBotId)就在你打電話之前Adapter.ProcessAsync

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM