Lync消息机器人开发(ucma)

        private UserEndpointSettings CreateUserSetting()
        {
            UserEndpointSettings userSetting = null;
            try
            {
                userSetting = new UserEndpointSettings(this._strUserURI, this._strServerFQDN);
                userSetting.Credential = new NetworkCredential(this._strUserName, this._strUserPassword, this._strUserDomain);
            }
            catch (InvalidOperationException ex)
            {
                userSetting = null;
                LogUtility.GetInstance().Error(MethodBase.GetCurrentMethod(), "UserEndpointSettings初始化错误。");
                LogUtility.GetInstance().Debug(MethodBase.GetCurrentMethod(), "UserEndpointSettings初始化错误:\r\n" + ex.ToString());
            }
            return userSetting;
        }

  创建UserEndpoint

        private UserEndpoint CreateUserEndpoint(UserEndpointSettings userEndpointSettings)
        {
            try
            {
                ClientPlatformSettings clientPlatformSettings = new ClientPlatformSettings(ConstInfo.SERVICE_NAME, _transportType);
                this._collabPlatform = new CollaborationPlatform(clientPlatformSettings);
                this._collabPlatform.InstantMessagingSettings.SupportedFormats = InstantMessagingFormat.HtmlText;
                this._userEndpoint = new UserEndpoint(_collabPlatform, userEndpointSettings);
            }
            catch (Exception ex)
            {
                _userEndpoint = null;
                LogUtility.GetInstance().Error(MethodBase.GetCurrentMethod(), "创建UserEndpoint错误。");
                LogUtility.GetInstance().Debug(MethodBase.GetCurrentMethod(), "创建UserEndpoint错误:\r\n" + ex.ToString());
            }
            return _userEndpoint;
        }

  

        private void EstablishUserEndpoint(UserEndpoint userEndpoint)
        {
            try
            {
                // Startup the platform
                userEndpoint.Platform.BeginStartup(this.EndPlatformStartup, userEndpoint.Platform);
                // Again, just for sync. reasons.
                _platformStartupCompleted.WaitOne();
                // Establish the user endpoint
                userEndpoint.BeginEstablish(EndEndpointEstablish, userEndpoint);
                // Sync; wait for the registration to complete.
                _endpointInitCompletedEvent.WaitOne();
            }
            catch (Exception ex)
            {
                LogUtility.GetInstance().Error(MethodBase.GetCurrentMethod(), "EstablishUserEndpoint错误。");
                LogUtility.GetInstance().Debug(MethodBase.GetCurrentMethod(), "EstablishUserEndpoint错误:\r\n" + ex.ToString());
            }
        }

  

        private void EndPlatformStartup(IAsyncResult ar)
        {
            CollaborationPlatform collabPlatform = ar.AsyncState as CollaborationPlatform;
            try
            {
                // The platform should now be started.
                collabPlatform.EndStartup(ar);
            }
            catch (Exception ex)
            {
                LogUtility.GetInstance().Error(MethodBase.GetCurrentMethod(), "EndStartup调用异常。");
                LogUtility.GetInstance().Debug(MethodBase.GetCurrentMethod(), "EndStartup调用异常:\r\n" + ex.ToString());
            }
            finally
            {
                // Again, just for sync. reasons.
                _platformStartupCompleted.Set();
            }
        }

  

        private void EndEndpointEstablish(IAsyncResult ar)
        {
            LocalEndpoint currentEndpoint = ar.AsyncState as LocalEndpoint;
            try
            {
                currentEndpoint.EndEstablish(ar);
            }
            catch (Exception ex)
            {
                LogUtility.GetInstance().Error(MethodBase.GetCurrentMethod(), "EndEstablish调用异常。");
                LogUtility.GetInstance().Debug(MethodBase.GetCurrentMethod(), "EndEstablish调用异常:\r\n" + ex.ToString());
            }
            finally
            {
                // Again, just for sync. reasons.
                _endpointInitCompletedEvent.Set();
            }
        }

  建立对话并捕获对话状态,根据状态发送消息。

                    //Init Conversation and InstantMessagingCall
                    Conversation conversation = new Conversation(_userEndpoint, _convSettings);
                    _instantMessagingCall = new InstantMessagingCall(conversation);

                    //Binding Event
                    _instantMessagingCall.InstantMessagingFlowConfigurationRequested +=
                      this.InstantMessagingCall_FlowConfigurationRequested;

                    //Establish instantMessagingCall
                    _instantMessagingCall.BeginEstablish(string.Format(ConstInfo.FORMAT_SIP_USER, strUser), null,
                        CallEstablishCompleted, _instantMessagingCall);

                    //Wait for Sending message
                    _conversationCompletedEvent.WaitOne();

                    //Wait for establishing InstantMessagingCall;
                    _establishCompletedEvent.WaitOne();

  

        private void InstantMessagingCall_FlowConfigurationRequested(object sender,
           InstantMessagingFlowConfigurationRequestedEventArgs e)
        {
            try
            {
                _instantMessagingFlow = e.Flow;

                // Now that the flow is non-null, bind the event handlers for State 
                // Changed and Message Received. When the flow goes active, 
                // (as indicated by the state changed event) the program will send 
                // the IM in the event handler.
                _instantMessagingFlow.StateChanged += this.InstantMessagingFlow_StateChanged;
            }
            catch (Exception ex)
            {
                 try
                {
                    if (_drInstantMessage != null)
                    {
                        DBProxy.InsertExceptionInfo(_drInstantMessage);
                        _drInstantMessage = null;
                    }
                }
                catch (Exception dbEx)
                {
                }
            }
        }

  

        private void InstantMessagingFlow_StateChanged(object sender, MediaFlowStateChangedEventArgs e)
        {
            try
            {
                // When flow is active, media operations (here, sending an IM) 
                // may begin.
                if (e.State == MediaFlowState.Active)
                {
                    // Send the message on the InstantMessagingFlow.
                    if (_strContent.Contains(ConstInfo.HTML_TEXT_FLAG))
                    {
                        byte[] htmlByte = Encoding.UTF8.GetBytes(_strContent.Replace(ConstInfo.HTML_TEXT_FLAG, string.Empty));
                        _instantMessagingFlow.BeginSendInstantMessage(new System.Net.Mime.ContentType("text/html"), htmlByte,
                            SendMessageCompleted, _instantMessagingFlow);
                    }
                    else
                    {
                        _instantMessagingFlow.BeginSendInstantMessage(_strContent, SendMessageCompleted,
                            _instantMessagingFlow);
                    }
                }
                else if (e.State == MediaFlowState.Terminated)
                {
                    _conversationCompletedEvent.Set();
                }
            }
            catch (Exception ex)
            {
                if (e.State == MediaFlowState.Active)
                {
                    _conversationCompletedEvent.Set();
                }
                else if (e.State == MediaFlowState.Terminated)
                {
                    _conversationCompletedEvent.Set();
                }

            }
        }

  本人可以做lync开发兼职,QQ106888270

原文地址:https://www.cnblogs.com/zorro8z8/p/2654522.html