【问题标题】:Skype C# API Select ChatSkype C# API 选择聊天
【发布时间】:2017-02-23 11:56:16
【问题描述】:

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

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

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

Parser.Pause();

在运行时,我当然会收到一个异常,告诉我我需要选择一个聊天,但我不确定我该怎么做,我看过 here 但这对我没有多大帮助.

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

【问题讨论】:

    标签: c# console skype skype4com


    【解决方案1】:

    我已经构建了这个sn-p,它应该可以帮助你......

    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");
    

    【讨论】:

    • 那么群聊呢?这是一个群组的示例截图:prntscr.com/ctnr41 我在想它可以通过邀请 ID 或群组名称选择群组,例如majesticsubway? :-)
    • 谢谢,我试试看!
    猜你喜欢
    • 2018-05-02
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多