From 0bb9a58711698d009b631c0850b4f4be0ba4df23 Mon Sep 17 00:00:00 2001 From: jetsparrow Date: Wed, 19 Dec 2018 22:43:30 +0300 Subject: [PATCH] handle commands with mentions skip commands directed at other bots --- JetKarmaBot/CommandRouter.cs | 19 ++++++++++++---- JetKarmaBot/Commands/CommandString.cs | 31 ++++++++++++++++++++------- JetKarmaBot/JetKarmaBot.cs | 2 +- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/JetKarmaBot/CommandRouter.cs b/JetKarmaBot/CommandRouter.cs index 7f944cf..71cfb46 100644 --- a/JetKarmaBot/CommandRouter.cs +++ b/JetKarmaBot/CommandRouter.cs @@ -3,20 +3,30 @@ using System; using System.Collections.Generic; using System.Linq; using Telegram.Bot.Args; +using Telegram.Bot.Types; namespace JetKarmaBot { class ChatCommandRouter { - Dictionary commands = new Dictionary(); + User BotUser { get; } + + public ChatCommandRouter(User botUser) + { + BotUser = botUser; + } + public bool Execute(object sender, MessageEventArgs args) { var text = args.Message.Text; - + if (CommandString.TryParse(text, out var cs)) { - if (commands.ContainsKey(cs.Name)) - return commands[cs.Name].Execute(sender,args); + if (cs.UserName != null && cs.UserName != BotUser.Username) // directed not at us! + return false; + + if (commands.ContainsKey(cs.Command)) + return commands[cs.Command].Execute(sender,args); } return false; @@ -32,5 +42,6 @@ namespace JetKarmaBot } } + Dictionary commands = new Dictionary(); } } diff --git a/JetKarmaBot/Commands/CommandString.cs b/JetKarmaBot/Commands/CommandString.cs index fb2fd2f..fe8f304 100644 --- a/JetKarmaBot/Commands/CommandString.cs +++ b/JetKarmaBot/Commands/CommandString.cs @@ -1,31 +1,46 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Text; +using System.Text.RegularExpressions; namespace JetKarmaBot.Commands { public class CommandString { - public CommandString(string name, params string[] parameters) + public CommandString(string command, params string[] parameters) { - Name = name; + Command = command; Parameters = parameters; } - public string Name { get; } + public string Command { get; } + public string UserName { get; set; } = null; public string[] Parameters { get; } + public static readonly char[] WS_CHARS = new[] { ' ', '\r', '\n', '\n' }; + public static bool TryParse(string s, out CommandString result) { result = null; if (string.IsNullOrWhiteSpace(s) || s[0] != '/') return false; - int space = s.IndexOf(' '); - if (space < 0) - result = new CommandString(s.Substring(1)); - else - result = new CommandString(s.Substring(1, space - 1), s.Substring(space).Split(' ', StringSplitOptions.RemoveEmptyEntries)); + string[] words = s.Split(WS_CHARS, StringSplitOptions.RemoveEmptyEntries); + + if (!words.Any()) + return false; + + var cmdRegex = new Regex(@"/(?\w+)(@(?\w+))?"); + var match = cmdRegex.Match(words.First()); + if (!match.Success) + return false; + + string cmd = match.Groups["cmd"].Captures.First().Value; + string username = match.Groups["name"].Captures.FirstOrDefault()?.Value; + string[] parameters = words.Skip(1).ToArray(); + + result = new CommandString(cmd, parameters) { UserName = username}; return true; } diff --git a/JetKarmaBot/JetKarmaBot.cs b/JetKarmaBot/JetKarmaBot.cs index d350c4a..9cc1c85 100644 --- a/JetKarmaBot/JetKarmaBot.cs +++ b/JetKarmaBot/JetKarmaBot.cs @@ -59,7 +59,7 @@ namespace JetKarmaBot void InitCommands(Container c) { - commands = new ChatCommandRouter(); + Commands = new ChatCommandRouter(Me); Commands.Add(c.ResolveObject(new StartCommand())); Commands.Add(c.ResolveObject(new AwardCommand(Me))); }