Click here to Skip to main content
15,860,859 members
Articles / Programming Languages / C#

Make your Skype Bot in .NET

Rate me:
Please Sign up or sign in to vote.
4.83/5 (25 votes)
7 Jul 2009CPOL3 min read 268.2K   10.6K   103   63
This article explains how to make a Skype bot in .NET.

skypebot.jpg

Introduction

The motive of this article is to explain:

  • How to make Skype work with .NET.
  • How to make an answering bot.

When you type a command !Date (say), it will display the current date. You can extend this application to make your own bots which can handle your own requests.

Some months earlier, I wrote a Skype plug-in for Cropper (an Open Source screen capture tool) called SendToSkype based on the same concept. You can check it here if you wish.

Prerequisites

  • Skype. I used version 4.0.0.227.
  • I used Visual C# 2008 Express Edition. But you can use any edition.
  • COM wrapper - Skype4COM which you an download here. I used release 1.0.31.0 which is the latest available at the time of writing this article.

What is the Skype4COM COM wrapper?

Skype4COM is an interface which represents the Skype API as objects, with properties, commands, and events and notifications. Use Skype4COM in any ActiveX environment, such as Visual Studio or Delphi, and use familiar scripting languages, such as Visual Basic, PHP, or JavaScript. Read more here.

When you develop an app from scratch...

  1. Download Skype4COM.dll.
  2. 'Add Reference' to this DLL on your project.

Sample code

Below is the code of the sample application attached. I would like to paste the whole code here since it is only a few lines.

C#
using System;
using System.Windows.Forms;
using SKYPE4COMLib; // Our COM library

namespace SkypeBing
{
    public partial class Form1 : Form
    {
        private Skype skype;
        private const string trigger = "!"; // Say !help
        private const string nick = "BOT";
        
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            skype = new Skype();
            // Use skype protocol version 7 
            skype.Attach(7, false); 
            // Listen 
            skype.MessageStatus += 
              new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);
        }
        private void skype_MessageStatus(ChatMessage msg, 
                     TChatMessageStatus status)
        {
            // Proceed only if the incoming message is a trigger
            if (msg.Body.IndexOf(trigger) == 0)
            {
                // Remove trigger string and make lower case
                string command = msg.Body.Remove(0, trigger.Length).ToLower();

                // Send processed message back to skype chat window
                skype.SendMessage(msg.Sender.Handle, nick + 
                      " Says: " + ProcessCommand(command));
            }
        }

        private string ProcessCommand(string str)
        {
            string result;
            switch (str)
            {
                case "hello":
                    result = "Hello!";
                    break;
                case "help":
                    result = "Sorry no help available";
                    break;
                case "date":
                    result = "Current Date is: " + 
                             DateTime.Now.ToLongDateString();
                    break;
                case "time":
                    result = "Current Time is: " + 
                             DateTime.Now.ToLongTimeString();
                    break;
                case "who":
                    result = "It is Praveen, aka NinethSense " + 
                             "who wrote this tutorial";
                    break;
                default:
                    result = "Sorry, I do not recognize your command";
                    break;
            }

            return result;
        }
    }
}

Remember!

When you try to run this application, make sure Skype is also running. You will be getting a dialog like the one below requesting your permission to communicate your app with Skype.

Screenshot_1.jpg

So do not get nervous. It is nothing unusual.

Source code explanation

Declaration

Once you have referenced Skype4COM.dll, you will be able to use SKYPE4COMLib which is our core Skype communication component.

C#
using SKYPE4COMLib;

Next, I created a private object skype of type SKYPE4COMLib.Skype so that I can use this on all the inner methods (Form1_Load(), skype_MessageStatus(), and ProcessCommand()).

I used two identifiers to store the trigger key and a nick name so that the ordinary chat messages we use in Skype will not have confusions with the triggers.

C#
private const string trigger = "!"; // Say !help
private const string nick = "BOT";

An example of a chat looks like this:

>>Blah
>>!hello
>>BOT Says: Hello!

You can change the trigger and nick according to your taste.

Create a Skype object and attach to Skype

C#
skype = new Skype(); // Usual c# object creation
skype.Attach(7, false);

Here, 7 is a Skype protocol.

Listen to incoming messages

You need to create a new event based on _ISkypeEvents_MessageStatusEventHandler for this.

C#
skype.MessageStatus += 
  new _ISkypeEvents_MessageStatusEventHandler(skype_MessageStatus);

All the messages you type in the Skype chat window will be monitored and passed to a method named skype_MessageStatus() for processing the incoming messages/commands.

Process commands

Here comes the core bot operation. In the method skype_MessageStatus, our application checks whether the incoming message is a command. Also, to avoid case sensitivity, I convert the message to lower case so that in the processing method (ProcessCommand()), I need to check only the lowercase commands.

C#
if (msg.Body.IndexOf(trigger) == 0)
{
    // Remove trigger string and make lower case
    string command = msg.Body.Remove(0, trigger.Length).ToLower();

    // Send processed message back to skype chat window
    skype.SendMessage(msg.Sender.Handle, nick + " Says: " + 
                      ProcessCommand(command));
}

skype.SendMessage() is the command to send a string to the Skype text window. msg.Sender.Handle defines to which Skype user this message is to be send.

Here comes the command processing part. This method simply checks the incoming commands and returns a pre-defined text. I am sure not much explanation is required here.

C#
private string ProcessCommand(string str)
{
    string result;
    switch (str)
    {
        case "hello":
            result = "Hello!";
            break;
        case "help":
            result = "Sorry no help available";
            break;
        case "date":
            result = "Current Date is: " + DateTime.Now.ToLongDateString();
            break;
        case "time":
            result = "Current Time is: " + DateTime.Now.ToLongTimeString();
            break;
        case "who":
            result = "It is Praveen, aka NinethSense who wrote this tutorial";
            break;
        default:
            result = "Sorry, I do not recognize your command";
            break;
    }

    return result;
}

You can see it is very easy to extend the number of commands by simply adding more case statements.

History

  • Initial post: 07, July 2009.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect ORION INDIA SYSTEMS
India India
Praveen.V.Nair - aka NinethSense - PMP, Microsoft MVP - is working as a Head of Technology and Architecture at Orion India Systems, Kochi, India. He has been playing with electronics from the age of 10 and with computers from the age of 14. He usually blogs at http://blog.ninethsense.com/.

Comments and Discussions

 
Questioncan I send attachmen without using AxSkype1.Client.OpenFileTransferDialog(skypeID, "") Pin
hareshdgr829-Jan-15 21:50
hareshdgr829-Jan-15 21:50 
QuestionNot Working Anymore - Skype Kit is Deprecated Pin
Mikloo24-Jan-15 8:14
Mikloo24-Jan-15 8:14 
AnswerRe: Not Working Anymore - Skype Kit is Deprecated Pin
Member 114451982-Sep-16 5:45
Member 114451982-Sep-16 5:45 
QuestionEmulate typing Pin
Member 1118712213-Nov-14 2:47
Member 1118712213-Nov-14 2:47 
QuestionMessageStatusEventHandler problem Pin
Member 1047049529-Aug-14 12:14
Member 1047049529-Aug-14 12:14 
QuestionNot working? Pin
LaimMc23-Sep-13 4:43
LaimMc23-Sep-13 4:43 
QuestionHow to run in windows 7 64bit Pin
Sunil Mahanta14-Aug-13 21:58
Sunil Mahanta14-Aug-13 21:58 
QuestionUpdates Pin
kiquenet.com11-Aug-13 20:48
professionalkiquenet.com11-Aug-13 20:48 
QuestionError (1) Pin
Topias Jäppilä8-May-13 2:57
Topias Jäppilä8-May-13 2:57 
Questionsame code in VB. net Pin
Member 80328084-Apr-13 3:04
Member 80328084-Apr-13 3:04 
QuestionHave I miss anything.. Pin
Kasie20-Jan-13 5:34
Kasie20-Jan-13 5:34 
QuestionHow make this on VC++? I no have a skype.MessageStatus...??? Pin
xman91115-Jan-13 6:54
xman91115-Jan-13 6:54 
AnswerFixes and Handy tools Pin
Xander43997-Jan-13 4:47
Xander43997-Jan-13 4:47 
GeneralRe: Fixes and Handy tools Pin
MrPonyCaptain11-May-13 11:38
MrPonyCaptain11-May-13 11:38 
SuggestionCalling twice fix Pin
Gelidus7-Nov-12 11:12
Gelidus7-Nov-12 11:12 
GeneralRe: Calling twice fix Pin
Kameron Schwab14-Oct-15 17:11
Kameron Schwab14-Oct-15 17:11 
QuestionSkype4Com.MSM, what is it? Pin
Onur Guzel1-Sep-12 7:31
Onur Guzel1-Sep-12 7:31 
Questionerror while starting skypebing.exe Pin
djorno101-May-12 0:59
djorno101-May-12 0:59 
QuestionSkypeBot Group Chat Pin
bearbear1234516-Feb-12 18:45
bearbear1234516-Feb-12 18:45 
AnswerRe: SkypeBot Group Chat Pin
iPSiArt28-Feb-12 3:42
iPSiArt28-Feb-12 3:42 
GeneralRe: SkypeBot Group Chat Pin
bearbear1234528-Feb-12 20:31
bearbear1234528-Feb-12 20:31 
GeneralRe: SkypeBot Group Chat Pin
bearbear1234529-Feb-12 1:46
bearbear1234529-Feb-12 1:46 
GeneralRe: SkypeBot Group Chat Pin
iPSiArt29-Feb-12 2:19
iPSiArt29-Feb-12 2:19 
GeneralRe: SkypeBot Group Chat Pin
bearbear1234529-Feb-12 19:59
bearbear1234529-Feb-12 19:59 
GeneralRe: SkypeBot Group Chat Pin
bearbear1234529-Feb-12 21:05
bearbear1234529-Feb-12 21:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.