How to Write your own IM Bot

Write IM bot This quick tutorial will show you how to develop your own functional IM bot that works with Google Talk, Yahoo! Messenger, Windows Live and all other popular instant messaging clients.

To get started, all you need to know are some very basic programming skills (any language would do) and web space to host your “bot”.

For this example, I have created a dummy bot called “labnol” that listens to your IM messages and return related search phrases based on Google Suggest. To see this live, add   labnol@bot.im to your GTalk buddy list and start chatting.

google-talk-bot

If you like to write a personal IM bot, just follow these simple steps:

Update: Imified is no longer available but you can use Google Scripts to create bots.

Step 1: Go to imified.com and request an invite. You should also give your bot a decent name because you can have just one bot per email address.

Step 2. An email with a secret key should arrive in your Inbox the next minute.

Step 3. Now it’s time to create a bot which is actually a simple script that resides on your public web server. It could be in PHP, Perl, Python or any other language.

This is the source of the PHP script I wrote for the labnol IM bot - pretty self explanatory - it reads your message, gets the relevant data from Google Suggest and echoes it back to the IM window.

<?php
  // Get all the related keywords from Google Suggest
  $u = "http://google.com/complete/search?output=toolbar";
  $u = $u . "&q=" . $_REQUEST['msg'];

  // Using the curl library since dreamhost doesn't allow fopen
  $ch = [curl_init](http://www.php.net/curl_init)();
  [curl_setopt](http://www.php.net/curl_setopt)($ch, CURLOPT_URL, $u);
  [curl_setopt](http://www.php.net/curl_setopt)($ch, CURLOPT_HEADER, 0);
  [curl_setopt](http://www.php.net/curl_setopt)($ch, CURLOPT_RETURNTRANSFER, 1);

  $xml = simplexml_load_string([curl_exec](http://www.php.net/curl_exec)($ch));
  [curl_close](http://www.php.net/curl_close)($ch);

  // Parse the keywords and echo them out to the IM window
  $result = $xml->xpath('//@data');
  [while](http://www.php.net/while) ([list](http://www.php.net/list)($[key](http://www.php.net/key), $value) = [each](http://www.php.net/each)($result)) {
    [echo](http://www.php.net/echo) $value ."<br>";
  }
?>

Step 4: Once your script is ready, put it somewhere on your web server and copy the full URI to the clipboard.

web-url Now login to your imified account, paste the script URL and add that im bot your friends list. That’s it.

This was a very basic bot but the possibilities are endless.

For instance, you could write a bot that will send an email to all your close friends via a simple IM message. Or you could write one that will does currency conversion. See stuff that is already implemented in this list of most useful Google Talk bots.

Amit Agarwal

Amit Agarwal

Google Developer Expert, Google Cloud Champion

Amit Agarwal is a Google Developer Expert in Google Workspace and Google Apps Script. He holds an engineering degree in Computer Science (I.I.T.) and is the first professional blogger in India.

Amit has developed several popular Google add-ons including Mail Merge for Gmail and Document Studio. Read more on Lifehacker and YourStory

0

Awards & Titles

Digital Inspiration has won several awards since it's launch in 2004.

Google Developer Expert

Google Developer Expert

Google awarded us the Google Developer Expert award recogizing our work in Google Workspace.

ProductHunt Golden Kitty

ProductHunt Golden Kitty

Our Gmail tool won the Lifehack of the Year award at ProductHunt Golden Kitty Awards in 2017.

Microsoft MVP Alumni

Microsoft MVP Alumni

Microsoft awarded us the Most Valuable Professional (MVP) title for 5 years in a row.

Google Cloud Champion

Google Cloud Champion

Google awarded us the Champion Innovator title recognizing our technical skill and expertise.

Email Newsletter

Sign up for our email newsletter to stay up to date.

We will never send any spam emails. Promise.