Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#

Bot Suite

Rate me:
Please Sign up or sign in to vote.
1.35/5 (16 votes)
24 Aug 2008LGPL36 min read 94.1K   877   31  
Bot Suite is a collection of utilities provided to create, schedule and run the chat bots. Bot suite consists of three applications i.e. Bot Studio, Bot Engine & MyMessenger. Bot suite provides the solution for the automated conversation using these applications. We will see their usage here.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using BotSuite.MyWebAssembly;


namespace BotSuite.BotStudio
{
	/// <summary>
	/// 
	/// </summary>
	public class frmChat : System.Windows.Forms.Form
	{
				
		private System.ComponentModel.Container components = null;

		private Bot bot;
		public string _nickname = "<bot> ";
		private ArrayList NodeChanges = new ArrayList();

		private System.Windows.Forms.Button cmdSend;
		private System.Windows.Forms.RichTextBox rtfSend;
		private System.Windows.Forms.RichTextBox rtfChat;
		private System.Windows.Forms.Label lblWelcome;

		public frmChat(string botPath)
		{
            InitializeComponent();
			bot = new Bot(botPath);
//			int i = botPath.LastIndexOf("\\");
//			_nickname = botPath.Substring(i+1,botPath.Length -i - 4);
			this.Text = "Chat with " + _nickname;
			bot.BotEvent+=new BotSuite.MyWebAssembly.Bot.botEvent(BotEvent);
		}

		
		/// <summary>
		/// 
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
			
			
		}

		#region Form-Designer Code
		
		private void InitializeComponent()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmChat));
			this.cmdSend = new System.Windows.Forms.Button();
			this.rtfSend = new System.Windows.Forms.RichTextBox();
			this.rtfChat = new System.Windows.Forms.RichTextBox();
			this.lblWelcome = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// cmdSend
			// 
			this.cmdSend.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.cmdSend.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("cmdSend.BackgroundImage")));
			this.cmdSend.Cursor = System.Windows.Forms.Cursors.Hand;
			this.cmdSend.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
			this.cmdSend.Location = new System.Drawing.Point(332, 448);
			this.cmdSend.Name = "cmdSend";
			this.cmdSend.Size = new System.Drawing.Size(53, 51);
			this.cmdSend.TabIndex = 7;
			this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
			// 
			// rtfSend
			// 
			this.rtfSend.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.rtfSend.Location = new System.Drawing.Point(21, 453);
			this.rtfSend.Name = "rtfSend";
			this.rtfSend.Size = new System.Drawing.Size(303, 47);
			this.rtfSend.TabIndex = 8;
			this.rtfSend.Text = "";
			// 
			// rtfChat
			// 
			this.rtfChat.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.rtfChat.HideSelection = false;
			this.rtfChat.Location = new System.Drawing.Point(21, 96);
			this.rtfChat.Name = "rtfChat";
			this.rtfChat.ReadOnly = true;
			this.rtfChat.Size = new System.Drawing.Size(360, 298);
			this.rtfChat.TabIndex = 10;
			this.rtfChat.Text = "";
			// 
			// lblWelcome
			// 
			this.lblWelcome.BackColor = System.Drawing.Color.Transparent;
			this.lblWelcome.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.lblWelcome.Location = new System.Drawing.Point(56, 16);
			this.lblWelcome.Name = "lblWelcome";
			this.lblWelcome.Size = new System.Drawing.Size(336, 23);
			this.lblWelcome.TabIndex = 11;
			this.lblWelcome.Text = "Welcome";
			// 
			// frmChat
			// 
			this.AcceptButton = this.cmdSend;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = new System.Drawing.Size(403, 557);
			this.Controls.Add(this.lblWelcome);
			this.Controls.Add(this.rtfChat);
			this.Controls.Add(this.rtfSend);
			this.Controls.Add(this.cmdSend);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "frmChat";
			this.Text = "Chat with Bot";
			this.Load += new System.EventHandler(this.frmChat_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void OutgoingMessage(string msg)
		{
			rtfChat.SelectionColor = Color.Blue;
			rtfChat.AppendText("Me said: \r\n");
			rtfChat.SelectionColor = Color.Black;
			rtfChat.AppendText("  " + msg);
			rtfChat.AppendText("\r\n");
		}

		public void IncomingMessage(string msg)
		{
			rtfChat.SelectionColor = Color.Red;
			rtfChat.AppendText(_nickname + " said: \r\n");
			rtfChat.SelectionColor = Color.Black;
			rtfChat.AppendText("  " + msg);
			rtfChat.AppendText("\r\n");
			rtfChat.Focus();
		
		}

		private void cmdSend_Click(object sender, System.EventArgs e)
		{
			OutgoingMessage(rtfSend.Text.Trim());
			IncomingMessage(bot.getNext(rtfSend.Text.Trim()));
			rtfSend.Text = "";
			rtfSend.Focus();
			if(bot.LastMessage)
				bot_ChatEnd();
		}

		private void BotEvent(ArrayList events)
		{
			for(int i=0; i<events.Count; i++)
			{
				Event evt = (Event)events[i];
				MessageBox.Show(this,
					"To: " + evt.email + "\r\n Type: " + evt.MType.ToString() + "\r\n Message: " + evt.message,
					Application.Name,
					MessageBoxButtons.OK,
					MessageBoxIcon.Information);
			}
		}

		private void bot_ChatEnd()
		{
			MessageBox.Show(this,
				"Chat with the bot ended.",
				Application.Name,
				MessageBoxButtons.OK,
				MessageBoxIcon.Information);
			this.Close();
		}

		private void frmChat_Load(object sender, System.EventArgs e)
		{
			string q = "";
			try
			{
				bot.initialize();
				q = bot.getFirstMessage();
			}
			catch(Exception ex)
			{
				MessageBox.Show(this, "Error: " + ex.Message, Application.Name, 
					MessageBoxButtons.OK, MessageBoxIcon.Error);
				this.Close();
				return;
			}
			
			IncomingMessage(q);
		}

		
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Web Developer
Pakistan Pakistan
Mansoor Sarfraz works in a well reputed multinational software development company. Software development is not only his duty but his passion too. In his professional career he was Involved in the development of resource/content management system, workflow based systems, enterprise projects based on MS Windows DNA architecture and .NET framework, web based rich client UI development, Rest based backend web services and windows services for different systems. He was also involved in software designing and architecture. He has expertise in C#.NET, ASP.NET, Sql Server, Adobe Flex, Java, Ajax and JavaScript.

You can reach him through his blog
http://mansoorsarfraz.blogspot.com

Comments and Discussions