CGC_Codes

Chatbot/Console Chat session

Jun 2nd, 2017
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.25 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.CompilerServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace QXS.ChatBot
  9. {
  10.     public class ConsoleChatSession : ChatSessionInterface
  11.     {
  12.         /// <summary>
  13.         /// The session received a messsage
  14.         /// </summary>
  15.         public event Action<ChatSessionInterface, string> OnMessageReceived;
  16.  
  17.         /// <summary>
  18.         /// The session replied to a message
  19.         /// </summary>
  20.         public event Action<ChatSessionInterface, string> OnMessageSent;
  21.  
  22.         public ConsoleChatSession()
  23.         {
  24.             SessionStorage = new SessionStorage();
  25.         }
  26.  
  27.         public string readMessage()
  28.         {
  29.             Console.Write("YOU> ");
  30.             string s = Console.ReadLine();
  31.             if (s != null && OnMessageReceived != null)
  32.             {
  33.                 OnMessageReceived(this, s);
  34.             }
  35.             return s;
  36.         }
  37.         public void sendMessage(string message)
  38.         {
  39.             Console.ForegroundColor = ConsoleColor.Yellow;
  40.             Console.Write("BOT> ");
  41.             Console.WriteLine(message.Replace("\n", "\n     "));
  42.             Console.ResetColor();
  43.             if (message != null && OnMessageSent != null)
  44.             {
  45.                 OnMessageSent(this, message);
  46.             }
  47.         }
  48.  
  49.         public string askQuestion(string message)
  50.         {
  51.             sendMessage(message);
  52.             Console.Write("YOU> ");
  53.             return readMessage();
  54.         }
  55.  
  56.         public bool IsInteractive { get { return true; } set { } }
  57.  
  58.         public SessionStorage SessionStorage { get; set; }
  59.  
  60.  
  61.         public void SetResponseHistorySize(int Size)
  62.         {
  63.             _ResponseHistory = new LinkedList<BotResponse>(_ResponseHistory, Size, false);
  64.         }
  65.         protected LinkedList<BotResponse> _ResponseHistory = new LinkedList<BotResponse>(10, false);
  66.         public void AddResponseToHistory(BotResponse Response)
  67.         {
  68.             _ResponseHistory.Push(Response);
  69.         }
  70.  
  71.         public Stack<BotResponse> GetResponseHistory()
  72.         {
  73.             return new Stack<BotResponse>(_ResponseHistory.GetAsReverseArray());
  74.         }
  75.  
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment