0
刪除Botframework的對話框棧

問:如何是Botframework的對話框棧從控制檯應用程序(或Web作業)刪除如何從console.application

我想刪除用戶的對話堆棧,使用戶可以回根如果他們停止與Bot交談並且幾個小時過去,他們將進行對話作爲測試,我創建了以下控制檯應用程序,並試圖刪除用戶的對話堆棧。

using Autofac; 
using Microsoft.Bot.Builder.Dialogs; 
using Microsoft.Bot.Builder.Dialogs.Internals; 
using Microsoft.Bot.Connector; 
using System; 
using System.Threading; 
using System.Threading.Tasks; 

namespace ConsoleApp6 
{ 
    class Program 
    { 
     const string MicrosoftTeamsUserId = "29:xxxxxxxxxxx-Q"; 
     const string BotId = "28:xxxx-xxxx-xxx-xxxx-xxx"; 
     const string channelId = "msteams"; 
     const string appId = "xxxx-xxxx-xxxx-xxxx-xxxxx"; 
     const string password = "xxxxxxxxxxxxxxx"; 
     const string serviceUrl = "https://smba.trafficmanager.net/"; 

     static void Main(string[] args) 
     { 
      Test().Wait(); 
      Console.WriteLine("Done"); 
      Console.Read(); 
     } 

     public static async Task Test() 
     { 
      MicrosoftAppCredentials.TrustServiceUrl(serviceUrl, DateTime.MaxValue); 
      var connector = new ConnectorClient(new Uri(serviceUrl), appId, password); 
      var botAccount = new ChannelAccount(BotId); 
      var userAccount = new ChannelAccount(MicrosoftTeamsUserId); 
      var res = connector.Conversations.CreateDirectConversation(botAccount, userAccount); 

      var activity = Activity.CreateMessageActivity(); 
      activity.ChannelId = channelId; 
      activity.Recipient = userAccount; 
      activity.From = botAccount; 
      activity.ServiceUrl = serviceUrl; 
      activity.Conversation = new ConversationAccount(id: res.Id); 
      activity.Locale = "ja-JP"; 
      activity.Text = "TEST"; 

      using (var scope = DialogModule.BeginLifetimeScope(Conversation.Container, activity)) 
      { 
       var botData = scope.Resolve<IBotData>(); 
       await botData.LoadAsync(default(CancellationToken)); 
       var stack = scope.Resolve<IDialogStack>(); 
       stack.Reset(); 
       await botData.FlushAsync(default(CancellationToken)); 
       //await Conversation.SendAsync(activity,() => new RootDialog()); 
      } 
     } 
    } 
} 

但是,調用以下方法時發生異常。

await botData.LoadAsync(default(CancellationToken));

例外情況是如下(401未授權)。在 系統在 System.Threading.Tasks.Task.ThrowIfExceptional(布爾 includeTaskCanceledExceptions):

System.AggregateException發生的HResult = 0x80131500消息= 1 つ以上のエラーが発生しました源= mscorlib程序棧跟蹤。 .Threading.Tasks.Task.Wait(的Int32 millisecondsTimeout, 的CancellationToken的CancellationToken)處 ConsoleApp6.Program.Main(字串[] args) System.Threading.Tasks.Task.Wait()在 C:\用戶\神大\的文檔\ Visual Studio的 2017年\項目\ ConsoleApp6 \ ConsoleApp6 \的Program.cs:行22

內部異常1:OAuthException:未經授權

內部異常2:HttpRequestException:応答の狀態コードは成功を示していません:401 (未授權)

你能告訴我怎麼樣刪除對話框堆棧?

FYI: 我知道如果我創建StateCient的實例並調用DeleteStateForUserAsync()方法,我確認該方法可以刪除對話框堆棧。但是,該方法將刪除不僅對話框堆棧,而且還設定狀態服務屬性。我想只刪除對話框堆棧。所以我正在尋找另一種方式。

回答

0

當你在代碼中調用:

var connector = new ConnectorClient(new Uri(serviceUrl), appId, password) 

內部,機器人框架SDK註冊自己MicrosoftAppCredentials實例;那個實例是請求你的機器人使用的令牌的東西。但是,令牌只能持續60分鐘左右。只要用戶保持說話的機器人,該令牌透明刷新在幕後,但是當用戶還沒有在談話時機器人,這不會發生,這是什麼導致了異常。如果您使用招看你的機器人,你會看到它試圖獲得令牌空白的appid和密碼,然後是HTTP 401異常。

如果將以下內容添加到app.config或web.config文件中,則SDK中的MicrosoftAppCredentials實例將使用ConfigurationManager選取應用程序ID和密碼。的AppSettings並刷新令牌你:

<appSettings> 
    <add key="MicrosoftAppId" value="[id]"/> 
    <add key="MicrosoftAppPassword" value="[password]"/> 
</appSettings> 

一旦你這樣做,你可以清理自己的代碼來從ConfigurationManager.AppSettings API檢索MicrosoftAppId和MicrosoftAppPassword太多,而不是在您的測試將它們設置爲變量()函數。