示例#1
0
        public override async Task HandleAsync(
            IUpdateContext context,
            UpdateDelegate next,
            string[] args,
            CancellationToken cancellationToken
            )
        {
            string agencyTag = context.GetUserProfile().DefaultAgencyTag;

            if (args.Length == 0)
            {
                _logger.LogTrace("There is no argument provided. Instructing the user on how to use this command.");
                await SendUsageInstructionsAsync(context, agencyTag, cancellationToken)
                .ConfigureAwait(false);
            }
            else
            {
                _logger.LogTrace(
                    "Passing the command args, the bus route/direction query, to the rest of the pipeline."
                    );
                context.Items[nameof(BusPredictionsContext)] = new BusPredictionsContext
                {
                    Query      = args[0],
                    Interfaces = new List <string>(new[] { "command" }),
                };
                await next(context, cancellationToken).ConfigureAwait(false);
            }
        }
示例#2
0
        public override async Task HandleAsync(
            IUpdateContext context,
            UpdateDelegate next,
            string[] args,
            CancellationToken cancellationToken
            )
        {
            if (args.Any() && args[0].Equals("remove", StringComparison.OrdinalIgnoreCase))
            {
                _logger.LogTrace(@"Removing user profile upon a ""/profile remove"" command.");

                await context.Bot.Client.SendTextMessageAsync(
                    context.Update.Message.Chat,
                    $"{context.Update.Message.From.FirstName}, You are about to remove your profile. " +
                    "Sadly, I won't remember much of our conversations after the removal.😟\n\n" +
                    "*Are you sure you want to remove your profile?* Reply to this message with the text `forget me`.",
                    ParseMode.Markdown,
                    replyMarkup : new ForceReplyMarkup(),
                    cancellationToken : cancellationToken
                    );
            }
            else
            {
                _logger.LogTrace(@"Showing the user his profile upon the ""/profile"" command.");
                var profile = context.GetUserProfile();

                var agency = await _agencyRepo.GetByTagAsync(profile.DefaultAgencyTag, cancellationToken)
                             .ConfigureAwait(false);

                if (agency != null)
                {
                    await context.Bot.Client.SendTextMessageAsync(
                        context.Update.Message.Chat,
                        $"Hey {context.Update.Message.From.FirstName}\n" +
                        $"Your default transit agency is set to *{agency.Title}* " +
                        $"in {agency.Region}, {agency.Country}.\n\n" +
                        "💡 *Pro Tip*: You can remove your profile from my memory by sending " +
                        "the `/profile remove` message.",
                        ParseMode.Markdown,
                        cancellationToken : cancellationToken
                        ).ConfigureAwait(false);
                }
                else
                {
                    _logger.LogError(
                        "User has the agency {0} on his profile but the agency is not found in the database.",
                        profile.DefaultAgencyTag
                        );

                    // ToDo clear the profile
                }
            }
        }
        public async Task HandleAsync(IUpdateContext context, UpdateDelegate next, CancellationToken cancellationToken)
        {
            var    busContext = (BusPredictionsContext)context.Items[nameof(BusPredictionsContext)];
            string agencyTag  = context.GetUserProfile().DefaultAgencyTag;

            string query = busContext.Interfaces[0] == "command"
                ? busContext.Query
                : $"{busContext.RouteTag} {busContext.DirectionTag}";

            var result = await _agencyParser.FindMatchingRouteDirectionsAsync(
                agencyTag,
                query,
                cancellationToken
                ).ConfigureAwait(false);

            if (result.Error == null)
            {
                if (result.Matches.Length == 1)
                {
                    _logger.LogTrace(
                        "The exact route and the direction are found. Inserting them into the cache."
                        );
                    var match = result.Matches.Single();

                    busContext.RouteTag     = match.Route.Tag;
                    busContext.DirectionTag = match.Direction.Tag;

                    await _cache.SetBusPredictionAsync(context.Update.ToUserchat(), busContext, cancellationToken)
                    .ConfigureAwait(false);

                    await next(context, cancellationToken).ConfigureAwait(false);
                }
                else
                {
                    _logger.LogTrace(
                        "There are multiple matching route-direction combinations. Asking user to choose one."
                        );
                    await AskUserToChooseOneRouteDirectionAsync(context, result.Matches, cancellationToken)
                    .ConfigureAwait(false);
                }
            }
            else
            {
                _logger.LogTrace(
                    "An error occured while trying to find the matching routes. Notifying the user via a message."
                    );
                await SendErrorMessageAsync(context, result.Error, cancellationToken)
                .ConfigureAwait(false);
            }
        }