/// <summary> /// Retrives chat instance by id. /// </summary> /// <param name="id"> /// Id of the chat. /// </param> /// <returns> /// Found skype chat instance of null if wrong id was supplied. /// </returns> public SkypeChat GetSkypeChat(string id) { var chat = new SkypeChat { Id = id }; foreach (User user in this.skype.Chat[id].Members) { chat.Contacts.Add(new SkypeContact { Id = user.Handle }); } return chat; }
/// <summary> /// The receive message. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> public void ReceiveMessage(SkypeContact contact, SkypeChat chat, string message) { this.logger.LogMessage( string.Format("Received message <{0}> from <{1}> in chat <{2}>", message, contact.Id, chat.Id)); List <ISkypeCommand> commands = this.commandManager.GetCommandsForChat(chat); ISkypeCommand command = this.messageParser.ParseMessage( message, commands.OfType <AbstractDirectCommand>(), commands.OfType <AbstractReplaceCommand>(), chat.Contacts.Count == 2); if (command != null) { ThreadPool.QueueUserWorkItem( data => { string response; try { response = command.Execute(contact, chat, message); } catch (Exception) { response = "Something bad happened with command execution. Please address it to developers or try later"; } this.SendMessageToChat(chat, response); }); } else if (!this.dataManager.IsChatEnabled(chat.Id)) { // for disabled chats no messages should be shown except the replies to system // commands (like on/help/off). return; } else if (AbstractDirectCommand.IsDirectCommand(message) && chat.Contacts.Count != 2) { // public chats ignore messages that don't match commands since they may be addressed // not to eva. this.SendMessageToChat(chat, "No such command found"); } else if (chat.Contacts.Count == 2) { // For private chats any message is addressed to eva and if the message doesn't match // commands then it is considered to be an incorrect command (EVA-4 issue). this.SendMessageToChat(chat, "No such command found"); } }
/// <summary> /// Retrives chat instance by id. /// </summary> /// <param name="id"> /// Id of the chat. /// </param> /// <returns> /// Found skype chat instance of null if wrong id was supplied. /// </returns> public SkypeChat GetSkypeChat(string id) { var chat = new SkypeChat { Id = id }; foreach (User user in this.skype.Chat[id].Members) { chat.Contacts.Add(new SkypeContact { Id = user.Handle }); } return(chat); }
/// <summary> /// Handles incoming message. /// </summary> /// <param name="message"> /// Received message. /// </param> /// <param name="status"> /// Message status. /// </param> private void OnMessageReceived(ChatMessage message, TChatMessageStatus status) { if (status == TChatMessageStatus.cmsReceived) { if (!message.Sender.IsAuthorized || message.Sender.IsBlocked) { this.logger.LogMessage( string.Format("Receiving message from unauthorized/blocked user: <{0}>", message.Sender.Handle)); return; } SkypeContact contact = this.contactsManager.GetSkypeContact(message.Sender.Handle); SkypeChat chat = this.contactsManager.GetSkypeChat(message.ChatName); this.ReceiveMessage(contact, chat, message.Body); } }
/// <summary> /// The send message to chat. /// </summary> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> public void SendMessageToChat(SkypeChat chat, string message) { if (!string.IsNullOrEmpty(message) && this.skype.Chat[chat.Id] != null) { this.logger.LogMessage(string.Format("Sending message <{0}> to chat <{1}>", message, chat.Id)); try { this.skype.Chat[chat.Id].SendMessage(message); } catch (Exception ex) { this.logger.LogException( string.Format("Failed on sending message <{0}> to chat <{1}>", message, chat.Id), ex); } } }
/// <summary> /// The get commands for chat. /// </summary> /// <param name="chat"> /// The chat. /// </param> /// <returns> /// The <see cref="List"/>. /// </returns> public List <ISkypeCommand> GetCommandsForChat(SkypeChat chat) { List <ISkypeCommand> commands = new List <ISkypeCommand>(this.commonCommands); int projectId = this.dataManager.GetProjectForChat(chat.Id); if (projectId != 0) { commands.AddRange(this.projectsCommands.Where(c => c.ProjectId == projectId)); } // For disabled chats eva accepts only system commands. // Usually they are on and off commands. commands = commands .Where(c => this.dataManager.IsChatEnabled(chat.Id) || c.CommandType == CommandType.System) .ToList(); commands.Add(new HelpCommand(commands.OfType <AbstractDirectCommand>().ToList())); return(commands); }
/// <summary> /// The send message to contact. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> public void SendMessageToContact(SkypeContact contact, SkypeChat chat, string message) { string chatId = chat != null ? chat.Id : "'no chat'"; this.logger.LogMessage( string.Format("Sending message <{0}> to <{1}> in chat <{2}>", message, contact.Id, chatId)); try { this.skype.SendMessage(contact.Id, message); } catch (Exception ex) { this.logger.LogException( string.Format( "Failed on sending message <{0}> from <{1}> in chat <{2}>", message, contact.Id, chatId), ex); } }
/// <summary> /// The execute. /// </summary> /// <param name="sender"> /// The sender. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The args. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public override string Execute(SkypeContact sender, SkypeChat chat, string message) { string projectName = this.ExtractCommandArgument(message, chat.Contacts.Count == 2); int projectId = this.dataManager.GetProjectId(projectName); if (projectId == 0) { return "No project with such name was found"; } try { this.dataManager.SubscribeChat(chat.Id, projectId); } catch (Exception) { return "Oops, something went wrong with subscribing"; } return string.Format("You've successfully been subscribed to project {0}", projectName); }
/// <summary> /// Executes the command on answer to the specified chat name. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="args"> /// The args. /// </param> /// <returns> /// The result string to send back as the answer. /// </returns> public override string Execute(SkypeContact contact, SkypeChat chat, string args) { if (this.dataManager.IsChatEnabled(chat.Id)) { return AlreadyListeningMessage; } this.dataManager.EnabledChat(chat.Id); return HelloMessage; }
/// <summary> /// The get commands for chat. /// </summary> /// <param name="chat"> /// The chat. /// </param> /// <returns> /// The <see cref="List"/>. /// </returns> public List<ISkypeCommand> GetCommandsForChat(SkypeChat chat) { List<ISkypeCommand> commands = new List<ISkypeCommand>(this.commonCommands); int projectId = this.dataManager.GetProjectForChat(chat.Id); if (projectId != 0) { commands.AddRange(this.projectsCommands.Where(c => c.ProjectId == projectId)); } // For disabled chats eva accepts only system commands. // Usually they are on and off commands. commands = commands .Where(c => this.dataManager.IsChatEnabled(chat.Id) || c.CommandType == CommandType.System) .ToList(); commands.Add(new HelpCommand(commands.OfType<AbstractDirectCommand>().ToList())); return commands; }
/// <summary> /// The execute. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public override string Execute(SkypeContact contact, SkypeChat chat, string message) { string argument = this.ExtractCommandArgument(message, chat.Contacts.Count == 2); try { if (!string.IsNullOrEmpty(argument)) { if (argument.Length > MaxFeedbackLength) { return string.Format( "The feedback message is too long. The maximum allowed length is 500. Your's is {0}", argument.Length); } this.dataManager.AddFeedback(contact.Id, argument); } else { return "Please use feedback command with argument"; } } catch (Exception) { return "Sorry, couldn't process your feedback"; } return "Got your message. We will try to take a look at it asap. Thanks!"; }
/// <summary> /// The execute. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public override string Execute(SkypeContact contact, SkypeChat chat, string message) { try { this.dataManager.UnsubscribeChat(chat.Id); } catch (Exception) { return "Oops, something went wrong with unsubscribing"; } return string.Format("You've successfully been unsubscribed from any project"); }
/// <summary> /// Executes the command on answer to the specified chat name. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> /// <returns> /// The result string to send back as the answer. /// </returns> public override string Execute(SkypeContact contact, SkypeChat chat, string message) { string json = string.Empty; string argument = this.regex.Match(message).Value; try { var request = WebRequest.Create(this.url + "rest/api/latest/issue/" + argument) as HttpWebRequest; request.ContentType = "application/json"; request.Method = "GET"; string base64Credentials = this.GetEncodedCredentials(); request.Headers.Add("Authorization", "Basic " + base64Credentials); var response = request.GetResponse() as HttpWebResponse; using (var reader = new StreamReader(response.GetResponseStream())) { json = reader.ReadToEnd(); } } catch (Exception) { return string.Empty; } if (!string.IsNullOrEmpty(json)) { JObject jsonObj = JObject.Parse(json); string key = jsonObj["key"].ToString().ToUpper(); string summary = jsonObj["fields"]["summary"].ToString(); string status; string assignee; if (summary[0] == '{') { summary = jsonObj["fields"]["summary"]["value"].ToString(); status = jsonObj["fields"]["status"]["value"]["name"].ToString(); assignee = jsonObj["fields"]["assignee"]["value"]["displayName"].ToString(); } else { status = jsonObj["fields"]["status"]["name"].ToString(); assignee = jsonObj["fields"]["assignee"].HasValues ? jsonObj["fields"]["assignee"]["displayName"].ToString() : "Unassigned"; } var sb = new StringBuilder(); if (!message.StartsWith("http")) { sb.AppendLine(this.url + "browse/" + key); } sb.AppendLine('"' + summary + '"'); sb.AppendLine(string.Format("Status: {0}", status)); sb.AppendLine(string.Format("Assignee: {0}", assignee)); return sb.ToString(); } return string.Empty; }
/// <summary> /// The receive message. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> public void ReceiveMessage(SkypeContact contact, SkypeChat chat, string message) { this.logger.LogMessage( string.Format("Received message <{0}> from <{1}> in chat <{2}>", message, contact.Id, chat.Id)); List<ISkypeCommand> commands = this.commandManager.GetCommandsForChat(chat); ISkypeCommand command = this.messageParser.ParseMessage( message, commands.OfType<AbstractDirectCommand>(), commands.OfType<AbstractReplaceCommand>(), chat.Contacts.Count == 2); if (command != null) { ThreadPool.QueueUserWorkItem( data => { string response; try { response = command.Execute(contact, chat, message); } catch (Exception) { response = "Something bad happened with command execution. Please address it to developers or try later"; } this.SendMessageToChat(chat, response); }); } else if (!this.dataManager.IsChatEnabled(chat.Id)) { // for disabled chats no messages should be shown except the replies to system // commands (like on/help/off). return; } else if (AbstractDirectCommand.IsDirectCommand(message) && chat.Contacts.Count != 2) { // public chats ignore messages that don't match commands since they may be addressed // not to eva. this.SendMessageToChat(chat, "No such command found"); } else if (chat.Contacts.Count == 2) { // For private chats any message is addressed to eva and if the message doesn't match // commands then it is considered to be an incorrect command (EVA-4 issue). this.SendMessageToChat(chat, "No such command found"); } }
/// <summary> /// The execute. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The message. /// </param> /// <returns> /// The <see cref="string"/>. /// </returns> public abstract string Execute(SkypeContact contact, SkypeChat chat, string message);
/// <summary> /// Executes the command on answer to the specified chat name. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="chat"> /// The chat. /// </param> /// <param name="message"> /// The args. /// </param> /// <returns> /// The result string to send back as the answer. /// </returns> public override string Execute(SkypeContact contact, SkypeChat chat, string message) { string argument = this.ExtractCommandArgument(message, chat.Contacts.Count == 2); string result = string.Empty; if (!string.IsNullOrWhiteSpace(argument)) { Thread thread = new Thread( () => { try { result = new Engine().Execute(argument).GetCompletionValue().ToString(); } catch (Exception) { result = "Sorry, couldn't parse."; } }); thread.Start(); if (!thread.Join(Timeout)) { thread.Abort(); result = "Sorry, your code took too long and has been aborted."; } } return result; }