2017-09-15 66 views
0

我正在C#中建立一個ChatBot,並且我希望在對話停止後的一些消息後,但我不知道該怎麼做。我已經設置了消息的限制,並且我希望在達到此限制後不再發送消息。有我的代碼:結束對話c#bot

private int NombreDeMessages; 

protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item) 
{ 
    var message = await item; 
    NombreDeMessages += 1; 

    if (message.Text != null && NombreDeMessages < 3) 
    { 
     await base.MessageReceived(context, item); 
    } 
    else 
    { 
     var reply = context.MakeMessage(); 
     await context.PostAsync(reply); 
     context.Wait(this.MessageReceived); 
    }     
} 

我刪除了HeroCard部分,因爲它在這裏沒用。

我想要的是在最後的context.Wait之後,添加對話的結尾,以便用戶不能再多聊聊聊天機器人。

回答

1

你的問題有點含糊不清,你是什麼意思結束對話你想讓用戶再也不能和chatbot聊天嗎?有一件事你可以在else塊中做,你可以撥打context.Done()並刪除你的context.Wait(this.MessageReceived),這樣可以讓你的用戶發送消息但沒有迴應,或者在下面的情況下讓用戶知道對話已經結束。

else 
{ 

    var reply = context.MakeMessage(); 
    reply.Text = "conversation ended"; 
    await context.PostAsync(reply); 
    context.Done(this); 
} 
0

謝謝你,我已經解決了我的問題。我發佈的代碼,如果它可以幫助某人!

private int NombreDeMessages; 
     protected override async Task MessageReceived(IDialogContext context, IAwaitable<IMessageActivity> item) 
     { 
      var message = await item; 
      NombreDeMessages += 1; 
      string code = EndOfConversationCodes.CompletedSuccessfully; 
      CancellationToken cancellationToken = default(CancellationToken); 


      if (message.Text != null && NombreDeMessages < 3) 
      { 
       await base.MessageReceived(context, item); 

      } 
      else if (message.Text != null && NombreDeMessages == 3) 
      { 
       AdaptiveCard card = new AdaptiveCard(); 
       card.Body.Add(new TextBlock() 
       { 
        Text = "STOP FLOODING", 
        Weight = TextWeight.Bolder, 
        IsSubtle = true, 
        Wrap = true, 
        Size = TextSize.Large 
       }); 

       card.Body.Add(new TextBlock() 
       { 
        Text = "You have reach the limit of queries", 
        IsSubtle = false, 
        Wrap = true, 
        Size = TextSize.Normal 
       }); 

       card.Body.Add(new Image() 
       { 
        Url = "http://images.roadtrafficsigns.com/img/dp/lg/traffic-stop-sign.png", 

        HorizontalAlignment = HorizontalAlignment.Center, 
        Size = ImageSize.Stretch 
       }); 

       Attachment attachment = new Attachment() 
       { 
        ContentType = AdaptiveCard.ContentType, 
        Content = card 
       }; 
       var flood = context.MakeMessage(); 
       flood.Attachments.Add(attachment); 

       await context.PostAsync(flood); 

      } 
      else 
      { 

       var reply = context.MakeMessage(); 

       reply.Type = ActivityTypes.EndOfConversation; 
       reply.AsEndOfConversationActivity().Code = code; 

       await context.PostAsync(reply, cancellationToken); 

      } 

     }