using System; using System.Threading; using xCommons.Providers; using xBotService.Models; using xModels.Extensions; using xCommons.Extensions; using System.Threading.Tasks; using xBotService.Interfaces; using xBotService.Models.Dtos; using System.Collections.Generic; using xBotService.Interfaces.Dtos; using Microsoft.Extensions.Logging; using Microsoft.Extensions.DependencyInjection; using xBotService.Constants; using xBotService.Extensions; namespace xBotService.Reciever { public class XBotBotUpdateRecieverBackgroundService : XTimeBasedBackgroundService { // public XBotBotUpdateRecieverBackgroundService( IServiceProvider serviceProvider, ILogger logger, int intervalInMinutes = 5 ) : base(serviceProvider, logger, intervalInMinutes) { } public override async Task DoWorkAsync( CancellationToken cancellationToken = default ) { // // Inject Provider ... using var scope = serviceProvider.CreateScope(); var botProvider = scope.ServiceProvider.GetRequiredService(); var botUserProvider = scope.ServiceProvider.GetRequiredService(); // // Check Provider Service Registered Successfully ... if (botProvider.IsNull() || botUserProvider.IsNull()) { // logger.LogInformation("xBotProvider Service not Provided, ignore ..."); return; } // // Try to Retrieve Updates ... IList updates = []; try { // updates = await botProvider.GetUpdates( requestModel: new(), cancellationToken: cancellationToken ); } catch (Exception ex) { // logger.LogError(ex, "Retrieve Updates failed ..."); return; } // // Check Updates Exists ... if (!updates.HasChild()) { // logger.LogInformation("there is not any new Updates ..."); return; } // // Log ... logger.LogInformation($"Updates: {Environment.NewLine}"); // // HandleRecieved Updates ... foreach (var update in updates) { // logger .LogInformation($"Handle Update: {Environment.NewLine} {update.ToJSON()}"); // // Check Item Exists in Db ... var item = await botUserProvider.FindOneAsync( predicate: x => x.BotUserId.Equals(update.Message.From.Id) ); if (item.IsNullOrDefault()) { // // Not Exists ... item = await botUserProvider.AddAsync( saveChanges: true, connectionId: null, item: new XBotUserDto { BotUserId = update.Message.From.Id, BotUserName = update.Message.From.Username, BotLastname = update.Message.From.LastName, BotFirstname = update.Message.From.FirstName, }, cancellationToken: cancellationToken ); // logger.LogInformation("Add new BotUser ..."); } else { // // Exists ... logger.LogInformation("BotUser exists ..."); } // // Check Verified ... if (item.IsVerified()) { // logger.LogInformation("User Verified ..."); continue; } // // TODO: Remove this ... if (item.BotUserName != "llhajamir") { continue; } // // Handle Callback Commands ... if (!update.CallbackQuery.IsNullOrDefault()) { // return; } // // Try to Show Verification Menu on User ... logger .LogInformation($"Show Verification Request Method to User: {item.User.GetFullname()}"); var response = await botProvider.ShowVerificationMenu( botUserId: item.BotUserId, cancellationToken: cancellationToken ); // logger.LogInformation($"Verification Menu Shown for User: {item.BotUserId}"); } } public async Task HandleBotAction(XBotUpdateResponse update) { // // Validate ... if (update.IsNullOrDefault() || update.CallbackQuery.IsNullOrDefault() || update.CallbackQuery.Data.IsNullOrEmpty()) { return; } // // Switch Betwen Data ... switch (update.CallbackQuery.Data) { // case XBotMessageIds.XBotRequestVerificationCode: { // // Remove Inline Keyboard ... // And Send Verification ... break; } } } } }