Writing your own SkypeBot

If you use Skype, do you know that you can program against it? Head over to developer.skype.com if you’re interested. There’s a COM API, one for Java and even one for Python.

Just to show how easy it is, we’ll write a bot in .NET that will simply echo whatever is sent to it.

You first need to download Skype4COM, a COM library provided by Skype developers to control Skype.

Create a WinForms application in your favorite version of Visual Studio (>= 2005), and in the default Form class, paste the following piece of code

public partial class MainForm : Form
{
    Skype skype;
    Dictionary<string, Session> userSessions = new Dictionary<string, Session>();

    public MainForm()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        skype = new SkypeClass();
        skype.Attach(5, false);
        skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
    }

    void skype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
    {
        if (Status == TChatMessageStatus.cmsReceived)
        {
            string text = pMessage.Body;
            string response = "Echoing " + text;
            skype.SendMessage(pMessage.FromHandle, response);
        }
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        skype.MessageStatus -= new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
    }
}

Add a reference to Skype4COM and build the code. Make sure Skype is running before launching this application. You’ll need to accept accessing Skype from this application by hitting the “Accept” button when Skype asks- the Skype developers put it in there to avoid malicious code taking control.


That’s all there is to it. If you now send a message to the currently signed in user, you should get back the same message text prefixed by “Echoing”.


There are a lot of other things you could do with the  library, but the documentation is pretty thin, so you’ll have to experiment a bit to get things working.


Still, it’s pretty neat and I can see plenty of situations where a bot running on Skype would be useful. Imagine chatting with your bot running on your home PC, asking it the list of running applications. Skype takes care of NAT, firewall and other network related issues for you. How cool would it be to ask your build server bot the version number of the previous week’s build?

One thought on “Writing your own SkypeBot”

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>