/// <summary> /// The get skype contact. /// </summary> /// <param name="id"> /// The handle. /// </param> /// <returns> /// The <see cref="SkypeContact"/>. /// </returns> public SkypeContact GetSkypeContact(string id) { foreach (User user in this.skype.Friends) { if (user.Handle == id) { var contact = new SkypeContact { Id = id }; string[] nameTokens = user.DisplayName.Split(' '); if (nameTokens.Length > 0) { contact.FirstName = nameTokens[0]; } if (nameTokens.Length > 1) { contact.LastName = nameTokens[1]; } return(contact); } } return(new SkypeContact { Id = id }); }
/// <summary> /// The get skype contact. /// </summary> /// <param name="id"> /// The handle. /// </param> /// <returns> /// The <see cref="SkypeContact"/>. /// </returns> public SkypeContact GetSkypeContact(string id) { foreach (User user in this.skype.Friends) { if (user.Handle == id) { var contact = new SkypeContact { Id = id }; string[] nameTokens = user.DisplayName.Split(' '); if (nameTokens.Length > 0) { contact.FirstName = nameTokens[0]; } if (nameTokens.Length > 1) { contact.LastName = nameTokens[1]; } return contact; } } return new SkypeContact { Id = id }; }
/// <summary> /// The try authorize user. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="replyMessage"> /// The reply message. /// </param> /// <param name="shouldBeAuthorized"> /// The should be authorized. /// </param> public void TryAuthorizeUser(SkypeContact contact, out string replyMessage, out bool shouldBeAuthorized) { replyMessage = string.Empty; shouldBeAuthorized = false; replyMessage += "Hello " + contact.Id + "! I'm the Eva bot. "; this.skype.UsersWaitingAuthorization.RemoveAll(); }
/// <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> /// 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 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> /// Receives 'add to friend' request and processes it. /// </summary> /// <param name="user"> /// The user. /// </param> public void ReceiveAuthorizationRequest(User user) { this.logger.LogMessage(string.Format("Received 'add to friend' request from <{0}>", user.Handle)); if (user.IsAuthorized) { return; } string replyMessage; bool shouldBeAuthirized; SkypeContact contact = this.contactsManager.GetSkypeContact(user.Handle); this.contactsManager.TryAuthorizeUser(contact, out replyMessage, out shouldBeAuthirized); if (shouldBeAuthirized) { user.IsAuthorized = true; } this.SendMessageToContact(contact, null, replyMessage); }
/// <summary> /// The send message to contact. /// </summary> /// <param name="contact"> /// The contact. /// </param> /// <param name="message"> /// The message. /// </param> public void SendMessageToContact(SkypeContact contact, string message) { this.SendMessageToContact(contact, null, message); }
/// <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="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="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> /// 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 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> /// 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; }