skip navigation
Previous Next

The HandleMessage Method: Using Parameters

Clearly, to make a better chatbot, we will need to able to examine the message received before issuing a response. From the last section, we know that string data can be stored in variables of type string. Luckily, the HandleMessage method already provides some pre-assigned variables for us, called parameters.

Look at the following HandleMessage method from HelloWorldBot. Can you find the parameters?

public override string HandleMessage(string message, string user) {
	return "Hello World!";

Two parameters are defined on the same line as the method name. This line is called the method header. The parameters are contained inside the parentheses right after the method name, and are separated by commas:

string message
Contains the most recent message sent to the chatbot.
string user
Contains the username of the person that most recently sent a message.

The parameters are different than other variables we have seen because we cannot know their values beforehand. However, this is the true power of parameters. Let's try an example:

Instead of having the bot say the same thing for every response, let's have it reply back with the message that was received. To do this, change this line:

return "Hello World!";

to this:

return message;

This will send the person's message to the bot right back to them. Compile, run, and switch back to Windows Live Messenger to test out your new bot. If you're having trouble getting this bot to work, here's an example solution: BouncerBot.cs.

This code is very similar to the HandleMessage method in HelloStringsBot.cs from the previous example. In HelloStringsBot, we returned a string that we created and could modify. In BouncerBot, we just returned the value stored inside the parameter, which is a variable that already has a value.

Previous Next
This work is supported by the following National Science Foundation Grants: CNS-087508, CNS-0549481, IIS-0811884, IIS-0415273
Send comments to Richard Ladner