Skype C# API Select 聊天

Skype C# API Select Chat

所以我知道这个 API 很旧而且没有记录,这正是我提出 SO 问题的原因,所以我想知道如何 select 在 Skype 中聊天使用 C# Skype Desktop API,我做了一些环顾四周,但大多数人似乎都在使用 WinForms 来制作他们的应用程序,我的只是一个简单的控制台应用程序,代码:

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

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

Parser.Pause();

在运行时,我当然得到一个异常告诉我我需要 select 聊天,但我不确定我该怎么做,我看过 here但这对我帮助不大。

有没有办法使用特定代码轻松引用聊天?等等...谢谢!

我构建了这个应该可以帮助你的代码片段...

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;
        }


    }
}

您可以使用方法

创建新的聊天对象,而不是使用现有的聊天对象
Chat chat = s.CreateChatWith("name of the user to chat with");
chat.SendMessage("test");

您可以通过以下方式创建群聊:

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

或创建方法以按显示名称检索组

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

然后像这样使用它:

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