.NET How to create a Skype Auto Talk Bot [C#]

Discussion in 'Source Code & Tutorial Database' started by KrzyEdit, Dec 1, 2015 with 0 replies and 7,714 views.

Tags:
  1. KrzyEdit

    KrzyEdit Newbie

    Messages:
    26
    Ratings:
    10
    DISCLAIMER: I DON'T CONDONE CREATING MALICIOUS/SPAM-RELATED APPLICATIONS, THIS TUTORIAL SHOULD BE USED TO LEGITIMATE REASONS ONLY!

    What you will learn by the end of this tutorial:
    • The basics of the Skype .NET library
    • How to connect to Skype using C#
    • How to send messages to Skype contacts
    • How to create a Talk-Bot

    In this tutorial I will be teaching you the basics of creating a C# Skype Tool, so lets go to the requirements

    The Tutorial
    Alright, let's start creating a Windows Form Project:
    [​IMG]
    Now our project needs to communicate with the Skype4COM.dll, so let's add a reference to that .dll. Right Click your project's name > Add Reference
    [​IMG]
    Then go to the Browse Tab, click the Browse... Button and locate your Skype4COM.dll, after that, click OK.
    [​IMG]

    Now lets code, this is your Form1.cs code:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace TutorialOne
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
        }
    }
    Now let's add the line "using SKYPE4COMLib;"
    Let's declare a Skype Constructor doing this:
    Code:
    private Skype MySkype = new Skype();
    Now that we have a constructor we need to attach to our current Skype instance, you can create a button for this or simply attach it when the form loads, I will do a button.

    Code:
    private void ConnectButton_Click(object sender, EventArgs e)
            {
                MySkype.Attach(5, false);
            }
    A good thing you can do with this is create a program where it checks wheter you are allowed to use a application or not, for example. But how can we get the Skype's name of the current attached Skype Client? Using the variable CurrentUserHandle.

    Code:
    private void ConnectButton_Click(object sender, EventArgs e)
            {
                MySkype.Attach(5, false);
                MessageBox.Show("Hello " + MySkype.CurrentUserHandle);
            }
    In the example above it Attach's to Skype and shows a message box with "Hello [YourUserID]"
    I will go through 2 more things there: Sending messages to a contact and create a reply bot.

    To send a message to a friend, you need to use the function SendMessage() that take the contact's user/handle and the message text. For Example:

    Code:
     private void SendMessageButton_Click(object sender, EventArgs e)
            {
                MySkype.SendMessage("vitor.marques45", "Hello World!");
                // You could also use a TextBox to get both variables,
                // just make sure to check if them are not empty/null.
            }

    Now lets go to the coolest thing you will learn in this tutorial: A Skype Talk-Bot!
    To do this we will need events, but what are they?
    Events are methods that are called when some action happens, basically.
    So let's start creating a Message Recieved event on our program, what I'll do is I'll add the following line after my Attach() line on my button_click method:
    Code:
    MySkype.MessageStatus += MySkype_MessageStatus;
    and add another method:

    Code:
    void MySkype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
            {
        
            }
    So basically, when you recieved/send a message, this method is called.
    To read the message's text you can do this:

    Code:
    void MySkype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
            {
                string MessageText = pMessage.Body;
            }
    Now we'll do some a little bit more complex stuff, so I'll explain everything later:

    Code:
    void MySkype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
            {
                if (pMessage.Body[0].ToString() == "@") // Check if it is a command, @ can be anything that you want.
                {
                    string Command = pMessage.Body.Substring(1, pMessage.Body.Length - 1); // Basicaly remove @ from it.
                    if (pMessage.Sender.Handle != MySkype.CurrentUserHandle) // Check if the sender is not yourself.
                        MySkype.SendMessage(pMessage.Sender.Handle, ProcessCommand(Command)); // Send the message back.
                    else
                        MessageBox.Show("Message not sent.");
                }
            }
    
            private string ProcessCommand(string cmd)
            {
                switch (cmd)
                {
                    case "help":
                        return "The commands are: blablabla...";
                    case "LOL":
                        return "(chuckle)";
                    default:
                        return "This is not a command.\nSend '@help' for help.";
                }
            }
    Let me explain the code: when skype recieves a message (pMessage.Body) it will check if it starts with "@" (I used this to indicate the command, it could be anything you want), if it starts with "@" it will get the command, basicaly remove the @ from the message (You could use the Replace() method, however this could give you bad results).
    Then it will check if the sender (pMessage.Sender.Handle) is not you, when it checks it, the code will process the command, pretty basic coding there.

    And that's it! If you read the whole thing: Thank you.

    Full Code: here.

    Feel free to comment any questions here,

    - Naccib/Guilherme Almeida
     
    • Like Like x 2
    Last edited: Dec 2, 2015

Share This Page