Exemple #1
0
 /// <inheritdoc />
 public Task <bool> IsEmptyAsync(
     CancellationToken cancellationToken = default
     ) =>
 _agencyRepo
 .GetByCountryAsync("Canada", cancellationToken)
 .ContinueWith(
     t => t.Result.Length == 0,
     TaskContinuationOptions.OnlyOnRanToCompletion
     );
Exemple #2
0
        public async Task HandleAsync(IUpdateContext context, UpdateDelegate next, CancellationToken cancellationToken)
        {
            string queryData = context.Update.CallbackQuery.Data;

            // ToDo if user already has profile set, ignore this. check if "instructions sent" is set in cache
            // if (await _userContextManager.TryReplyIfOldSetupInstructionMessageAsync(bot, update)) return;

            if (queryData.StartsWith("ups/a:"))
            {
                _logger.LogTrace("Setting the agency for user");

                var userchat = context.Update.ToUserchat();

                string agencyTag = queryData.Substring("ups/a:".Length);
                Agency agency    = await _agencyRepo.GetByTagAsync(agencyTag, cancellationToken)
                                   .ConfigureAwait(false);

                var error = await _userService.SetDefaultAgencyAsync(
                    userchat.UserId.ToString(),
                    userchat.ChatId.ToString(),
                    agency.Tag,
                    cancellationToken
                    ).ConfigureAwait(false);

                if (error is null)
                {
                    _logger.LogTrace("Updating the cache and removing the instructions.");

                    var profileContext = await _cache.GetProfileAsync(userchat, cancellationToken)
                                         .ConfigureAwait(false);

                    profileContext.IsInstructionsSent = false;
                    await _cache.SetProfileAsync(userchat, profileContext, cancellationToken)
                    .ConfigureAwait(false);

                    await context.Bot.Client.SendTextMessageAsync(
                        context.Update.CallbackQuery.Message.Chat,
                        $"Great! Your default agency is now set to *{agency.Title}* " +
                        $"in {agency.Region}, {agency.Country}.\n\n\n" +
                        "💡 *Pro Tip*: You can always view or modify it using the /profile command.",
                        ParseMode.Markdown,
                        replyMarkup : new ReplyKeyboardRemove(),
                        cancellationToken : cancellationToken
                        );


                    context.Items[nameof(WebhookResponse)] = new EditMessageReplyMarkupRequest(
                        context.Update.CallbackQuery.Message.Chat,
                        context.Update.CallbackQuery.Message.MessageId
                        );
                }
                else
                {
                    context.Items[nameof(WebhookResponse)] = new AnswerCallbackQueryRequest
                                                                 (context.Update.CallbackQuery.Id)
                    {
                        Text      = "Oops! failed to set the agency. Try again later",
                        CacheTime = 10,
                    };
                }
            }
            else
            {
                _logger.LogTrace("Updating the inline keyboard of the profile setup message");

                InlineKeyboardMarkup inlineKeyboard;

                if (queryData.StartsWith("ups/c:"))
                {
                    _logger.LogTrace("Update the menu with unique regions for a country");
                    string country  = queryData.Substring("ups/c:".Length);
                    var    agencies = await _agencyRepo.GetByCountryAsync(country)
                                      .ConfigureAwait(false);

                    string[] regions = agencies
                                       .Select(a => a.Region)
                                       .Distinct(StringComparer.OrdinalIgnoreCase)
                                       .OrderBy(r => r)
                                       .ToArray();

                    inlineKeyboard = CreateRegionsInlineKeyboard(regions);
                }
                else if (queryData.StartsWith("ups/r:"))
                {
                    _logger.LogTrace("Updating the menu with all agencies in the region");
                    string region   = queryData.Substring("ups/r:".Length);
                    var    agencies = await _agencyRepo.GetByRegionAsync(region)
                                      .ConfigureAwait(false);

                    inlineKeyboard = CreateAgenciesInlineKeyboard(agencies);
                }
                else if (queryData == "ups/c")
                {
                    _logger.LogTrace("Updating the menu with the list of countries");
                    inlineKeyboard = CreateCountriesInlineKeyboard();
                }
                else
                {
                    _logger.LogError("Invalid callback query data {0} is passed", queryData);
                    return;
                }

                await context.Bot.Client.EditMessageReplyMarkupAsync(
                    context.Update.CallbackQuery.Message.Chat,
                    context.Update.CallbackQuery.Message.MessageId,
                    inlineKeyboard
                    ).ConfigureAwait(false);

                context.Items[nameof(WebhookResponse)] =
                    new AnswerCallbackQueryRequest(context.Update.CallbackQuery.Id);
            }
        }