how to make a chat botOne of the great things about owning a website and being an online writer is the opportunity to connect with so many people across the world.
Unfortunately, there is one difficulty with connecting with so many people across the world – time zones. You may want to be available to interact with your readership or to do business with potential clients, but at some point, you have to sleep.
Enter the chat bot. A chat bot can serve as your front desk digital secretary when you aren’t available to accept IM requests. The intelligence of this digital “secretary” really only comes down to how well you’re able to program the dialogue into your chat bot.
MUO has previously covered ChitterIM, which is essentially a chat bot that accepts IM commands from you in order to interact with Twitter. This is just one example of how a chat bot can automatically perform tasks for your site or your business, but in this article I’m going to show you the basic steps to make your own chat bot for your own site or business.

How to Make a Chat Bot

By far, the best available tool for doing this is IMified. We’ve briefly covered IMified in the directory, and Mark covered how to use the prebuilt IMified bot to use a whole list of premade applications that the folks at IMified already offer.
However, you can also create your own customized chat bot by signing up for a new account, and then clicking on the “Create a New Bot” button on the right menu.
how to make a chat bot
Setting up your own customized chat bot is as simple as creating an ID for your bot, and defining the URL where your bot’s chat code will reside. In my case, I’m storing the PHP code for the bot programming on my own website, so I’ve provided the path to that PHP file.
make a chat bot
When you’re done, you’ll receive a long “Bot Key” that you can use when you get more deeply into integrating your bot into various services, such as programming your own Twitter features. However, for now we’re going to focus on creating that basic PHP file where you can program your bot to interact with your users.
make a chat bot
To show how this basically works, I’m going to explain how the basic PHP file offered by IMified works. Here’s what the basic interaction code looks like.
<?php
switch ($_REQUEST['step']) {
case 1:
{echo "Hi, I'm the TSW Support bot, do you already know what service you want?";
break;}
case 2:
{echo "You've requested service " . $_REQUEST['value1'] . ", is that correct?";
break;}
case 3:
{echo "Thank you for requesting service " . $_REQUEST['value1'] . "<br><br>You said " . $_REQUEST['value2'] . " that you know.<reset>";
break;}
}
?>
If you step through this sample code you’ll see just how simple it is. The IMified bot lets you take the conversation through several “steps”, and you can force navigation. You can allow the user to type something and continue to the next step by doing nothing and just ending that section with “break;”. If you end your echo with a “”, it’ll reset the bot (and the conversation) back to the start. Your bot will forget everything that was recently said.
IMified also lets you use a “goto” command to go to a specific step in the conversation, and will force the conversation one step back (I’ll show you how this works below).
But first, here’s what the bot conversation will look like. The bot is called “tswdesk”.
make a chat bot
Of course, you’re going to want your chat bot to be a little more intelligent (and interesting) than this simple example. However, since you are controlling the conversation using PHP code, just think of the possibilities. Based on the conversation, you could perform different services for your readers or your clients. Send out an email, log “feedback” test to a file, or provide the user with requested information. The possibilities are really unlimited.
Here’s a sample PHP bot code where you can provide the visitor with the option to select from a menu of “services” that your bot offers.
<?php
switch ($_REQUEST['step']) {
case 1:
{echo "Hi, I'm the TSW Support bot, do you already know what service you want?";
break;}
case 2:
{
if ($_REQUEST['msg'] == "yes") {
    echo "Cool, go ahead and type your request.";
    break;
} elseif ($_REQUEST['msg'] == "no") {
    echo "Here are our service commands:<br><br>1-Subscribe to newsletter<br>2-Request to talk to editor<br>3-Submit Feedback<br>4-Subscribe to RSS Feed<br>5-Request a phone call<br><br>Please type your request:";
    break;
} else {
    echo "I don't understand your answer, try again. <error>";
    break;
}
}
case 3:
{
if ($_REQUEST['msg'] == "1") {echo "Thank you. I've subscribed you to our newsletter using your IM email account. Have a nice day.<reset>";}
if ($_REQUEST['msg'] == "2") {echo "Thank you. I've let our editor know that you would like to speak with him. Have a nice day.<reset>";}
if ($_REQUEST['msg'] == "3") {echo "Thank you. Please type your feedback now.";}
if ($_REQUEST['msg'] == "4") {echo "Thank you. Add our RSS url to your Reader to subscribe: http://www.topsecretwriters.com/rss. Have a nice day.<reset>";}
if ($_REQUEST['msg'] == "5") {echo "Thank you. You've requested a phone call. Please type your phone number.";}  
 
break;
 
}
 
case 4:
{
   echo "Thank you. Your information has been submitted. Have a nice day.<reset>";
   break; 
}
}
?>
Now that there’s a bit more logic and information in the background code, you can see how much more intelligent the bot will appear to users, as shown in the conversation below.
how to make a chat bot
Now, carrying out some of these tasks may be as simple as writing to a logfile or sending out an email to the editor using PHP, but if you want to have your bot serve you by accepting Twitter commands or issuing alerts to all chat “friends”, it will require using some of the IMified APIs. That’s outside the scope of this article, but we’ll get to those cool features in an upcoming article.
Have you ever used IMified to customize your own bot, or have you used any other similar service? Share your chat bot experiences and insights in the comments section below.