public static HipchatMessage ToHipchatMessage(this RoomMessageJson roomMessage) { var hipchatMessage = new HipchatMessage { DateSent = roomMessage.Item.Message.Date, FileUrl = roomMessage.Item.Message.File, From = new HipchatUser { MentionName = roomMessage.Item.Message.From.Mention_Name, Name = roomMessage.Item.Message.From.Name, UserId = roomMessage.Item.Message.From.Id }, MessageSent = roomMessage.Item.Message.Message, RoomId = roomMessage.Item.Room.Id, RoomName = roomMessage.Item.Room.Name }; if (roomMessage.Item.Message.Mentions != null) { hipchatMessage.Mentions = roomMessage.Item.Message.Mentions.Select(x => new HipchatUser { MentionName = x.Mention_Name, Name = x.Name, UserId = x.Id }).ToList(); } return hipchatMessage; }
/// <summary> /// This method is called for every message received /// this is the main starting point where we kick off the parser /// and the dispatcher /// </summary> /// <param name="message">The message that was sent via hipchat</param> private void HandleMessage(HipchatMessage message) { var command = CommandParser.GetCommandFromMessage(message); if (command != null) Dispatcher.DispatchCommands(command, message); }
public static void DispatchCommands(List<ICommand> commands, HipchatMessage originalMessage) { if (!commands.Any()) BotsoHipchatHelpers.SendError("I don't understand that command<br/>Type <code>Botso commands</code> for a list of commands", originalMessage.RoomId); BotsoHipchatHelpers.SendError("<code><pre>" +originalMessage.Dump() + "<pre></code>", originalMessage.RoomId); commands.Each(x => x.Execute()); }
//factory method public ICommand CreateCommand(Type type, string cmdText, HipchatMessage originalMessage) { var cmd = _commandDictionary[type]; var match = Regex.Match(cmdText, cmd.Pattern); if (match.Groups.Count > 1) { Enumerable.Range(1, match.Groups.Count -1).Each( x => cmd.Arguments.Add(x,match.Groups[(int) x].Value)); } cmd.OriginalMessage = originalMessage; return cmd; }
/// <summary> /// Gets a typed ICommand from a hipchat message /// </summary> /// <param name="message">The hipchat message recieved</param> /// <returns>A typed ICommand object</returns> public static List<ICommand> GetCommandFromMessage(HipchatMessage message) { if (IsCommand(message)) { var cmdText = GetCommandText(message.MessageSent); var commandFactory = new CommandFactory(); //find what type this command is (can be multiples) var cmdTypes = CommandHelper.GetCommandTypes(cmdText); var matchedCommands = cmdTypes.Select(type => commandFactory.CreateCommand(type, cmdText, message)).ToList(); return matchedCommands; } return new List<ICommand>(); }
/// <summary> /// Returns weather or not a message is a command /// </summary> /// <param name="message">The hipchat message received</param> public static bool IsCommand(HipchatMessage message) { return message.MessageSent.StartsWith(CommandPrefix,true, CultureInfo.InvariantCulture); }