Click here to Skip to main content
11,432,168 members (52,489 online)
Click here to Skip to main content

Enhanced Skype Chatter Robot

, 4 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
An Enhanced Skype Chatter Bot, with a friendly user interface, programable knowledge base, testing interface with Export/Import knowledge base to files
Sample Image

Introduction

This piece of software enables the user to have a programmable ChatBot. Where most of the tutorials circulating on the internet have their knowledge base hard coded (obviously for demonstration purposes), here we will see that it is possible to add and edit rules to knowledge base during runtime, and the database is automatically saved to a file on storage.

Second thing that this program does is it doesn't simply compare strings to search for answers, but it uses a different strategy to calculate resemblance between strings, and this sort of overcomes typing errors, misspelled words, or inverted phrases.

In addition to the features mentioned above, it has several secondary features that enhance user experience like a testing section, message delaying, greeting message, and the ability to load and backup database into files.

Background

Originally, this was an application that I made previously for someone who had nothing to do with any programing. I found many examples that do illustrate how it should be basically done, but for my end-users, several things needed to be enhanced like:

  • User friendly interface
  • An editable knowledge base via the GUI
  • A way to overcome typing errors, misspelled words, etc.
  • Message delaying

I have based my work on this CodeProject article, Make your Skype Bot in .NET, as it was a nice 'scratch the surface' with the Skype4COM COM wrapper, and it's highly suggested to check it out as it contains a lot of definitions that will not be covered in this article.

Code Explanation

String Comparison Strategy and Message Processing

It is obvious that normal string comparison will only match strings character by character, which is not very effective when dealing with messages that were input by humans. In the case like we have here, strings are commonly compared by Resemblance. i.e., we will have to calculate the rate of how much do a pair of strings look alike. There are many algorithms and approaches to do such a comparison, for the sake of this project we will be using the Levenshtein distance.

Message processing is summarized in the following diagram :

Source code preview of the actual method for message processing is as follows:

// this method is the core of our message processing
private string ProcessCommand(string msg,bool showSimilarity)
{
	// remove all special characters, punctuation etc ...
	msg = preTreatMessage(msg);

	// we build our answer search query
	string answer="";
	string searchQuery = "";

	// we tokenize the given message
	string[] tokens = msg.Split(' ');
	string oper = "";
	// we search for all rules with the 'receive' that looks like the given message
	foreach (string token in tokens)
	{
		if (token != "")
		{
			searchQuery += oper + " receive LIKE '%" + token + "%' ";
			oper = "OR";
		}
	}
	// we store the results
	DataRow[] results = this.database.Rules.Select
	("active = True AND "+searchQuery);
	// set the minimum wordsMatching score (precision) 
	// messages that are matched below this threshold will be ignored
	double maxScore = this.intelligence;
	List<string> answerlist =new List<string>();
	// we calculate the matching score foreach rule and store the ones
	// with the highest score in the answerlist array
	foreach (DataRow result in results)
	{
		MatchsMaker m = new MatchsMaker(msg, result["receive"].ToString());
		if (m.Score > maxScore)
		{
			answerlist.Clear();
			answerlist.Add(result["send"] + ((showSimilarity)?" 
			(" + m.Score + ")":""));
			this.messageDelay = (int)result["delay"];
			maxScore = m.Score;
		}
		else if (m.Score == maxScore)
		{
			answerlist.Add(result["send"] + 
			((showSimilarity)?" (" + m.Score + ")":""));
			this.messageDelay = (int)result["delay"];
		}
	}
	if (answerlist.Count == 0) // if no rule was found (scored high)
	{
		// we give an empty message
		// OR , you can modify this to be a default message
		// like : "I dont understand !"
		answer = "";
	}else{
		// if there are rules with the same score
		// we just choose a random one
		Random rand = new Random();
		answer = answerlist[rand.Next(0,answerlist.Count)];
	}
	return answer;
}

Message Delaying

This feature was required to make the bot more realistic, a delay amount is assigned to each rule; However it can be done differently for example, we can count the characters of the response in order to avoid setting the delay every time we add a new rule.

Another thing that is not covered is to make Skype show that we are currently typing a message, during the message delaying.

Here is a preview of the method (Thread) for delaying the message, its simply sleeps for the amount of seconds set by the message.

private void SendSkypeMessage(string username, string message)
{
	// the message is not empty
	if (message != "")
	{
		// pause the thread delay the message so it appears as if the bot types the message
		System.Threading.Thread.Sleep(1000*this.messageDelay);
		try
		{
			// send the actual message
			skype.SendMessage(username, message);
		}
		catch { }
	}
}

It is called within the skype_MessageStatus callback method:

// create a thread to send the message with its delay
System.Threading.Thread t = new System.Threading.Thread(() => 
	SendSkypeMessage(msg.Sender.Handle, send));
t.Start();

History

  • August 2013: Released

Related Material

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Osman Kalache
Software Developer Smart Solutions Médéa
Algeria Algeria
No Biography provided

Comments and Discussions

 
Questionhelp? Pin
Member 1098146030-Jul-14 8:01
memberMember 1098146030-Jul-14 8:01 
AnswerRe: help? Pin
Member 1098146030-Jul-14 8:03
memberMember 1098146030-Jul-14 8:03 
GeneralRe: help? Pin
Member 1136625610-Jan-15 21:02
memberMember 1136625610-Jan-15 21:02 
GeneralRe: help? Pin
Osman Kalache11-Jan-15 2:32
memberOsman Kalache11-Jan-15 2:32 
GeneralVERSION Pin
Member 1088737216-Jun-14 5:21
memberMember 1088737216-Jun-14 5:21 
QuestionNice if it worked Pin
Member 1088737216-Jun-14 5:03
memberMember 1088737216-Jun-14 5:03 
AnswerRe: Nice if it worked Pin
Osman Kalache16-Jun-14 5:06
memberOsman Kalache16-Jun-14 5:06 
GeneralRe: Nice if it worked Pin
Member 1088737216-Jun-14 5:07
memberMember 1088737216-Jun-14 5:07 
GeneralRe: Nice if it worked Pin
Osman Kalache16-Jun-14 5:09
memberOsman Kalache16-Jun-14 5:09 
GeneralRe: Nice if it worked Pin
Member 111999832-Nov-14 5:44
memberMember 111999832-Nov-14 5:44 
GeneralRe: Nice if it worked Pin
Osman Kalache2-Nov-14 5:46
memberOsman Kalache2-Nov-14 5:46 
Questionehhhh Pin
Member 1087894112-Jun-14 11:18
memberMember 1087894112-Jun-14 11:18 
QuestionHow do you start it? Pin
Member 1078718430-Apr-14 20:37
memberMember 1078718430-Apr-14 20:37 
BugPost 5.3.0.116 Pin
Timberwolf Programmers6-Apr-14 13:37
professionalTimberwolf Programmers6-Apr-14 13:37 
QuestionHow to trigger it Pin
Member 1070624028-Mar-14 1:34
memberMember 1070624028-Mar-14 1:34 
AnswerRe: How to trigger it Pin
Osman Kalache28-Mar-14 1:53
memberOsman Kalache28-Mar-14 1:53 
QuestionBOT with JOB SITES Pin
DJChrisBlue17-Jan-14 14:59
memberDJChrisBlue17-Jan-14 14:59 
QuestionMore Pin
Paulo Augusto Künzel24-Oct-13 3:09
professionalPaulo Augusto Künzel24-Oct-13 3:09 
AnswerRe: More Pin
Osman Kalache3-Nov-13 6:35
memberOsman Kalache3-Nov-13 6:35 
GeneralMy vote of 5 Pin
MuhsinEmre5-Aug-13 21:39
memberMuhsinEmre5-Aug-13 21:39 
SuggestionNot a tip Pin
Vitaly Tomilov4-Aug-13 14:47
memberVitaly Tomilov4-Aug-13 14:47 
GeneralRe: Not a tip Pin
Paulo Augusto Künzel24-Oct-13 3:09
professionalPaulo Augusto Künzel24-Oct-13 3:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150428.2 | Last Updated 4 Aug 2013
Article Copyright 2013 by Osman Kalache
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid