Exemple #1
0
 public void SendMessage(int userId, string message, IEnumerable <PhotosPhoto> attachments = null)
 {
     try
     {
         var result = cit.Messages.Send(new Citrina.StandardApi.Models.MessagesSendRequest()
         {
             AccessToken = token, UserId = userId, Message = message, Attachment = attachments == null ? null : string.Join(",", attachments.Select(a => "photo" + a.OwnerId + '_' + a.Id).ToArray())
         }).Result;
         if (result.Response.HasValue)
         {
             MessageSended?.Invoke(this, new MessageEventArgs <TUserStates>(new VkMessage <TUserStates>(this, userId, message, result.Response.Value)));
         }
         else
         {
             UpdateMessagesError?.Invoke(this, EventArgs.Empty);
         }
     }
     catch
     {
         UpdateMessagesError?.Invoke(this, EventArgs.Empty);
     }
 }
Exemple #2
0
 /// <summary> Отправка сообщения </summary>
 public void SendMessage(int userId, string message, IEnumerable <string> attachments = null)
 {
     try
     {
         var result = client.Messages.Send(new Citrina.StandardApi.Models.MessagesSendRequest()
         {
             AccessToken = token, UserId = userId, Message = message, Attachment = attachments == null ? null : string.Join(",", attachments /*attachments.Select(a => "photo" + a.OwnerId + '_' + a.Id)*/.ToArray())
         }).Result;
         if (result.Response.HasValue)
         {
             MessageSended?.Invoke(this, new MessageEventArgs(new Message(userId, DateTime.Now, MessageDirection.Out, message, string.Join(",", attachments ?? Enumerable.Empty <string>()))));
         }
         else
         {
             SendMessageError?.Invoke(this, new MessageEventArgs(new Message(userId, DateTime.Now, MessageDirection.Out, message, string.Join(",", attachments ?? Enumerable.Empty <string>()))));
         }
     }
     catch
     {
         UpdateMessagesError?.Invoke(this, EventArgs.Empty);
     }
 }
 public async Task AddMessageAsync(ChatMessage chatMessage)
 {
     MessageSended?.Invoke(chatMessage.Message);
 }
Exemple #4
0
        /// <summary>
        /// Listen for messages from server.
        /// </summary>
        public async void Listen()
        {
            if (!isLoggedIn)
            {
                return;
            }

            // message from server
            Message message;

            while (true)
            {
                try
                {
                    // wait for message
                    message = await client.ReceiveMessageAsync();
                }
                catch (ConnectionInterruptedException)
                {
                    if (isLoggedIn)
                    {
                        ConnectionBreaks?.Invoke();
                    }

                    return;
                }

                // handle message
                if (message is AllUsersMessage)
                {
                    OnlineUsersList  = new List <string>(((AllUsersMessage)message).OnlineUsers);
                    OfflineUsersList = new List <string>(((AllUsersMessage)message).OfflineUsers);
                }
                else if (message is FriendsMessage)
                {
                    OnlineFriendsList  = new List <string>(((FriendsMessage)message).OnlineFriends);
                    OfflineFriendsList = new List <string>(((FriendsMessage)message).OfflineFriends);
                }
                else if (message is IncomeFriendshipRequestsMessage)
                {
                    IncomeRequestsList = new List <string>(((IncomeFriendshipRequestsMessage)message).Logins);
                }
                else if (message is OutcomeFriendshipRequestsMessage)
                {
                    OutcomeRequestsList = new List <string>(((OutcomeFriendshipRequestsMessage)message).Logins);
                }
                else if (message is UserActionMessage)
                {
                    UserActionMessage msg = (UserActionMessage)message;

                    switch (msg.Action)
                    {
                    case UserActions.GoOnline:
                        FriendGoOnline?.Invoke(msg.UserLogin);
                        break;

                    case UserActions.GoOffline:
                        FriendGoOffline?.Invoke(msg.UserLogin);
                        break;

                    case UserActions.AcceptFriendship:
                        FriendshipAccepted?.Invoke(msg.UserLogin, msg.Time);
                        OutcomeRequestsList?.Remove(msg.UserLogin);
                        if (OnlineFriendsList != null && !OnlineFriendsList.Contains(msg.UserLogin))
                        {
                            OnlineFriendsList.Add(msg.UserLogin);
                        }
                        break;

                    case UserActions.RejectFriendship:
                        FriendshipRejected?.Invoke(msg.UserLogin, msg.Time);
                        OutcomeRequestsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.SendFriendshipRequest:
                        NewFriendshipRequest?.Invoke(msg.UserLogin, msg.Time);
                        if (IncomeRequestsList != null && !IncomeRequestsList.Contains(msg.UserLogin))
                        {
                            IncomeRequestsList.Add(msg.UserLogin);
                        }
                        break;

                    case UserActions.CancelFriendshipRequest:
                        FriendshipRequestCancelled?.Invoke(msg.UserLogin, msg.Time);
                        IncomeRequestsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.RemoveFromFriends:
                        RemovedFromeFriends?.Invoke(msg.UserLogin, msg.Time);
                        OnlineFriendsList?.Remove(msg.UserLogin);
                        OfflineFriendsList?.Remove(msg.UserLogin);
                        break;

                    case UserActions.MessageSended:
                        MessageSended?.Invoke(msg.UserLogin, msg.Time);
                        break;

                    default:
                        break;
                    }
                }
                else if (message is NewReplyMessage)
                {
                    Conversations.AddReply(
                        ((NewReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((NewReplyMessage)message).Author,
                        Time   = ((NewReplyMessage)message).Time,
                        Text   = ((NewReplyMessage)message).Text
                    }
                        );
                    NewReplyComes?.Invoke(
                        ((NewReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((NewReplyMessage)message).Author,
                        Time   = ((NewReplyMessage)message).Time,
                        Text   = ((NewReplyMessage)message).Text
                    });
                }
                else if (message is OldReplyMessage)
                {
                    Conversations.InsertReplyToTop(
                        ((OldReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((OldReplyMessage)message).Author,
                        Time   = ((OldReplyMessage)message).Time,
                        Text   = ((OldReplyMessage)message).Text
                    }
                        );
                    OldReplyComes?.Invoke(
                        ((OldReplyMessage)message).Interlocutor,
                        new ConversationReply
                    {
                        Author = ((OldReplyMessage)message).Author,
                        Time   = ((OldReplyMessage)message).Time,
                        Text   = ((OldReplyMessage)message).Text
                    });
                }
            }
        }
Exemple #5
0
 public void Send(string message)
 {
     MessageSended?.Invoke(this, message);
 }
Exemple #6
0
 public Controller()
 {
     vk.MessageReceived     += Vk_MessageReceived;
     vk.MessageSended       += (s, e) => MessageSended?.Invoke(s, e);
     vk.UpdateMessagesError += (s, e) => MessagesUpdateError?.Invoke(s, e);
 }
 public void SendMessage(object chat, object message)
 {
     MessageSended?.Invoke(chat, message);
 }
Exemple #8
0
 protected virtual void OnMessageSended(MessageSendedEventArgs e)
 {
     MessageSended?.Invoke(this, e);
 }