首页 > 代码库 > (c#)SKYPE API项目总结(一)

(c#)SKYPE API项目总结(一)

原文: (c#)SKYPE API项目总结(一)

这个项目的需求:SKYPE软件文字聊天同步翻译,并将翻译后的内容会发送给对方,将对方发给自己的话翻译成自己语种。功能见图:

                                      

这里发的话,SKYPE软件也会同步:

这个SKYPE小插件主要用到微软在线翻译api和SKYPE API,微软在线翻译参考使用文档 ,  skype api下载地址,执行文件夹里的reg.bat对此dll进行注册后,方可使用。Skype4COM.dll的引用请参考于小章的博客,接下来我将总结下这个小插件用到的一些知识点。

1.软件界面:

软件界面使用的是常用的DotNetBar破解的.dll类库的下载地址。

2.翻译接口的使用:

利用翻译接口进行翻译的功能制作成了一个WebService,因为考虑到翻译接口API里共用一个APP_ID,首先在自己的WS中引用微软在线翻译的web_service.


代码如下:

 public class WS_Translate : System.Web.Services.WebService
    {
        string appId = System.Configuration.ConfigurationManager.AppSettings["Application_id"];
        ServiceRefer.LanguageServiceClient client = new ServiceRefer.LanguageServiceClient();//微软在线翻译的web service
       
        [WebMethod(Discription="获取翻译结果<BR>参数:string text(要翻译的文本), string from(该文本的语言代码), string to(要翻译成的目标语言代码)<BR>返回:string (翻译后的文本)")]
        public string GetTranslateResult(string text,string from,string to)
        {                                
            string resulte=client .Translate (appId ,text ,from,to,"text/html","general");          
            return resulte;
        }      

        [WebMethod(Discription="返回语言代码<BR>参数:无<BR>返回:string[]")]
        public string[] GetLanguagesCode() { string[] LanguagesCode = client.GetLanguagesForTranslate(appId); return LanguagesCode; }

        [WebMethod(Discription="根据提供的显示语言类型,返回对应该语言的语言名称列表。<BR>参数:string localeLanguage<BR>返回:string[]")]
        public string[] GetLanguage() { string[] LanguagesName = client.GetLanguageNames(appId,"zh-CHS", GetLanguagesCode());//"zh-CHS"简体中文 
        return LanguagesName; } 
    }

skype api工具类代码如下:

namespace YiNiu
{
    /// <summary>
    /// Skype工具
    /// </summary>
    public class SkypeTool
    {
        /// <summary>
        /// Skype对象
        /// </summary>
        public Skype Skype { get; set; }

        /// <summary>
        /// 当前用户
        /// </summary>
        public User CurrentUser
        {
            get
            {
                try
                {
                    return this.Skype.CurrentUser;
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 当前用户的资料
        /// </summary>
        public Profile CurrentUserProfile { get { return this.Skype.CurrentUserProfile; } }

        /// <summary>
        /// 我的语言种类
        /// </summary>
        public String MyCode { get { return this.CurrentUser .LanguageCode ; } }

        /// <summary>
        /// 在线好友
        /// </summary>
        public UserCollection  Friends { get { return this.Skype.Friends ; } }

        /// <summary>
        /// 连接状态
        /// </summary>
        public TOnlineStatus  Status { get { return this.Skype.CurrentUser.OnlineStatus; }}

        /// <summary>
        /// 是否Skype正在运行
        /// </summary>
        public bool IsSkypeRunning { get { return this.Skype.Client.IsRunning; } }

        /// <summary>
        /// 消息状态变化时
        /// </summary>
        public event _ISkypeEvents_MessageStatusEventHandler MessageStatus;

        /// <summary>
        /// 在线用户状态变化时
        /// </summary>
        public event _ISkypeEvents_OnlineStatusEventHandler OnlineStatus;

        /// <summary>
        /// 实例化WebService
        /// </summary>
        public  TranslateService translate = new TranslateService(ConstStrings.TRANSLATE_WEBSERVICE_URL);

        /// <summary>
        /// 构造函数
        /// </summary>
        public SkypeTool()
        {
            this.Skype = new Skype();//实例化Skype对象
            this.Skype.MessageStatus += new _ISkypeEvents_MessageStatusEventHandler(Skype_MessageStatus);//发送信息和接受信息时触发此事件
            this.Skype.OnlineStatus += new _ISkypeEvents_OnlineStatusEventHandler(Skype_OnlineStatus);//用户在线状态改变触发此事件
        }

        /// <summary>
        /// 返回状态列表
        /// </summary>     
        public DataTable GetStatus()
        {
            DataTable list = new DataTable();
            list.Columns.Add(new DataColumn("Display", typeof(string)));
            list.Columns.Add(new DataColumn("Value", typeof(string)));
            for (int i = 0; i < 5; i++)
            { list.Rows.Add(list.NewRow()); }
            //英文的对应是用SKYPE API读出的链接状态
            list.Rows[0][0] = "在线";
            list.Rows[1][0] = "离开";
            list.Rows[4][0] = "请勿打扰";
            list.Rows[2][0] = "隐身";
            list.Rows[3][0] = "离线";
            list.Rows[0][1] = "Online";
            list.Rows[1][1] = "Away";
            list.Rows[2][1] = "Invisible";
            list.Rows[3][1] = "Offline";
            list.Rows[4][1] = "DND";                                   
            return list;
        }

        /// <summary>
        /// 返回好友列表
        /// </summary>
        /// <returns></returns>
        public string[,] GetFriends()
        {    
            string[,] list = new string[Skype.Friends.Count, 3];
            for (int i = 0; i < Skype.Friends.Count; i++)
            {
                int index = 1;
                if (Skype.Friends[i + 1].FullName != "" || Skype.Friends[i + 1].DisplayName != "" || Skype.Friends[i + 1].Handle!="")
                {
                    //获得用户在线状态
                    string status=this.TextSataus ((this.Skype.get_User(Skype.Friends[i + 1].Handle.ToString()).OnlineStatus)).ToUpper();
                    if (status  == "ONLINE")
                    {
                       index = 0;
                    }
                    if (Skype.Friends[i + 1].DisplayName != "")
                    {
                        list[i, 0] = Skype.Friends[i + 1].DisplayName.ToString();//用户昵称
                    }
                    else
                    {
                        if (Skype.Friends[i + 1].FullName != "")
                        { list[i, 0] = Skype.Friends[i + 1].FullName.ToString(); }//用户昵称
                        else
                        { list[i, 0] = Skype.Friends[i + 1].Handle.ToString(); }
                    }
                    list[i, 1] = Skype.Friends[i + 1].Handle.ToString();//用户名
                    list[i, 2] = index.ToString();//用户在线状态
                }
            }
            return list;        
        }
   
        /// <summary>
        /// 返回语言列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetToLanguage(string code)
        {
            DataTable list = new DataTable();
            list.Columns.Add(new DataColumn("Display", typeof(string)));
            list.Columns.Add(new DataColumn("Value", typeof(string)));           
            List<string> LanguagesName = translate.GetLanguagesName(code);//调用webService
            List<string> LanguagesCode = translate.GetLanguagesCode();
            for (int i = 0; i < LanguagesName.Count; i++)
            {
                list.Rows.Add(list.NewRow());
                list.Rows[i][0] = LanguagesName[i].ToString();
                list.Rows[i][1] = LanguagesCode[i].ToString(); ;
            }
            return list;
        }
       
        /// <summary>
        /// 获得某一用户的语言类型
        /// </summary>
        /// <param name="fullname"></param>
        /// <returns></returns>
        public string  GetUserLanguageCode(string fullname)
        {
           return   SetMycode(Skype.get_User(fullname ).LanguageCode.ToString()); 
        }

        /// <summary>
        /// 初始化语言
        /// </summary>
        /// <returns></returns>
        public string SetMycode(string code)
        {
            //在Skype中和TranslateSope中汉语的英文code不对应需要转换           
            if (code == "zh-Hans" || code == "zh")//获取sender.languageCode,汉语是ch,不分简体和繁体
            {
                code = "zh-CHS";
            }
            if (code == "zh-Hant")
            {
                code = "zh-CHT";
            }
            return code;
        }

        /// <summary>
        /// 建立聊天
        /// </summary>
        /// <param name="friend">好友的用户名handle</param>      
        public IChat CreateIchat(string friend)
        {
            IChat ichat = Skype.CreateChatWith(friend);
            return ichat;
        }

        /// <summary>
        /// 状态变化
        /// </summary>
        /// <param name="pMessage">信息</param>
        /// <param name="Status">信息状态</param>
        private void Skype_MessageStatus(ChatMessage pMessage, TChatMessageStatus Status)
        {
            if (this.MessageStatus != null)
            {
                this.MessageStatus(pMessage, Status);
            }
        }

        /// <summary>
        /// 改变用户状态
        /// </summary>
        /// <param name="Status">用户在线状态</param>
        public  void ChangeStatus(string  Status)
        {            
            this.Skype.ChangeUserStatus(Skype.Convert.TextToUserStatus(Status)); 
        }

        /// <summary>
        /// 启动Skype
        /// </summary>
        public bool StartSkype()
        {
            try
            {
                if (!this.Skype.Client.IsRunning)
                {
                    this.Skype.Client.Start(false, true);                  
                }            
                //添加到类,用于控制
                this.Skype.Attach(5, false);              
                return true;
            }
            catch 
            {          
                return false;
            }            
        }

        /// <summary>
        /// 用户状态变化事件
        /// </summary>
        /// <param name="pUser">用户</param>
        /// <param name="Status">状态</param>
        public void Skype_OnlineStatus(User pUser, TOnlineStatus Status)
        {
            if (this.OnlineStatus  != null)
            {
                this.OnlineStatus(pUser, Status);
            }
        }

        /// <summary>
        /// 用户在线状态改变成"online"格式
        /// </summary>      
        public string TextSataus(TOnlineStatus Status)
        {
            return this.Skype.Convert.OnlineStatusToText(Status);
        }
    }  
}

这里软件整体的框架就出来了,后面章节再具体介绍细节的处理,软件的全部代码下载