Creating your own IM BOT

So one of the cooler things about programming is being able to create stuff like this , i have to say that it is probably what got me into programming in the first place.

What we are going to be making today is a add-on to one of my favorite applications (Miranda IM) that will allow you to create a bot via PHP. Just keep in mind that while i am demonstrating this with PHP there is another plug-in that allows for Python as well.

Things you need to have done :

At this point of the post i need you to have at least installed Miranda IM ( the latest version).

Things we are going to need :

Download the Miranda Scripting Plug-in from here

Getting Started :

First up is installing the mbot plug-in into miranda , this is a lot easier than it sounds .Make sure that you exit Miranda first then simply extract the zip file that was downloaded into the root of your Miranda install (usually C:\Program Files\Miranda IM\) , it should ask you if you want to overwrite the plug-ins folder .At this point say yes to overwrite. Once that is done , start Miranda again then navigate to the options menu and then plug-ins .On the plug-ins page make sure that the mbot plug-in is ticked. Once this is done restart Miranda again.

At this point you should have the scripting engine up and running , if you have any problems drop a comment in the comments section and i will help you out. On start the mbot console pops up by default (as far as i remember) if you see this window then you know all is working .One small tip is that if you find the Pop up window on load annoying like i do , simply navigate to the options page and untick the option ‘Show mbot console on start up’.

To make use of the scripting language in Miranda we are going to have to get acquainted with a bit of PHP , since this is a bit out of the scope of this post i am just going to give a few links that will get you started.

Now to dive into some code quickly.Copy and paste the code below into a text file which you can then rename to helloworld.php

<?php
function mbot_load()
{
//here we’ll register our handler;
mb_SelfRegister(MB_EVENT_MSG_IN,1);
}
//This is the function that gets called when someone sends the bot a messages
function mbe_MsgIn($cid,$body,$timestamp,$known)
{
if($body == “hello”) //Check if the message that was sent to the bot is what we are looking for
{
mb_MsgSend($cid, ‘Hey hwsit going’, 1);//If it is then send the user another message in response to what was sent to us.
return “drop”; //just drop the message
}else{
return 0; //doesn’t match, let others do their jobs
}
}
?>

Now that you have created your file and setup everything correctly its time to do a little explaining.

  • Line 1 : This is php marker , anything found in here is php code.
  • Line 2 : Bot Load Function , this is function that is first called when the script is registered , if we need to register for any events it should be done here.
  • Line 5 : Register for a event , on this line we are registering for the message received event.
  • Line 8 : This is the Message In Function , this is the function that is executed when a new message is received.
  • Line 10 : Check if the message that was sent to us was the word ‘hello’ , if so execute one path of code other wise execute the other path
  • Line 12 : If the message that was sent to us was ‘hello’ , then send a message back to the user say ‘hey Hwist going’. It should be noted that the $cid variable contains the ID of contact that sent us a message. If you knew the ID of another user you could set up message forwarding here.
  • Line 13 : We drop the original message that the user sent us , to store the message as normal return store (although you wont be notified) , to continue as normal return 0.
  • Line 15 : We return 0 as there was nothing else that i wanted to do and let message continue on to the user as normal

Now for the last and final step.

Installing the script :

This is actually very simple depending on how you go about this.

  1. Click on the Miranda Menu  -> Mbot -> Mbot Console . This will bring up the console , now simply drag the file from where ever you created it onto the section of the console where you can type.
  2. mbotconsole

Once that is done any knew messages that are received will now follow the rules in your script.

It should be noted that the other way to install scripts is to click the new button then select script then find the file you want to install.

Now all the stuff i have shown here is still very basic ( your hello world IM BOT) , what you can do from here on is limited mostly to how much you know in PHP. This should also not limit your skills since you can easily hook  .Net into php.

Stuff i have done.

  • Created a Warcraft 3 bot that gave me hero stats for DOTA
  • A salaah time calculator (you ask for the time of a salaah and it would get you the time)
  • A VLC remote control.

For more info and details have a look at the MBOT Wiki

As always these are just a few examples for the things that you can do. Please let me know of there is anything else that i can help with.

~stalkerh

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks

Leave a Reply