Introduction
The purpose of this article is to show how to write simple chat bot using C#. Bot will simply answer on any received messages, like that:
User: Hi
Skype Bot: User said: Hi
After that you can improve logic of your bot is much as needed, for example, i wrote bot, which helps me and my friends to play D&D ;) Your bot can use web services, databases or his own logic to provide assistance to user. Skype bots have great applied area. They can be pretty useful.
Library and Documentation
If you want to build skype bot you need Skype4COM.dll. It can be downloaded from developer zone of skype. Documentation also will be helpful.
“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. Skype4Com.dll is automatically included together with Skype Extras Manager during Skype installation. If a user has unchecked Extras Manager during installation, Skype4Com library will be unavailable on that machine.” — from developer zone. Obviously, C# is well suited for working with COM libraries and GUI. Documentation has visual basic script examples, but no examples on C#.
Creating C# Project and Adding Reference
Let’s start VS2005. Create C# project as Windows Application. I called it Simple Skype Chat Bot.
Now, we need to include Skype4COM.dll to our project. Just right-click “References” in Solution Explorer and select option “Add Reference” in popup menu. You will see four tabs in the “Add Reference” dialog box, choose COM tab. In the listbox choose Skype4COM 1.0 Type Library. You should have it, if you have Skype Extras Manager. If you don’t have it, then download it and register using regsvr32 command.
Press Ok. Now Skype4COM.dll is included to our project. VS2005 generate Runtime Callable Wrapper using TlbImp.exe. It will imports types definitions found in COM library into equivalent .NET types. If you want know more about .NET and COM interaction read “C#.NET and COM Interop” article by Ravi Sankar. You can view generated types in object explorer, select SKYPE4COMLib.
Attaching to the Skype
SkypeClass is the main class for working with skype, we need to create it and attach to Skype. First, create a variable of this class in you from class. I prefer to make initialization of SkypeClass object atFrom_Load event, you can choose any other suitable for you.
As you can see, variable skype of the class SkypeClass was declared. At the SkypeChatBotForm_Load event handler we check is skype running. If not we start the skype application. After that we subscibe on two events: AttachmentStatus and MessageStatus. Change of the user status fires AttachmentStatus event handler, there we attach to skype. Run the project. You will see Skype dialog. It will ask you about our application accessing skype. Say yes.
Answering to the received messages
Our next goal is to handle receive messages in skype_MessageStatus event handler and answer to them. There are a lot of types of messages, but we have interest only in received messages. Enum TChatMessageStatus contains all possible values, so TChatMessageStatus.cmsReceived is the proper value for received message.
pMessage is instance of ChatMessage class. Through it we can get access to our chat and answer using method SendMessage. pMessage.Body contains text of the received message and pMessage.FromDisplayName contains name of the person who sent message. That’s all. You can run your bot and chat with somebody. He will be surprised of your typing speed ;-)
As you can see, Aleksey said “Hi” to me and bot answered from my account.
Deployment
If you will decide to publish your bot, you should keep in mind, that not all users have Skype4COM.dll. Maybe you need to include it in your deployment package and register in user’s systems.
Conclusion
As you noticed, bot answered to received message by my name, not something like “Answering Bot”. It is because your application can attach only to working copy of skype. One of the most pity things: there is no way to run more than one copy of skype in a single PC. That mean if you want to create useful bot, which will work always and has his own name — you need to create skype account and run this account with this bot on separate PC or virtual machine. In any way it is very resource consuming. I am dreaming about COM component which will allows to use skype network without attaching to already running GUI. Something like Skype engine library. But it seems impossible due to commercial reasons, because then anybody will have possibility to write his own Skype client.
Thanks for assistance
- Aleksey Shmatov
- Tanya Koltunova
Download Source Code