People write the source code for programs in the programming language of their choice, which uses a simple vocabulary of human-readable words to express commands for the computer to follow.
This source code file is then given to a special computer program called a compiler that converts the source code to a language understandable by a computer, usually machine code represented in binary (zeros and ones).
We're going to use the programming language C# (pronounced C Sharp) to program chatbots, although it was an arbitrary choice. No programming language is inherently better than any other - they all have their strengths and weaknesses.
In Windows, each file type has its own file extension, which is identified by the characters following the dot in the file name. For example, somefile.txt has the file extension txt. You'll encounter several file types while coding in C#:
Open TextPad and open the file HelloWorldBot.cs, which is inside the "Your Bots" subfolder of the chatbots folder. Before trying to understand this code, let's compile this code and get a chatbot running.
When compiling your chatbot, make sure that the code file is in the current front window in TextPad. Press CTRL+1 to compile your chatbot code. Once compilation is complete, open Windows Live Messenger and log into a different account than you're using for your chatbot, which you specified in Setup. Add your bot's account as a contact. Now, switch back to TextPad and press CTRL+2 to run the chatbot.
Switch back to Windows Live Messenger. Your chatbot should now be logged in, so go ahead and message it. No matter what you type, the chatbot will respond:
Hello World!
Not very exciting, but this is a good start. Let's go ahead and change the message that chatbot sends back. Switch back to TextPad, press CTRL+TAB until you arrive at the Command Results window, and press any key to kill the chatbot program, which signs the chatbot offline. You can also kill the program from any window in TextPad by pressing CTRL+BREAK. We'll need to do this every time that we want to change the code and relaunch the bots. Now, use CTRL+TAB to switch to HelloWorldBot.cs in TextPad. Find the line that says:
return "Hello World!";
This is the line of code that caused the chatbot to say "Hello World!" for every response. Change the code between the quotes to another message so the chatbot will say something else. Make it clever so you won't get tired of hearing it over and over again. Once your changes are done, compile (CTRL+1) and run (CTRL+2). Switch back to Windows Live Messenger. Message the bot to verify that its response has changed.
Unfortunately, no programmer writes mistake-free code 100% of the time, so it is useful to learn how to debug your programs. Here, we will purposely place a bug in our code and then use feedback from the compiler error and TextPad to fix it.
return "Hello World!";Remove the semicolon from the end of the line.
HelloWorldBot.cs(6,25): error CS1002: ; expected Tool completed with exit code 1This is an error message telling you that the file on line 6, column 25 in HelloWorldBot.cs, a semicolon is expected.
Sometimes compiler error messages can be very cryptic, unlike this straightforward one. Interpreting error messages is a skill gained with experience. Also, the compiler is also not always correct about where the error occurs; the error often occurs on the line before or after the mentioned line. Also, it is not uncommon to have multiple error messages on one compile. In these cases, it is usually best to try and fix the first error message, and then recompile to see if any of the others are corrected by the same change.
Outline Next