AI – C# – Chatbot with Random Responses

This is a chatbot I created to demonstrate two main concepts: Scanning inputs, and returning random responses. With this, you can help the chatbot build its intelligence by adding a variety of acceptable inputs and relative responses. The program will randomly choose from those responses. Written and tested in Visual Studio 2017.

The dialog form is where you will converse with Jarvis. The top field is an input textbox. The Jarvis responses are in a form label that gets updated. The full project: VS_Project.zip

Form1.cs

using System;
using System.Linq;
using System.Windows.Forms;  // Win Form

namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

String s;

int i;

Random r = new Random();

private void textBox1_TextChanged(object sender, KeyEventArgs e)
{

if (e.KeyCode == Keys.Enter) {

s = textBox1.Text.ToLower();

// input cleanup
s = s.Replace("'", "");
s = s.Replace(".", "");
s = s.Replace(",", "");
s = s.Replace("?", "");

Logic();

}

if (e.KeyCode == Keys.RShiftKey) textBox1.Text = null;

}

public void Logic()
{

// greetings
bool user_1 = new[] { "hello", "hi", "yo", "greetings", "hello jarvis", "hi jarvis" }.Any(c => s.Equals(c));
String[] pc_Response1 = { "Hello. How are you?" };

// greeting responses
bool user_2 = new[] { "im good", "good", "im great", "i am good", "im doing fine", "im okay" }.Any(c => s.Contains(c));
String[] pc_Response2 = { "That's great. What's going on?", "Good. What's happening?", "Okay, that's great. What's happening?" };

// leaving
bool user_3 = new[] { "leaving", "i gotta go", "im leaving", "got to go", "bye" }.Any(c => s.Contains(c));
String[] pc_Response3 = { "Okay, nice meeting.", "Later, my friend", "Okay, good bye" };

// check true for output
if (user_1) { OutputMessage(pc_Response1); }
else if (user_2) { OutputMessage(pc_Response2); }
else if (user_3) { OutputMessage(pc_Response3); }
else { NoAnswer(); }

// session
user_1 = false;
user_2 = false;
user_3 = false;
}

public void OutputMessage(String[] msg)
{
i = r.Next(msg.Length);
this.output.Text = msg[i]; textBox1.Text = "";

}

public void NoAnswer()
{
String[] output = { "I do not understand.", "Can you rephrase the question?", "My responses are limited. Ask something else." };
this.output.Text = output[i];
textBox1.Text = "";
}
}
}

Form1.Designers.cs

namespace WindowsFormsApp2
{
partial class Form1
{

//

<summary>
// Required designer variable.
// </summary>private System.ComponentModel.IContainer components = null;

//

<summary>
// Clean up any resources being used.
// </summary>// <param name="disposing">true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing &amp;&amp; (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.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.output = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBox1.Location = new System.Drawing.Point(12, 12);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(511, 26);
this.textBox1.TabIndex = 0;
this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.textBox1_TextChanged);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(8, 67);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(60, 20);
this.label1.TabIndex = 1;
this.label1.Text = "Jarvis:";
//
// output
//
this.output.AutoSize = true;
this.output.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.output.Location = new System.Drawing.Point(62, 67);
this.output.Name = "output";
this.output.Size = new System.Drawing.Size(29, 20);
this.output.TabIndex = 2;
this.output.Text = ".....";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(535, 162);
this.Controls.Add(this.output);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Jarvis AI";
this.ResumeLayout(false);
this.PerformLayout();

}

#endregion

private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label output;
}
}