Exemplo n.º 1
0
        internal void SendMessage(string contactUri, string imText)
        {
            try
            {
                if (_Client.State == ClientState.SignedIn)
                {
                    ConversationManager myConversationManager = _Client.ConversationManager;
                    Conversation        myConversation        = myConversationManager.AddConversation();
                    Participant         myParticipant         = myConversation.AddParticipant(_Client.ContactManager.GetContactByUri(contactUri));

                    if (myParticipant.IsSelf == false)
                    {
                        if (myConversation.State == ConversationState.Active && myConversation.Properties.ContainsKey(ConversationProperty.Subject))
                        {
                            myConversation.Properties[ConversationProperty.Subject] = SUBJECT;
                        }

                        if (myConversation.Modalities.ContainsKey(ModalityTypes.InstantMessage))
                        {
                            InstantMessageModality imModality = (InstantMessageModality)myConversation.Modalities[ModalityTypes.InstantMessage];
                            if (imModality.CanInvoke(ModalityAction.SendInstantMessage))
                            {
                                Dictionary <InstantMessageContentType, string> textMessage = new Dictionary <InstantMessageContentType, string>();
                                textMessage.Add(InstantMessageContentType.PlainText, SUBJECT + " : " + imText);

                                imModality.BeginSendMessage(textMessage, SendMessageCallback, imModality);
                            }
                        }
                    }
                }
            }
            catch
            {
            }
        }
        private void OnAllRecipientsAdded(Conversation currentConversation)
        {
            InstantMessageModality imModality = currentConversation.Modalities[ModalityTypes.InstantMessage] as InstantMessageModality;

            IDictionary <InstantMessageContentType, string> textMessage = new Dictionary <InstantMessageContentType, string>();

            textMessage.Add(InstantMessageContentType.PlainText, _message);

            if (imModality.CanInvoke(ModalityAction.SendInstantMessage))
            {
                IAsyncResult asyncResult = imModality.BeginSendMessage(textMessage, ar => { SendMessageCallback(imModality, ar); }, null);
                if (!_sendIMEvent.WaitOne(75000))
                {
                    Trace.WriteLine($"SendIM action failed in timeout recipients = {string.Join(", ", _recipients)}");
                    SendIMTimeout = true;
                }
                else
                {
                    //if at the end of the send, it remains only 1 participant (me) ==> All other participants are offline
                    //AllPArticipantsOffline = (currentConversation.Participants.Count = 1)

                    Trace.WriteLine($"SendIM action succedeed recipients = {string.Join(", ", _recipients)}");
                    Thread.Sleep(250);
                }
            }
        }
        void Conversation_ParticipantAdded(object sender, ParticipantCollectionChangedEventArgs e)
        {
            // add event handlers for modalities of participants other than self participant:
            if (e.Participant.IsSelf == false)
            {
                if (((Conversation)sender).Modalities.ContainsKey(ModalityTypes.InstantMessage))
                {
                    ((InstantMessageModality)e.Participant.Modalities[ModalityTypes.InstantMessage]).InstantMessageReceived += new EventHandler <MessageSentEventArgs>(ConversationTest_InstantMessageReceived);
                    ((InstantMessageModality)e.Participant.Modalities[ModalityTypes.InstantMessage]).IsTypingChanged        += new EventHandler <IsTypingChangedEventArgs>(ConversationTest_IsTypingChanged);
                }
                Conversation conversation = (Conversation)sender;

                InstantMessageModality imModality = (InstantMessageModality)conversation.Modalities[ModalityTypes.InstantMessage];

                IDictionary <InstantMessageContentType, string> textMessage = new Dictionary <InstantMessageContentType, string>();
                textMessage.Add(InstantMessageContentType.PlainText, "Hello World");

                if (imModality.CanInvoke(ModalityAction.SendInstantMessage))
                {
                    IAsyncResult asyncResult = imModality.BeginSendMessage(
                        textMessage,
                        SendMessageCallback,
                        imModality);
                }
            }
        }
        void Conversation_ParticipantAdded(object sender, ParticipantCollectionChangedEventArgs e)
        {
            // add event handlers for modalities of participants other than self participant:
            if (e.Participant.IsSelf == false)
            {
                if (((Conversation)sender).Modalities.ContainsKey(ModalityTypes.InstantMessage))
                {
                    ((InstantMessageModality)e.Participant.Modalities[ModalityTypes.InstantMessage]).InstantMessageReceived += new EventHandler <MessageSentEventArgs>(ConversationTest_InstantMessageReceived);
                    ((InstantMessageModality)e.Participant.Modalities[ModalityTypes.InstantMessage]).IsTypingChanged        += new EventHandler <IsTypingChangedEventArgs>(ConversationTest_IsTypingChanged);
                }
                Conversation conversation = (Conversation)sender;

                InstantMessageModality imModality = (InstantMessageModality)conversation.Modalities[ModalityTypes.InstantMessage];
                string messageContent             = "Hello World";
                IDictionary <InstantMessageContentType, string> formattedMessage = this.GenerateFormattedMessage(messageContent);

                try
                {
                    if (imModality.CanInvoke(ModalityAction.SendInstantMessage))
                    {
                        IAsyncResult asyncResult = imModality.BeginSendMessage(
                            formattedMessage,
                            SendMessageCallback,
                            imModality);
                    }
                }
                catch (LyncClientException ex)
                {
                    txtErrors.Text = ex.Message;
                }
            }
        }
Exemplo n.º 5
0
        protected async Task DoSendMessageAsync(Conversation conversation, string data, InstantMessageContentType type)
        {
            try
            {
                InstantMessageModality imModality = (InstantMessageModality)conversation.Modalities[ModalityTypes.InstantMessage];
                var formattedMessage = new Dictionary <InstantMessageContentType, string>();
                formattedMessage.Add(type, data);

                if (imModality.CanInvoke(ModalityAction.SendInstantMessage))
                {
                    await Task.Factory.FromAsync(imModality.BeginSendMessage, imModality.EndSendMessage, formattedMessage, imModality);
                }
            }
            catch (LyncClientException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 6
0
 void ConversationTest_InstantMessageReceived(object sender, MessageSentEventArgs e)
 {
     ShowNewMessage(e);
     try
     {
         if (_ConversationImModality.CanInvoke(ModalityAction.SendInstantMessage))
         {
             _ConversationImModality.BeginSendMessage(
                 "Got your message",
                 (ar) =>
             {
                 _ConversationImModality.EndSendMessage(ar);
             }
                 ,
                 null);
         }
     }
     catch (LyncClientException ex)
     {
         txtErrors.Text = ex.Message;
     }
 }