Click here to Skip to main content
15,886,724 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.2K   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 agsXMPP;
using agsXMPP.protocol;
using agsXMPP.protocol.client;
using agsXMPP.Collections;

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

		private XmppClientConnection	_connection;
		private Jid						m_Jid;
		private System.Windows.Forms.Button cmdSend;
		private System.Windows.Forms.RichTextBox rtfSend;
		private System.Windows.Forms.RichTextBox rtfChat;
		private System.Windows.Forms.Label lblWelcome;
		private System.Windows.Forms.Label label1;
		private string					_nickname;

		
		public frmChat(Jid jid, XmppClientConnection con, string nickname)
		{
			m_Jid		= jid;
			_connection = con;
			_nickname	= nickname;
			InitializeComponent();
			
			label1.Text = "To: " + (nickname == "" ? jid.ToString(): nickname);
			lblWelcome.Text = con.MyJID + " - " + con.Status;
			this.Text = "Conversation - " + (nickname == "" ? jid.ToString(): nickname) ;
			
			Util.Forms.Add(m_Jid.Bare.ToLower(), this);

			// Setup new Message Callback
			con.MesagageGrabber.Add(jid, new BareJidComparer(), new MessageCB(MessageCallback), null);
		}

		public Jid Jid
		{
			get { return m_Jid; }
			set { m_Jid = value; }
		}
		/// <summary>
		/// 
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
			
			Util.Forms.Remove(m_Jid.Bare.ToLower());
			_connection.MesagageGrabber.Remove(m_Jid);
			_connection = null;
		}

		#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.label1 = 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 = 1;
			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 = 0;
			this.rtfSend.Text = "";
			// 
			// rtfChat
			// 
			this.rtfChat.BorderStyle = System.Windows.Forms.BorderStyle.None;
			this.rtfChat.Location = new System.Drawing.Point(21, 96);
			this.rtfChat.Name = "rtfChat";
			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";
			// 
			// label1
			// 
			this.label1.BackColor = System.Drawing.Color.Transparent;
			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(32, 74);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(352, 16);
			this.label1.TabIndex = 12;
			this.label1.Text = "Messages";
			// 
			// 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.label1);
			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.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Name = "frmChat";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
			this.Text = "Conversation";
			this.Load += new System.EventHandler(this.frmChat_Load);
			this.ResumeLayout(false);

		}
		#endregion

		private void OutgoingMessage(agsXMPP.protocol.client.Message msg)
		{
			rtfChat.SelectionColor = Color.Blue;
			rtfChat.AppendText("Me said: \r\n");
			rtfChat.SelectionColor = Color.Black;
			rtfChat.AppendText("  " + msg.Body);
			rtfChat.AppendText("\r\n");

			rtfSend.Text = "";
			rtfChat.Focus();
			rtfSend.Focus();
			
		}

		public void IncomingMessage(agsXMPP.protocol.client.Message msg)
		{
			if(msg.Type == MessageType.error)
			{
				MessageBox.Show(this,"Error in Message: " + msg.Body + "\n From : " + msg.From, 
					Application.Name, MessageBoxButtons.OK,
					MessageBoxIcon.Error);
			}
			else
			{
				rtfChat.SelectionColor = Color.Red;
				rtfChat.AppendText(_nickname + " said: \r\n");
				rtfChat.SelectionColor = Color.Black;
				rtfChat.AppendText("  " + msg.Body);
				rtfChat.AppendText("\r\n");
				
				rtfChat.ScrollToCaret();
				rtfChat.Focus();
				rtfSend.Focus();
			}
		}

		private void cmdSend_Click(object sender, System.EventArgs e)
		{
			agsXMPP.protocol.client.Message msg = new agsXMPP.protocol.client.Message();

			msg.Type	= MessageType.chat;
			msg.To		= m_Jid;
			msg.Body	= rtfSend.Text.Trim();
			
			_connection.Send(msg);
			OutgoingMessage(msg);
			
		}

		private void MessageCallback(object sender, agsXMPP.protocol.client.Message msg, object data)
		{
			IncomingMessage(msg);
		}

		private void frmChat_Load(object sender, System.EventArgs e)
		{
		
		}

	
		


		
	}
}

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