Skype - 如何开始?

发布于 2024-09-10 07:55:30 字数 296 浏览 3 评论 0原文

我花了很多时间。整个 Skype 论坛似乎已经崩溃或死亡,或者他们没有技术人员来处理这些部门。

入门代码示例无法正常工作或不可用(完全异常)。例如: http://forum.skype.com/index.php?showtopic=3557< /a>

因此,我请求有人向我展示一个简单的 C# 工作代码示例,说明如何逐步开始。

提前致谢。

I spend lot of time. And the whole Skype forum seems broken or dead or they don't have technical guys to handle those sectors.

Getting started code examples are not working or not available (completely abnormal). ex: http://forum.skype.com/index.php?showtopic=3557

Therefore, i request can someone kindly please show me a simple C# working code example how to just get started step by step.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

白日梦 2024-09-17 07:55:30
  1. 添加对 Skype COM 库的引用
  2. 添加以下类并开始探索 Skype。

代码(包括 using SKYPE4COMLib;,它搞砸了 stackoverflow 语法突出显示)

namespace Example
{
    class SkypeExample
    {
        private SkypeClass _skype;

        public SkypeExample()
        {
            _skype = new SkypeClass();
            _skype.MessageStatus += OnMessage;
            _skype._ISkypeEvents_Event_AttachmentStatus += OnAttach;
            _skype.Attach(7, false);
        }

        private void OnAttach(TAttachmentStatus status)
        {
            // this app was successfully attached to skype
        }

        private void OnMessage(ChatMessage pmessage, TChatMessageStatus status)
        {
            // dont do anything if the message is not received (i.e. we are sending a emssage)
            if (status != TChatMessageStatus.cmsReceived)
                return;

            // simple echo service.
            _skype.get_Chat(pmessage.ChatName).SendMessage(pmessage.Body);
        }

        public bool MakeFriend(string handle)
        {
            for (int i = 1; i <= _skype.Friends.Count; i++)
            {
                if (_skype.Friends[i].Handle == handle)
                    return true;
            }

            UserCollection collection = _skype.SearchForUsers(handle);
            if (collection.Count >= 1)
                collection[1].BuddyStatus = TBuddyStatus.budPendingAuthorization;

            return false;
        }
    }
}
  1. Add a reference to the skype COM library
  2. Add the following class and start exploring skype.

code (include using SKYPE4COMLib;, it screws up stackoverflow syntax highlighting)

namespace Example
{
    class SkypeExample
    {
        private SkypeClass _skype;

        public SkypeExample()
        {
            _skype = new SkypeClass();
            _skype.MessageStatus += OnMessage;
            _skype._ISkypeEvents_Event_AttachmentStatus += OnAttach;
            _skype.Attach(7, false);
        }

        private void OnAttach(TAttachmentStatus status)
        {
            // this app was successfully attached to skype
        }

        private void OnMessage(ChatMessage pmessage, TChatMessageStatus status)
        {
            // dont do anything if the message is not received (i.e. we are sending a emssage)
            if (status != TChatMessageStatus.cmsReceived)
                return;

            // simple echo service.
            _skype.get_Chat(pmessage.ChatName).SendMessage(pmessage.Body);
        }

        public bool MakeFriend(string handle)
        {
            for (int i = 1; i <= _skype.Friends.Count; i++)
            {
                if (_skype.Friends[i].Handle == handle)
                    return true;
            }

            UserCollection collection = _skype.SearchForUsers(handle);
            if (collection.Count >= 1)
                collection[1].BuddyStatus = TBuddyStatus.budPendingAuthorization;

            return false;
        }
    }
}
冰雪梦之恋 2024-09-17 07:55:30

除了 jgauffin 所说的之外,这是我曾经使用的(单例)包装类(C# 3):

using System;
using System.Collections.Generic;
using System.Linq;
using SKYPE4COMLib;

namespace SkypeR
{
    public class SkypeApplication
    {
        private static readonly SkypeApplication TheInstance = new SkypeApplication();
        private readonly Skype _skype = new Skype();

        private SkypeApplication()
        {
            _skype.OnlineStatus += SkypeOnlineStatus;
            _skype.UserStatus += SkypeUserStatus;
            _skype.UserMood += SkypeUserMood;
        }

        private void SkypeUserMood(User puser, string moodtext)
        {
            FireUserMood(puser.GetUserInfo(), puser.OnlineStatus.GetStatusValue(), moodtext);
        }

        public string MyMood
        {
            get { return _skype.CurrentUserProfile.MoodText; }
            set { _skype.CurrentUserProfile.MoodText = value; }
        }

        public static SkypeApplication Instance
        {
            get { return TheInstance; }
        }

        public bool Running
        {
            get { return _skype.Client.IsRunning; }
        }

        public string Information
        {
            get { return String.Format("v{0}", _skype.Version); }
        }

        public Status MyStatus
        {
            get { return _skype.CurrentUserStatus.GetStatusValue(); }
            set { _skype.CurrentUserStatus = value.GetTUserStatusValue(); }
        }

        public IEnumerable<Contact> ContactList
        {
            get
            {
                UserCollection friends = _skype.Friends;
                return from User user in friends select new Contact
                                                            {
                                                                UserInfo = user.GetUserInfo(),
                                                                Status = user.OnlineStatus.GetStatusValue()
                                                            };
            }
        }

        private void SkypeUserStatus(TUserStatus status)
        {
            FireUserStatus(status.GetStatusValue());
        }

        private void FireUserStatus(Status status)
        {
            if (OnStatusChange != null)
            {
                OnStatusChange(this, new StatusChangedArgs {Status = status, Time = DateTime.Now});
            }
        }

        private void SkypeOnlineStatus(User puser, TOnlineStatus status)
        {
            FireUserStatus(puser.GetUserInfo(), status.GetStatusValue(), puser.MoodText);
        }

        public void Start()
        {
            if (!_skype.Client.IsRunning)
            {
                _skype.Client.Start(true, true);
            }
        }

        public void Shutdown()
        {
            if (_skype.Client.IsRunning)
            {
                _skype.Client.Shutdown();
            }
        }

        public event EventHandler<UserSomethingChangedArgs> OnUserStatusChange;
        public event EventHandler<StatusChangedArgs> OnStatusChange;
        public event EventHandler<UserSomethingChangedArgs> OnUserMoodChange;

        private void FireUserStatus(UserInfo user, Status newStatus, string userMood)
        {
            if (OnUserStatusChange != null)
            {
                OnUserStatusChange(this,
                                   new UserSomethingChangedArgs
                                       {
                                           Contact = new Contact {UserInfo = user, Status = newStatus, Mood = userMood},
                                           Time = DateTime.Now
                                       });
            }
        }

        public void FireUserMood(UserInfo user, Status userStatus, string newMood)
        {
            if (OnUserMoodChange != null)
            {
                OnUserMoodChange(this,
                    new UserSomethingChangedArgs
                        {
                            Contact = new Contact { UserInfo = user, Status= userStatus, Mood = newMood },
                            Time = DateTime.Now
                        });
            }
        }
    }

    public class StatusChangedArgs : EventArgs
    {
        public Status Status { get; set; }
        public DateTime Time { get; set; }
    }

    public class UserSomethingChangedArgs : EventArgs
    {
        public Contact Contact { get; set; }
        public DateTime Time { get; set; }
    }

    public enum Sex
    {
        Female,
        Male
    }

    public enum Status
    {
        Online,
        Away,
        Offline,
        Unknown,
        DoNotDisturb,
        NotAvailable,
        SkypeMe,
        SkypeOut
    }

    public struct UserInfo
    {
        public string Name { get; set; }
        public Sex Sex { get; set; }

        public static bool operator ==(UserInfo i1, UserInfo i2)
        {
            return (i1.Name.Equals(i2.Name) && i1.Sex.Equals(i2.Sex));
        }

        public static bool operator !=(UserInfo i1, UserInfo i2)
        {
            return (!i1.Sex.Equals(i2.Sex) || !i1.Name.Equals(i2.Name));
        }

        public bool Equals(UserInfo other)
        {
            return Equals(other.Name, Name) && Equals(other.Sex, Sex);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            return obj.GetType() == typeof (UserInfo) && Equals((UserInfo) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Name != null ? Name.GetHashCode() : 0)*397) ^ Sex.GetHashCode();
            }
        }
    }

    public class Contact
    {
        public UserInfo UserInfo { get; set; }
        public Status Status { get; set; }
        public string Mood { get; set; }
    }

    public static class Extensions
    {
        public static string GetSomeName(this User user)
        {
            if (!string.IsNullOrEmpty(user.FullName))
                return user.FullName;
            if (!string.IsNullOrEmpty(user.DisplayName))
                return user.DisplayName;
            if (!string.IsNullOrEmpty(user.Handle))
                return user.Handle;
            if (!string.IsNullOrEmpty(user.Aliases))
                return user.Aliases;
            return "unbekannt";
        }


        public static Sex GetUserInfoSexValue(this TUserSex sex)
        {
            return sex == TUserSex.usexMale ? Sex.Male : Sex.Female;
        }

        public static string ToLocalString(this Status status)
        {
            switch (status)
            {
                case Status.Online:
                    return "online";
                case Status.Away:
                    return "abwesend";
                case Status.Offline:
                    return "offline";
                case Status.Unknown:
                    return "unbekannt";
                case Status.DoNotDisturb:
                    return "nicht stören";
                case Status.NotAvailable:
                    return "nicht verfügbar";
                case Status.SkypeMe:
                    return "Skype Me!";
                case Status.SkypeOut:
                    return "Skype out";
                default:
                    return String.Empty;
            }
        }

        public static Status GetStatusValue(this TOnlineStatus status)
        {
            switch (status)
            {
                case TOnlineStatus.olsOnline:
                    return Status.Online;
                case TOnlineStatus.olsAway:
                    return Status.Away;
                case TOnlineStatus.olsOffline:
                    return Status.Offline;
                case TOnlineStatus.olsDoNotDisturb:
                    return Status.DoNotDisturb;
                case TOnlineStatus.olsNotAvailable:
                    return Status.NotAvailable;
                case TOnlineStatus.olsSkypeMe:
                    return Status.SkypeMe;
                case TOnlineStatus.olsSkypeOut:
                    return Status.SkypeOut;
            }
            return Status.Unknown;
        }

        public static TUserStatus GetTUserStatusValue(this Status status)
        {
            switch (status)
            {
                case Status.Online:
                    return TUserStatus.cusOnline;
                case Status.Away:
                    return TUserStatus.cusAway;
                case Status.Offline:
                    return TUserStatus.cusOffline;
                case Status.Unknown:
                    return TUserStatus.cusUnknown;
                case Status.DoNotDisturb:
                    return TUserStatus.cusDoNotDisturb;
                case Status.NotAvailable:
                    return TUserStatus.cusNotAvailable;
                case Status.SkypeMe:
                    return TUserStatus.cusSkypeMe;
            }
            return TUserStatus.cusUnknown;
        }

        public static Status GetStatusValue(this TUserStatus status)
        {
            switch (status)
            {
                case TUserStatus.cusOnline:
                    return Status.Online;
                case TUserStatus.cusAway:
                    return Status.Away;
                case TUserStatus.cusOffline:
                    return Status.Offline;
                case TUserStatus.cusDoNotDisturb:
                    return Status.DoNotDisturb;
                case TUserStatus.cusNotAvailable:
                    return Status.NotAvailable;
                case TUserStatus.cusSkypeMe:
                    return Status.SkypeMe;
            }
            return Status.Unknown;
        }

        public static int GetImageIndex(this Status status)
        {
            switch (status)
            {
                case Status.Online:
                    return 0;
                case Status.Away:
                    return 1;
                case Status.Offline:
                    return 4;
                case Status.Unknown:
                    return 4;
                case Status.DoNotDisturb:
                    return 2;
                case Status.NotAvailable:
                    return 1;
                case Status.SkypeMe:
                    return 0;
                case Status.SkypeOut:
                    return 3;
                default:
                    return 4;
            }
        }

        public static UserInfo GetUserInfo(this User user)
        {
            // TODO: Maybe take from some database!
            return new UserInfo {Name = user.GetSomeName(), Sex = user.Sex.GetUserInfoSexValue()};
        }
    }
}

如果您愿意,可以扩展它,甚至可以将其发布回此处(如果您愿意)。

一般用法:

启动 Skype

SkypeApplication.Instance.Start();

注册一些事件

SkypeApplication.Instance.OnUserStatusChange += UserStatusChange;
SkypeApplication.Instance.OnUserMoodChange += UserMoodChange;
SkypeApplication.Instance.OnStatusChange += StatusChange;

,例如:

private void UserMoodChange(object sender, UserSomethingChangedArgs args)
{
  var listViewItem = new ListViewItem(new[]
    {
      args.Time.ToString(),
      args.Contact.UserInfo.Name,
      args.Contact.Status.ToLocalString(),
      args.Contact.Mood
    }, args.Contact.Status.GetImageIndex());

  listView1.Items.Add(listViewItem);
  listViewItem.EnsureVisible();

  NotifyStatus(args);
  RefreshContactList();

  LogIt(args);
}

如您所见,有一些辅助扩展方法来获取图像索引、状态消息文本(需要本地化)等。

设置我的状态

SkypeApplication.Instance.MyStatus = Status.Away;

与 jgauffin 相比,我编写的包装器不会修改任何内容,除了状态之外只是只读的。

最后,还有一个部署小问题:您必须将可执行文件编译为 x86,SKYPE4COMLib 才能正常工作。 IIRC 这无论如何都适用于所有 COM 互操作程序集。

如果您有疑问,请随时询问。

In addition to what jgauffin said, here's the (singleton) wrapper class that I used to use (C# 3):

using System;
using System.Collections.Generic;
using System.Linq;
using SKYPE4COMLib;

namespace SkypeR
{
    public class SkypeApplication
    {
        private static readonly SkypeApplication TheInstance = new SkypeApplication();
        private readonly Skype _skype = new Skype();

        private SkypeApplication()
        {
            _skype.OnlineStatus += SkypeOnlineStatus;
            _skype.UserStatus += SkypeUserStatus;
            _skype.UserMood += SkypeUserMood;
        }

        private void SkypeUserMood(User puser, string moodtext)
        {
            FireUserMood(puser.GetUserInfo(), puser.OnlineStatus.GetStatusValue(), moodtext);
        }

        public string MyMood
        {
            get { return _skype.CurrentUserProfile.MoodText; }
            set { _skype.CurrentUserProfile.MoodText = value; }
        }

        public static SkypeApplication Instance
        {
            get { return TheInstance; }
        }

        public bool Running
        {
            get { return _skype.Client.IsRunning; }
        }

        public string Information
        {
            get { return String.Format("v{0}", _skype.Version); }
        }

        public Status MyStatus
        {
            get { return _skype.CurrentUserStatus.GetStatusValue(); }
            set { _skype.CurrentUserStatus = value.GetTUserStatusValue(); }
        }

        public IEnumerable<Contact> ContactList
        {
            get
            {
                UserCollection friends = _skype.Friends;
                return from User user in friends select new Contact
                                                            {
                                                                UserInfo = user.GetUserInfo(),
                                                                Status = user.OnlineStatus.GetStatusValue()
                                                            };
            }
        }

        private void SkypeUserStatus(TUserStatus status)
        {
            FireUserStatus(status.GetStatusValue());
        }

        private void FireUserStatus(Status status)
        {
            if (OnStatusChange != null)
            {
                OnStatusChange(this, new StatusChangedArgs {Status = status, Time = DateTime.Now});
            }
        }

        private void SkypeOnlineStatus(User puser, TOnlineStatus status)
        {
            FireUserStatus(puser.GetUserInfo(), status.GetStatusValue(), puser.MoodText);
        }

        public void Start()
        {
            if (!_skype.Client.IsRunning)
            {
                _skype.Client.Start(true, true);
            }
        }

        public void Shutdown()
        {
            if (_skype.Client.IsRunning)
            {
                _skype.Client.Shutdown();
            }
        }

        public event EventHandler<UserSomethingChangedArgs> OnUserStatusChange;
        public event EventHandler<StatusChangedArgs> OnStatusChange;
        public event EventHandler<UserSomethingChangedArgs> OnUserMoodChange;

        private void FireUserStatus(UserInfo user, Status newStatus, string userMood)
        {
            if (OnUserStatusChange != null)
            {
                OnUserStatusChange(this,
                                   new UserSomethingChangedArgs
                                       {
                                           Contact = new Contact {UserInfo = user, Status = newStatus, Mood = userMood},
                                           Time = DateTime.Now
                                       });
            }
        }

        public void FireUserMood(UserInfo user, Status userStatus, string newMood)
        {
            if (OnUserMoodChange != null)
            {
                OnUserMoodChange(this,
                    new UserSomethingChangedArgs
                        {
                            Contact = new Contact { UserInfo = user, Status= userStatus, Mood = newMood },
                            Time = DateTime.Now
                        });
            }
        }
    }

    public class StatusChangedArgs : EventArgs
    {
        public Status Status { get; set; }
        public DateTime Time { get; set; }
    }

    public class UserSomethingChangedArgs : EventArgs
    {
        public Contact Contact { get; set; }
        public DateTime Time { get; set; }
    }

    public enum Sex
    {
        Female,
        Male
    }

    public enum Status
    {
        Online,
        Away,
        Offline,
        Unknown,
        DoNotDisturb,
        NotAvailable,
        SkypeMe,
        SkypeOut
    }

    public struct UserInfo
    {
        public string Name { get; set; }
        public Sex Sex { get; set; }

        public static bool operator ==(UserInfo i1, UserInfo i2)
        {
            return (i1.Name.Equals(i2.Name) && i1.Sex.Equals(i2.Sex));
        }

        public static bool operator !=(UserInfo i1, UserInfo i2)
        {
            return (!i1.Sex.Equals(i2.Sex) || !i1.Name.Equals(i2.Name));
        }

        public bool Equals(UserInfo other)
        {
            return Equals(other.Name, Name) && Equals(other.Sex, Sex);
        }

        public override bool Equals(object obj)
        {
            if (ReferenceEquals(null, obj)) return false;
            return obj.GetType() == typeof (UserInfo) && Equals((UserInfo) obj);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return ((Name != null ? Name.GetHashCode() : 0)*397) ^ Sex.GetHashCode();
            }
        }
    }

    public class Contact
    {
        public UserInfo UserInfo { get; set; }
        public Status Status { get; set; }
        public string Mood { get; set; }
    }

    public static class Extensions
    {
        public static string GetSomeName(this User user)
        {
            if (!string.IsNullOrEmpty(user.FullName))
                return user.FullName;
            if (!string.IsNullOrEmpty(user.DisplayName))
                return user.DisplayName;
            if (!string.IsNullOrEmpty(user.Handle))
                return user.Handle;
            if (!string.IsNullOrEmpty(user.Aliases))
                return user.Aliases;
            return "unbekannt";
        }


        public static Sex GetUserInfoSexValue(this TUserSex sex)
        {
            return sex == TUserSex.usexMale ? Sex.Male : Sex.Female;
        }

        public static string ToLocalString(this Status status)
        {
            switch (status)
            {
                case Status.Online:
                    return "online";
                case Status.Away:
                    return "abwesend";
                case Status.Offline:
                    return "offline";
                case Status.Unknown:
                    return "unbekannt";
                case Status.DoNotDisturb:
                    return "nicht stören";
                case Status.NotAvailable:
                    return "nicht verfügbar";
                case Status.SkypeMe:
                    return "Skype Me!";
                case Status.SkypeOut:
                    return "Skype out";
                default:
                    return String.Empty;
            }
        }

        public static Status GetStatusValue(this TOnlineStatus status)
        {
            switch (status)
            {
                case TOnlineStatus.olsOnline:
                    return Status.Online;
                case TOnlineStatus.olsAway:
                    return Status.Away;
                case TOnlineStatus.olsOffline:
                    return Status.Offline;
                case TOnlineStatus.olsDoNotDisturb:
                    return Status.DoNotDisturb;
                case TOnlineStatus.olsNotAvailable:
                    return Status.NotAvailable;
                case TOnlineStatus.olsSkypeMe:
                    return Status.SkypeMe;
                case TOnlineStatus.olsSkypeOut:
                    return Status.SkypeOut;
            }
            return Status.Unknown;
        }

        public static TUserStatus GetTUserStatusValue(this Status status)
        {
            switch (status)
            {
                case Status.Online:
                    return TUserStatus.cusOnline;
                case Status.Away:
                    return TUserStatus.cusAway;
                case Status.Offline:
                    return TUserStatus.cusOffline;
                case Status.Unknown:
                    return TUserStatus.cusUnknown;
                case Status.DoNotDisturb:
                    return TUserStatus.cusDoNotDisturb;
                case Status.NotAvailable:
                    return TUserStatus.cusNotAvailable;
                case Status.SkypeMe:
                    return TUserStatus.cusSkypeMe;
            }
            return TUserStatus.cusUnknown;
        }

        public static Status GetStatusValue(this TUserStatus status)
        {
            switch (status)
            {
                case TUserStatus.cusOnline:
                    return Status.Online;
                case TUserStatus.cusAway:
                    return Status.Away;
                case TUserStatus.cusOffline:
                    return Status.Offline;
                case TUserStatus.cusDoNotDisturb:
                    return Status.DoNotDisturb;
                case TUserStatus.cusNotAvailable:
                    return Status.NotAvailable;
                case TUserStatus.cusSkypeMe:
                    return Status.SkypeMe;
            }
            return Status.Unknown;
        }

        public static int GetImageIndex(this Status status)
        {
            switch (status)
            {
                case Status.Online:
                    return 0;
                case Status.Away:
                    return 1;
                case Status.Offline:
                    return 4;
                case Status.Unknown:
                    return 4;
                case Status.DoNotDisturb:
                    return 2;
                case Status.NotAvailable:
                    return 1;
                case Status.SkypeMe:
                    return 0;
                case Status.SkypeOut:
                    return 3;
                default:
                    return 4;
            }
        }

        public static UserInfo GetUserInfo(this User user)
        {
            // TODO: Maybe take from some database!
            return new UserInfo {Name = user.GetSomeName(), Sex = user.Sex.GetUserInfoSexValue()};
        }
    }
}

If you wish, extend it and maybe even post it back here if you want to.

Genaral usage:

Starting Skype

SkypeApplication.Instance.Start();

Registering for some events

SkypeApplication.Instance.OnUserStatusChange += UserStatusChange;
SkypeApplication.Instance.OnUserMoodChange += UserMoodChange;
SkypeApplication.Instance.OnStatusChange += StatusChange;

where, for example:

private void UserMoodChange(object sender, UserSomethingChangedArgs args)
{
  var listViewItem = new ListViewItem(new[]
    {
      args.Time.ToString(),
      args.Contact.UserInfo.Name,
      args.Contact.Status.ToLocalString(),
      args.Contact.Mood
    }, args.Contact.Status.GetImageIndex());

  listView1.Items.Add(listViewItem);
  listViewItem.EnsureVisible();

  NotifyStatus(args);
  RefreshContactList();

  LogIt(args);
}

As you can see there are some auxiliary extension methods to get image indices, status message texts (that need to be localized) and so on.

Setting my status

SkypeApplication.Instance.MyStatus = Status.Away;

In contrast to jgauffin, the wrapper I wrote does not modify anything, just read-only except for the status.

Finally, one small deployment issue: You must compile the executable to x86 for the SKYPE4COMLib to work correctly. IIRC this applies to all COM interop assemblies anyway.

Feel free to ask, if you have questions.

揽月 2024-09-17 07:55:30

自从 Microsoft 收购 Skype 以来,Skype API 已被弃用。您无法在新的群聊中使用它。

Since Microsoft bought Skype the Skype API is deprecated. You can't use it on new group chats.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文