Search code examples
c#consoleskypeskype4com

Skype C# API Select Chat


So I know this API is quite old and very undocumented, exactly the reason that I'm making a SO question, so I wanted to know how I can select a chat in Skype using the C# Skype Desktop API, I've done some looking around but most people seem to be using WinForms to make their app, mine's just a simple console application, code:

Skype Skype = new Skype();
Skype.Attach(5, true);

Skype.Chat.SendMessage("Hello ??");

Parser.Pause();

On runtime, I of course get an exception telling me I need to select a chat, but I'm not sure as to how I can do that, I've looked here but that didn't help me much.

Is there a way to reference a chat easily using a specific code? etc... Thanks!


Solution

  • I have constructed this snippet which should help you...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Runtime.Remoting.Channels;
    using System.Text;
    using System.Threading.Tasks;
    using SKYPE4COMLib;
    
    namespace skypeExperiment
    {
        class Program
        {   
            static void Main(string[] args)
            {
                Skype s = new Skype();
                s.Attach();
                if (!s.Client.IsRunning)
                {
                    // start minimized with no splash screen
                    s.Client.Start(true, true);
                }
    
                // wait for the client to be connected and ready
                //you have to click in skype on the "Allow application" button which has popped up there
                //to allow this application to communicate with skype
                s.Attach(6, true);
    
                //this will print out all the chat names to the console
                //it will enumerate all the chats you've been in
                foreach (Chat ch in s.Chats)
                {
                    Console.WriteLine(ch.Name);
                }
    
                //pick one chat name of the enumerated ones and get the chat object
                string chatName = "#someskypeuser/someskypeuser;9693a13447736b9";
                Chat chat = GetChatByName(s, chatName);
                //send a message to the selected chat
                if (chat != null)
                {
                    chat.SendMessage("test");
                }
                else
                {
                    Console.WriteLine("Chat with that name was not found.");
                }
    
                Console.WriteLine("Press any key to continue...");
                Console.ReadKey();
            }
    
            private static Chat GetChatByName(Skype client, string chatName)
            {
                foreach (Chat chat in client.Chats)
                {
                    if (chat.Name == chatName) return chat;
                }
                return null;
            }
    
    
        }
    }
    

    Instead of using an existing chat object, you can create new chat object with method

    Chat chat = s.CreateChatWith("name of the user to chat with");
    chat.SendMessage("test");
    

    You can create a group chat with:

    Group mygroup = s.CreateGroup("mygroup");
    mygroup.AddUser("user1");
    mygroup.AddUser("user2");
    Chat myGroupChat = s.CreateChatMultiple(mygroup.Users);
    myGroupChat.SendMessage("test");
    

    or create method to retrieve group by display name

    private static Group GetGroupByDisplayName(Skype client, string groupDisplayName)
    {
        foreach (Group g in client.Groups)
        {
            if (g.DisplayName == groupDisplayName)
            {
                return g;
            }
        }
        return null;
    }
    

    and use it then like:

    Group majesticSubwayGroup = GetGroupByDisplayName("majesticsubway"); 
    Chat majesticSubwayGroupChat = s.CreateChatMultiple(majesticSubwayGroup.Users);
    majesticSubwayGroupChat.SendMessage("test");