Пример #1
0
        public async Task MessageReceived(SlackMessage message)
        {
            Console.WriteLine("[[[Message started]]]");

            IMiddleware pipeline        = _pipelineFactory.GetPipeline();
            var         incomingMessage = new IncomingMessage
            {
                RawText        = message.Text,
                FullText       = message.Text,
                UserId         = message.User.Id,
                Username       = GetUsername(message),
                Channel        = message.ChatHub.Id,
                ChannelType    = message.ChatHub.Type == SlackChatHubType.DM ? ResponseType.DirectMessage : ResponseType.Channel,
                UserChannel    = await GetUserChannel(message),
                BotName        = _connection.Self.Name,
                BotId          = _connection.Self.Id,
                BotIsMentioned = message.MentionsBot
            };

            incomingMessage.TargetedText = incomingMessage.GetTargetedText();

            try
            {
                foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                {
                    await SendMessage(responseMessage);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR WHILE PROCESSING MESSAGE: {0}", ex);
            }

            Console.WriteLine("[[[Message ended]]]");
        }
Пример #2
0
 public virtual void Invoke(Context context)
 {
     if (next != null)
     {
         next.Invoke(context);
     }
 }
Пример #3
0
        public ResponseMessage Calculate(RequestMessage req)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            _log.Info($"[request catch '{req.UserChannel} - Value {req.Value}']");
            IMiddleware pipeline = _container.GetMiddlewarePipeline();

            try
            {
                var res = pipeline.Invoke(req);
                stopwatch.Stop();
                _log.Info($"[Message ended - Took {stopwatch.ElapsedMilliseconds} milliseconds]");
                _averageResponse.Log(stopwatch.ElapsedMilliseconds);
                return(res);
            }
            catch (Exception ex)
            {
                _log.Error($"ERROR WHILE PROCESSING MESSAGE: {ex}");
            }

            stopwatch.Stop();

            _log.Info($"[Message ended - Took {stopwatch.ElapsedMilliseconds} milliseconds]");
            _averageResponse.Log(stopwatch.ElapsedMilliseconds);
            return(null);
        }
Пример #4
0
 /// <inheritdoc/>
 void IApplication.Invoke(IContext context)
 {
     Invoke(context as HttpContext, ctx =>
     {
         if (next != null)
         {
             next.Invoke(ctx);
         }
     });
 }
Пример #5
0
        /// <summary>
        /// Executes all middlewares
        /// </summary>
        internal async Task RunSequence(MvcAppBuilder app, HttpRequest request, HttpResponse response)
        {
            foreach (MiddlewareDescriptor descriptor in app.Descriptors)
            {
                IMiddleware middleware = CreateInstance(descriptor);
                await middleware.Invoke(request, response, SetResult);

                if (LastResult != null)
                {
                    break;
                }
            }
        }
Пример #6
0
    public async Task Invoke(HttpContext context)
    {
        IMiddleware middleware = null;

        if (routes.TryGetValue(context.Request.Path.Value, out middleware))
        {
            await middleware.Invoke(context);
        }
        else
        {
            await next(context);
        }
    }
Пример #7
0
        public SingleChannel Use(IMiddleware middleware)
        {
            Func <RequestDelegate, RequestDelegate> mymiddleware = next =>
            {
                return(context =>
                {
                    middleware.Invoke(context);
                    return next(context);
                });
            };

            middlewares.Add(mymiddleware);
            return(this);
        }
Пример #8
0
        public async Task MessageReceived(SlackMessage message)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            _logger.LogInformation($"[Message found from '{message.User.Name}']");

            IMiddleware pipeline        = _container.GetMiddlewarePipeline();
            var         incomingMessage = new IncomingMessage
            {
                RawText        = message.Text,
                FullText       = message.Text,
                UserId         = message.User.Id,
                Username       = GetUsername(message),
                UserEmail      = message.User.Email,
                Channel        = message.ChatHub.Id,
                ChannelType    = message.ChatHub.Type == SlackChatHubType.DM ? ResponseType.DirectMessage : ResponseType.Channel,
                UserChannel    = await GetUserChannel(message),
                BotName        = _connection.Self.Name,
                BotId          = _connection.Self.Id,
                BotIsMentioned = message.MentionsBot
            };

            DataAccess dataAccess = new DataAccess("Server=(localdb)\\mssqllocaldb; Database=Reczept");
            User       user       = new User()
            {
                MemberId = incomingMessage.UserId,
                Name     = incomingMessage.Username
            };

            dataAccess.CheckIfExistingUser(user);
            _logger.LogInformation($"[User Created]");
            incomingMessage.TargetedText = incomingMessage.GetTargetedText();

            try
            {
                foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                {
                    await SendMessage(responseMessage);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"ERROR WHILE PROCESSING MESSAGE: {ex}");
            }

            stopwatch.Stop();

            _logger.LogInformation($"[Message ended - Took {stopwatch.ElapsedMilliseconds} milliseconds]");
            _averageResponse.Log(stopwatch.ElapsedMilliseconds);
        }
Пример #9
0
        public async Task MessageReceived(SlackMessage message)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            _log.Log($"[Message found from '{message.User.Name}']");

            IMiddleware pipeline        = _container.GetMiddlewarePipeline();
            var         incomingMessage = new IncomingMessage
            {
                RawText        = message.Text,
                FullText       = message.Text,
                UserId         = message.User.Id,
                Username       = GetUsername(message),
                Channel        = message.ChatHub.Id,
                ChannelType    = message.ChatHub.Type == SlackChatHubType.DM ? ResponseType.DirectMessage : ResponseType.Channel,
                UserChannel    = await GetUserChannel(message),
                BotName        = _connection.Self.Name,
                BotId          = _connection.Self.Id,
                BotIsMentioned = message.MentionsBot
            };

            incomingMessage.TargetedText = incomingMessage.GetTargetedText();

            try
            {
                foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                {
                    await SendMessage(responseMessage);
                }
            }
            catch (Exception ex)
            {
                try
                {
                    await SendMessage(incomingMessage.ReplyToChannel($"@{incomingMessage.Username} Uh Oh! Something went on fire!"));
                }
                catch
                {
                    _log.Log("Unable to send error code to chat");
                }
                _log.Log($"ERROR WHILE PROCESSING MESSAGE: {ex}");
            }

            stopwatch.Stop();

            _log.Log($"[Message ended - Took {stopwatch.ElapsedMilliseconds} milliseconds]");
            _averageResponse.Log(stopwatch.ElapsedMilliseconds);
        }
Пример #10
0
        public async Task MessageReceived(SlackMessage message)
        {
            Stopwatch stopwatch = Stopwatch.StartNew();

            this._logger.LogInformation($"[Message found from '{message.User.Name}']");

            IMiddleware pipeline        = this._container.GetMiddlewarePipeline();
            var         incomingMessage = new IncomingMessage
            {
                RawText     = message.Text,
                FullText    = message.Text,
                UserId      = message.User.Id,
                Username    = this.GetUsername(message),
                UserEmail   = message.User.Email,
                Channel     = message.ChatHub.Id,
                ChannelType = message.ChatHub.Type == SlackChatHubType.DM
                    ? ResponseType.DirectMessage
                    : ResponseType.Channel,
                UserChannel    = await this.GetUserChannel(message),
                BotName        = this._connection.Self.Name,
                BotId          = this._connection.Self.Id,
                BotIsMentioned = message.MentionsBot
            };

            incomingMessage.TargetedText = incomingMessage.GetTargetedText();

            try
            {
                foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                {
                    await this.SendMessage(responseMessage);
                }
            }
            catch (Exception ex)
            {
                this._logger.LogError($"ERROR WHILE PROCESSING MESSAGE: {ex}");
            }

            stopwatch.Stop();

            this._logger.LogInformation($"[Message ended - Took {stopwatch.ElapsedMilliseconds} milliseconds]");
            this._averageResponse.Log(stopwatch.ElapsedMilliseconds);
        }
Пример #11
0
        public static IOkraAppBuilder UseMiddleware <TMiddleware, TOptions>(this IOkraAppBuilder app, TOptions options)
            where TMiddleware : IMiddleware <TOptions>
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            var applicationServices = app.ApplicationServices;

            return(app.Use(next =>
            {
                IMiddleware <TOptions> middleware = applicationServices.GetService(typeof(TMiddleware)) as IMiddleware <TOptions>;
                middleware.Configure(next, options);

                return context =>
                {
                    return middleware.Invoke(context);
                };
            }));
        }
Пример #12
0
        public async Task MessageReceived(SlackMessage message)
        {
            using (_metrics.Measure.Timer.Time(ResponseOptions))
            {
                _logger.LogInformation($"[Message found from '{message.User.Name}']");

                IMiddleware pipeline        = null;//_container.GetMiddlewarePipeline();
                var         incomingMessage = new IncomingMessage
                {
                    RawText        = message.Text,
                    FullText       = message.Text,
                    UserId         = message.User.Id,
                    Username       = GetUsername(message),
                    UserEmail      = message.User.Email,
                    Channel        = message.ChatHub.Id,
                    ChannelType    = message.ChatHub.Type == SlackChatHubType.DM ? ResponseType.DirectMessage : ResponseType.Channel,
                    UserChannel    = await GetUserChannel(message),
                    BotName        = _connection.Self.Name,
                    BotId          = _connection.Self.Id,
                    BotIsMentioned = message.MentionsBot
                };

                incomingMessage.TargetedText = incomingMessage.GetTargetedText();

                try
                {
                    foreach (ResponseMessage responseMessage in pipeline.Invoke(incomingMessage))
                    {
                        await SendMessage(responseMessage);
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError($"ERROR WHILE PROCESSING MESSAGE: {ex}");
                }
            }
        }
Пример #13
0
 protected IEnumerable <ResponseMessage> Next(IncomingMessage message)
 {
     return(_next.Invoke(message));
 }
Пример #14
0
 public void Invoke <TAction>(TAction action) =>
 middleware.Invoke(store, next, action);
 protected ResponseMessage Next(RequestMessage message)
 {
     return(_next.Invoke(message));
 }
Пример #16
0
 protected internal IEnumerable <ResponseMessage> Next(IncomingMessage message)
 {
     return(_next?.Invoke(message) ?? new ResponseMessage[0]);
 }
Пример #17
0
 public async Task ExecuteAsync(ISockContext context)
 {
     await _start.Invoke(context);
 }