Example #1
0
        public void HandleMessage(string incomingMessage, Action <string> answerCallback, object data)
        {
            if (incomingMessage.Contains(botCommandPrefix))
            {
                var command = incomingMessage.Substring(incomingMessage.IndexOf(botCommandPrefix, StringComparison.InvariantCulture) + botCommandPrefix.Length);
                if (!string.IsNullOrEmpty(command))
                {
                    var systemCommandList = systemCommands.Where(x => x.Key.ToLower() == command.ToLower()).ToList();
                    if (systemCommandList.Any())
                    {
                        var systemComand = systemCommandList.First();
                        answerCallback(systemComand.Value.Item2());
                    }
                    else
                    {
                        IActionHandler handler = FindHandler(command, out command);

                        HandleCommand(incomingMessage, answerCallback, data, command, handler);
                    }
                }
            }
            else
            {
                IActionHandler handler = FindHandlerByRegex(incomingMessage);
                if (handler != null)
                {
                    handler.HandleMessage(incomingMessage, data, answerCallback);
                }
            }
        }
Example #2
0
        private void HandleCommand(string incomingMessage, Action <string> answerCallback, object data, string command, IActionHandler handler)
        {
            if (handler != null)
            {
                string args = incomingMessage.Substring(incomingMessage.IndexOf(command, StringComparison.InvariantCulture) + command.Length).Trim();

                IActionHandler hnd   = handler;
                var            cts   = new CancellationTokenSource(TimeSpan.FromSeconds(commandTimeoutSec));
                var            token = cts.Token;

                Task.Run(() =>
                {
                    using (cts.Token.Register(Thread.CurrentThread.Abort))
                    {
                        try
                        {
                            hnd.HandleMessage(args, data, answerCallback);
                        }
                        catch (Exception ex)
                        {
                            if (OnErrorOccured != null)
                            {
                                OnErrorOccured(ex);
                            }
                            answerCallback(command + " пал смертью храбрых :(");
                        }
                    }
                }, token);
            }
        }
Example #3
0
        public void HandleMessage(string incomingMessage, Action <string, AnswerBehaviourType> answerCallback, object data)
        {
            if (incomingMessage.Contains(botCommandPrefix))
            {
                var command = incomingMessage.Substring(incomingMessage.IndexOf(botCommandPrefix, StringComparison.InvariantCulture) + botCommandPrefix.Length);
                if (!string.IsNullOrEmpty(command))
                {
                    var systemCommandList = systemCommands.Where(x => x.Key.ToLower() == command.ToLower()).ToList();
                    if (systemCommandList.Any())
                    {
                        var systemComand = systemCommandList.First();
                        answerCallback(systemComand.Value.Item2(), AnswerBehaviourType.Text);
                    }
                    else
                    {
                        IActionHandler handler = FindHandler(command, out command);

                        if (handler != null)
                        {
                            string args = incomingMessage.Substring(incomingMessage.IndexOf(command, StringComparison.InvariantCultureIgnoreCase) + command.Length).Trim();

                            IActionHandler hnd   = handler;
                            var            cts   = new CancellationTokenSource(TimeSpan.FromSeconds(commandTimeoutSec));
                            var            token = cts.Token;

                            Task.Run(() =>
                            {
                                using (cts.Token.Register(Thread.CurrentThread.Abort))
                                {
                                    try
                                    {
                                        hnd.HandleMessage(command, args, data, answerCallback);
                                    }
                                    catch (Exception ex)
                                    {
                                        if (OnErrorOccured != null)
                                        {
                                            OnErrorOccured(ex);
                                        }
                                        answerCallback(command + " сломан :(", AnswerBehaviourType.Text);
                                    }
                                }
                            }, token);
                        }
                    }
                }
            }
        }