Click here to Skip to main content
15,895,746 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.4K   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 description for frmResponse.
	/// </summary>
	public class frmResponse : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Label From;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.ComboBox cboToNode;
		private System.Windows.Forms.ComboBox cboFromNode;
		private System.Windows.Forms.Button cmdOK;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.Button cmdAdvanced;
		private System.Windows.Forms.Label label1;
		
		private Hashtable Nodes;
		private System.Windows.Forms.TextBox txtResponse;
		private Response response;
		ArrayList events = new ArrayList(),stats = new ArrayList();
		double ratio = -1;
		string id = "";
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.NumericUpDown numPriority;
		private System.Windows.Forms.RadioButton rdoPriorityMin;
		private System.Windows.Forms.RadioButton rdoPriorityMax;
		private System.Windows.Forms.RadioButton rdoPriority;
		private System.Windows.Forms.ComboBox cboRspOperator;

		public frmResponse(Hashtable Nodes)
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			this.Nodes = Nodes;
			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.From = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label3 = new System.Windows.Forms.Label();
			this.txtResponse = new System.Windows.Forms.TextBox();
			this.cboToNode = new System.Windows.Forms.ComboBox();
			this.cboFromNode = new System.Windows.Forms.ComboBox();
			this.cmdOK = new System.Windows.Forms.Button();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.cmdAdvanced = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.cboRspOperator = new System.Windows.Forms.ComboBox();
			this.label4 = new System.Windows.Forms.Label();
			this.numPriority = new System.Windows.Forms.NumericUpDown();
			this.rdoPriority = new System.Windows.Forms.RadioButton();
			this.rdoPriorityMin = new System.Windows.Forms.RadioButton();
			this.rdoPriorityMax = new System.Windows.Forms.RadioButton();
			((System.ComponentModel.ISupportInitialize)(this.numPriority)).BeginInit();
			this.SuspendLayout();
			// 
			// From
			// 
			this.From.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.From.Location = new System.Drawing.Point(16, 18);
			this.From.Name = "From";
			this.From.Size = new System.Drawing.Size(72, 16);
			this.From.TabIndex = 2;
			this.From.Text = "From Node";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(16, 274);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(72, 16);
			this.label2.TabIndex = 3;
			this.label2.Text = "To Node";
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(16, 56);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(72, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "Response";
			// 
			// txtResponse
			// 
			this.txtResponse.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtResponse.Location = new System.Drawing.Point(16, 86);
			this.txtResponse.Multiline = true;
			this.txtResponse.Name = "txtResponse";
			this.txtResponse.Size = new System.Drawing.Size(256, 104);
			this.txtResponse.TabIndex = 5;
			this.txtResponse.Text = "This is a demo response......";
			// 
			// cboToNode
			// 
			this.cboToNode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboToNode.Location = new System.Drawing.Point(104, 270);
			this.cboToNode.Name = "cboToNode";
			this.cboToNode.Size = new System.Drawing.Size(168, 21);
			this.cboToNode.Sorted = true;
			this.cboToNode.TabIndex = 6;
			// 
			// cboFromNode
			// 
			this.cboFromNode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboFromNode.Location = new System.Drawing.Point(104, 16);
			this.cboFromNode.Name = "cboFromNode";
			this.cboFromNode.Size = new System.Drawing.Size(168, 21);
			this.cboFromNode.Sorted = true;
			this.cboFromNode.TabIndex = 7;
			// 
			// cmdOK
			// 
			this.cmdOK.Location = new System.Drawing.Point(43, 312);
			this.cmdOK.Name = "cmdOK";
			this.cmdOK.TabIndex = 8;
			this.cmdOK.Text = "&OK";
			this.cmdOK.Click += new System.EventHandler(this.cmdOK_Click);
			// 
			// cmdCancel
			// 
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.Location = new System.Drawing.Point(171, 312);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.TabIndex = 9;
			this.cmdCancel.Text = "&Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.cmdCancel_Click);
			// 
			// cmdAdvanced
			// 
			this.cmdAdvanced.Location = new System.Drawing.Point(197, 50);
			this.cmdAdvanced.Name = "cmdAdvanced";
			this.cmdAdvanced.TabIndex = 10;
			this.cmdAdvanced.Text = "&Advanced";
			this.cmdAdvanced.Click += new System.EventHandler(this.cmdAdvanced_Click);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(16, 210);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(72, 16);
			this.label1.TabIndex = 11;
			this.label1.Text = "Match Type";
			// 
			// cboRspOperator
			// 
			this.cboRspOperator.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.cboRspOperator.Items.AddRange(new object[] {
																"Exact",
																"Contains",
																"Doesnt Contain",
																"Begin With ",
																"End With\t",
																"Any Of The Words",
																"Not Any Of The Words",
																"Contain Words",
																"Does not Contain Words"});
			this.cboRspOperator.Location = new System.Drawing.Point(104, 203);
			this.cboRspOperator.Name = "cboRspOperator";
			this.cboRspOperator.Size = new System.Drawing.Size(168, 21);
			this.cboRspOperator.TabIndex = 12;
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(16, 239);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(72, 16);
			this.label4.TabIndex = 13;
			this.label4.Text = "Priority";
			// 
			// numPriority
			// 
			this.numPriority.Location = new System.Drawing.Point(128, 237);
			this.numPriority.Maximum = new System.Decimal(new int[] {
																		1000,
																		0,
																		0,
																		0});
			this.numPriority.Minimum = new System.Decimal(new int[] {
																		1,
																		0,
																		0,
																		0});
			this.numPriority.Name = "numPriority";
			this.numPriority.Size = new System.Drawing.Size(40, 20);
			this.numPriority.TabIndex = 14;
			this.numPriority.Value = new System.Decimal(new int[] {
																	  1,
																	  0,
																	  0,
																	  0});
			// 
			// rdoPriority
			// 
			this.rdoPriority.Location = new System.Drawing.Point(104, 235);
			this.rdoPriority.Name = "rdoPriority";
			this.rdoPriority.Size = new System.Drawing.Size(16, 24);
			this.rdoPriority.TabIndex = 15;
			// 
			// rdoPriorityMin
			// 
			this.rdoPriorityMin.Location = new System.Drawing.Point(232, 235);
			this.rdoPriorityMin.Name = "rdoPriorityMin";
			this.rdoPriorityMin.Size = new System.Drawing.Size(40, 24);
			this.rdoPriorityMin.TabIndex = 16;
			this.rdoPriorityMin.Text = "Min";
			// 
			// rdoPriorityMax
			// 
			this.rdoPriorityMax.Location = new System.Drawing.Point(176, 235);
			this.rdoPriorityMax.Name = "rdoPriorityMax";
			this.rdoPriorityMax.Size = new System.Drawing.Size(48, 24);
			this.rdoPriorityMax.TabIndex = 17;
			this.rdoPriorityMax.Text = "Max";
			// 
			// frmResponse
			// 
			this.AcceptButton = this.cmdOK;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.SystemColors.InactiveCaptionText;
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(288, 349);
			this.Controls.Add(this.rdoPriorityMax);
			this.Controls.Add(this.rdoPriorityMin);
			this.Controls.Add(this.rdoPriority);
			this.Controls.Add(this.numPriority);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.cboRspOperator);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.cmdAdvanced);
			this.Controls.Add(this.cmdCancel);
			this.Controls.Add(this.cmdOK);
			this.Controls.Add(this.cboFromNode);
			this.Controls.Add(this.cboToNode);
			this.Controls.Add(this.txtResponse);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.From);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Name = "frmResponse";
			this.ShowInTaskbar = false;
			this.Text = "Response Properties";
			this.Load += new System.EventHandler(this.frmResponse_Load);
			((System.ComponentModel.ISupportInitialize)(this.numPriority)).EndInit();
			this.ResumeLayout(false);

		}
		#endregion

		private void frmResponse_Load(object sender, System.EventArgs e)
		{
			
			cboFromNode.Items.Add("New Node");
			cboToNode.Items.Add("New Node");

			IDictionaryEnumerator ide = Nodes.GetEnumerator();
			while(ide.MoveNext())
			{
				cboFromNode.Items.Add(ide.Key);
				cboToNode.Items.Add(ide.Key);
			}
						
			cboFromNode.SelectedIndex = 0;
			cboToNode.SelectedIndex = 0;
			cboRspOperator.SelectedIndex = 7;
			rdoPriorityMax.Checked = true;

			if(this.Tag != null)
			{
				if(this.Tag is string)
				{
					cboFromNode.SelectedItem = this.Tag.ToString();
					return;
				}
				else if(this.Tag is BLog)
				{
					BLog blog = (BLog)this.Tag;
					if(cboFromNode.Items.Contains(blog.FromNode))
						cboFromNode.SelectedItem = blog.FromNode;
					txtResponse.Text = blog.rspTxt;
					return;
				}
				response = (Response) this.Tag;
				cboFromNode.SelectedItem = response.FromNode;
				cboToNode.SelectedItem = response.ToNode;
				txtResponse.Text = response.ResponseText;
				cboRspOperator.SelectedIndex = (int)response.rspOperator;
				numPriority.Value = response.priority;
				rdoPriority.Checked = true;
				this.ratio = response.ratio;
				this.id		= response.id;
				this.events = response.events;
				this.stats = response.stats;
				this.Text = response.id + " Properties";
			}
		}

		private void cmdOK_Click(object sender, System.EventArgs e)
		{
			if(txtResponse.Text == "")
			{
				MessageBox.Show(this, "Response cannot be empty.", Application.Name,
					MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			int priority = 1;
			if(rdoPriorityMax.Checked)
				priority = GetMaxPriority();
			else if(rdoPriorityMin.Checked)
				priority = GetMinPriority();
			else
				priority = (int)numPriority.Value; // A response with same priority shouldn't exist.
			if(isPriorityExist(priority))
			{
				MessageBox.Show(this, "A Response with same priority already exist", Application.Name,
					MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			ResponseOperator rspOp = (ResponseOperator)cboRspOperator.SelectedIndex;
			this.DialogResult = DialogResult.OK;
			Response response = new Response(cboFromNode.SelectedItem.ToString(),cboToNode.SelectedItem.ToString(),
			txtResponse.Text.Trim(), 
			rspOp,
			ratio, priority,this.id,events, stats);
			this.Tag = response;
			this.Close();

		}

		private void cmdAdvanced_Click(object sender, System.EventArgs e)
		{
			frmAdvResponse f = new frmAdvResponse(events, stats,  ratio);
			if(f.ShowDialog(this) == DialogResult.OK)
				this.ratio = (double) f.Tag;
			
		}

		private void cmdCancel_Click(object sender, System.EventArgs e)
		{
			this.Close();
			
		}
		private bool isPriorityExist(int priority)
		{
			bool isPriorityExist = false;

			string nodeID = cboFromNode.SelectedItem.ToString();
			if(nodeID == "New Node")
				return false;
			CtlNode node = (CtlNode)DataSource.Nodes[nodeID];
			for(int i=0; i<node.OutLines.Count; i++)
			{
				Line l = (Line)DataSource.Responses[node.OutLines[i].ToString()];
				if(l.response.priority == priority)
				{
					if(this.response != null && this.response.id == l.ID)
						continue;
					else
					{
						if(MessageBox.Show(this, "A Response with the same priority already exist.\r\r Do you want" + 
							" to increment other responses to set this priority ?", 
							Application.Name, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
						{
							return true;
						}
						else
						{
							isPriorityExist = true;
							break;
						}

					}
				}
			}
			if(!isPriorityExist)
				return false;
			else // cascade increment
			{
				cascadeIncrement(priority, node);
				return false;
			}
		}
		private int GetMaxPriority()
		{
			string nodeID = cboFromNode.SelectedItem.ToString();
			if(nodeID == "New Node")
				return 1;
			CtlNode node = (CtlNode)DataSource.Nodes[nodeID];
			int maxPriority = 0;
			for(int i=0; i<node.OutLines.Count; i++)
			{
				Line l = (Line)DataSource.Responses[node.OutLines[i].ToString()];
				if(l.response.priority > maxPriority)
				{
					maxPriority = l.response.priority ;
				}
			}
			return maxPriority == 0 ? 1: maxPriority + 1;
		}
		private int GetMinPriority()
		{
			string nodeID = cboFromNode.SelectedItem.ToString();
			if(nodeID == "New Node")
				return 1;
			CtlNode node = (CtlNode)DataSource.Nodes[nodeID];

			int minPriority = 1;
			cascadeIncrement(minPriority, node);
			return minPriority;
		}
		private void cascadeIncrement(int priority, CtlNode node)
		{
			int p = -1;
			if((p = chkPriority(priority, node)) >= 0)
				cascadeIncrement(priority + 1, node);
			else
				return;
			Line l = (Line)DataSource.Responses[node.OutLines[p].ToString()];
			l.response.priority = priority + 1;
		}

		private int chkPriority(int priority, CtlNode node)
		{
			
			for(int i=0; i<node.OutLines.Count; i++)
			{
				Line l = (Line)DataSource.Responses[node.OutLines[i].ToString()];
				if(l.response.priority == priority)
				{
					return i;
				}
			}
			return -1;
		}
	}
}

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