Lync 二次开发-对单个联络人和联系人组发送即时消息

对单个联系人发送消息此方法只是弹出联系人聊天对话框(对方在线时),并没有发送出消息      

[ScriptableMember]
        public void CallSingleMsg(string sipUri)
        {
            var lyncAutomation = LyncClient.GetAutomation();

            var inviteeList = new string[] { sipUri };
            var modalitySettings = new Dictionary();
            modalitySettings.Add(AutomationModalitySettings.SendFirstInstantMessageImmediately, false);
            if (string.IsNullOrEmpty(""))
            {
                modalitySettings.Add(AutomationModalitySettings.Subject, "");
            }

            lyncAutomation.BeginStartConversation(
                AutomationModalities.InstantMessage,
                inviteeList,
                modalitySettings,
                (ar) => { if (ar.IsCompleted) { ((Automation)ar.AsyncState).EndStartConversation(ar); } },
                lyncAutomation);

        }

 

对通讯组发送即时消息

[ScriptableMember]
        public void CallGroupMsg(string sipUri)
        {
            LyncClient client = LyncClient.GetClient();
            Automation lyncAutomation = LyncClient.GetAutomation();
            List inviteeList = new List();
            InstantMessageModality m = null;
            Conversation conversation = conversation = client.ConversationManager.AddConversation();

            //异步查询出联系人取出sipuri 添加到sip地址集合
            client.ContactManager.BeginLookup(sipUri, (ir) =>
            {
                try
                {
                    object foundItem = client.ContactManager.EndLookup(ir);
                    if (foundItem.GetType().Name == "DistributionGroup")
                    {
                        Group foundGroup = foundItem as Group;
                        foreach (var item in foundGroup)
                        {
                            inviteeList.Add(item.Uri);
                        }
                    }
                    else
                    {
                        Contact foundContact = foundItem as Contact;
                    }
                }
                catch (ItemNotFoundException)
                {
                    MessageBox.Show("Lookup returned no results");
                }


            }, null);

            var modalitySettings = new Dictionary();
            modalitySettings.Add(AutomationModalitySettings.SendFirstInstantMessageImmediately, false);
            if (string.IsNullOrEmpty(""))
            {
                modalitySettings.Add(AutomationModalitySettings.Subject, "");
            }

            //开始会话

            lyncAutomation.BeginStartConversation(
                AutomationModalities.InstantMessage,
                inviteeList,
                modalitySettings,
                (ar) => { if (ar.IsCompleted) { ((Automation)ar.AsyncState).EndStartConversation(ar); } },
                lyncAutomation);
        }


/// <summary>
        /// 发送即时消息
        /// </summary>
        /// <param name="lc"></param>
        /// <param name="uri"></param>
        private void AddConversion(LyncClient lc, string uri)
        {
            Contact contact = lc.ContactManager.GetContactByUri(uri);
            ContactAvailability availability = (ContactAvailability)contact.GetContactInformation(ContactInformationType.Availability);
            Conversation conversation = lc.ConversationManager.AddConversation();
            conversation.AddParticipant(contact);
            InstantMessageModality m = (InstantMessageModality)conversation.Modalities[ModalityTypes.Invalid];
            m.BeginConnect(null, null);
        }


        /// <summary>
        /// 弹出联系人聊天对话框(无论对方是否在线)
        /// </summary>
        /// <param name="sipUri"></param>
        public void CallSingleMsg(string sipUri)
        {
            var lyncAutomation = LyncClient.GetAutomation();
            var inviteeList = new string[] { sipUri };
            var modalitySettings = new Dictionary<AutomationModalitySettings,object>();
            modalitySettings.Add(AutomationModalitySettings.Subject, false);
            lyncAutomation.BeginStartConversation(
                AutomationModalities.InstantMessage,
                inviteeList,
                modalitySettings,
                (ar) => { if (ar.IsCompleted) { ((Automation)ar.AsyncState).EndStartConversation(ar); } },
                lyncAutomation);
        }

你可能感兴趣的:(Lync 二次开发-对单个联络人和联系人组发送即时消息)