Skip to content

Commit

Permalink
fix telegram setup
Browse files Browse the repository at this point in the history
  • Loading branch information
azhe403 committed Oct 21, 2024
1 parent 68e00ce commit 3b9c6ba
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 65 deletions.
5 changes: 4 additions & 1 deletion backend/ZiziBot.Console/Partials/WebComponentBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.ComponentModel;
using Microsoft.AspNetCore.Components.Authorization;
using ZiziBot.DataSource.MongoEf;

namespace ZiziBot.Console.Partials;

Expand All @@ -20,6 +21,9 @@ public class WebComponentBase<T> : ReactiveInjectableComponentBase<T> where T :
[Inject]
public required MongoDbContextBase MongoDbContextBase { get; set; }

[Inject]
public required MongoEfContext MongoEfContext { get; set; }

[Inject]
public required ILogger<T> Logger { get; set; }

Expand All @@ -31,5 +35,4 @@ public class WebComponentBase<T> : ReactiveInjectableComponentBase<T> where T :

[Inject]
protected NotificationService NotificationService { get; set; }

}
4 changes: 1 addition & 3 deletions backend/ZiziBot.Engine/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Serilog;
using ZiziBot.TelegramBot;
using ZiziBot.TelegramBot.Framework.Extensions;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -35,7 +34,6 @@
app.ConfigureConsole();
app.ConfigureApi();
app.UseHangfire();
await app.UseZiziBotTelegramBot();
// await app.RunTelegramBot();
await app.RunTelegramBot();

await app.RunAsync();
2 changes: 0 additions & 2 deletions backend/ZiziBot.TelegramBot/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,4 @@
global using ZiziBot.Contracts.Configs;
global using ZiziBot.Contracts.Constants;
global using ZiziBot.Contracts.Enums;
global using ZiziBot.DataSource.MongoDb;
global using ZiziBot.DataSource.MongoDb.Entities;
global using ZiziBot.Utils;
97 changes: 38 additions & 59 deletions backend/ZiziBot.TelegramBot/TelegramExtension.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using ZiziBot.DataSource.MongoEf;
using ZiziBot.DataSource.MongoEf.Entities;
using ZiziBot.TelegramBot.Framework.Extensions;
using ZiziBot.TelegramBot.Framework.Models.Configs;

Expand All @@ -13,33 +16,10 @@ public static IServiceCollection ConfigureTelegramBot(this IServiceCollection se
var provider = services.BuildServiceProvider();
var environment = provider.GetRequiredService<IWebHostEnvironment>();
var config = provider.GetRequiredService<IOptions<EngineConfig>>().Value;
var appSettingDbContext = provider.GetRequiredService<MongoDbContextBase>();
// var listBotOptions = provider.GetRequiredService<IOptions<List<SimpleTelegramBotClientOptions>>>().Value;


// services.AddTelegramControllers();
//
// switch (config.TelegramEngineMode)
// {
// case BotEngineMode.Webhook:
// services.AddTelegramWebHookManager();
// break;
//
// case BotEngineMode.Polling:
// services.AddTelegramManager();
// break;
//
// default: // or EngineMode.Auto
// if (environment.IsDevelopment())
// services.AddTelegramManager();
// else
// services.AddTelegramWebHookManager();
//
// break;
// }
var appSettingDbContext = provider.GetRequiredService<MongoEfContext>();

var listBotData = appSettingDbContext.BotSettings
.Where(settings => settings.Status == (int)EventStatus.Complete)
.Where(settings => settings.Status == EventStatus.Complete)
.ToList();

services.AddZiziBotTelegramBot(new BotEngineConfig() {
Expand All @@ -54,38 +34,37 @@ public static IServiceCollection ConfigureTelegramBot(this IServiceCollection se
return services;
}

// public async static Task<IApplicationBuilder> RunTelegramBot(this IApplicationBuilder app)
// {
// var provider = app.ApplicationServices;
// var appSettingsDbContext = provider.GetRequiredService<MongoDbContextBase>();
//
// var listBotOptions = provider.GetRequiredService<IOptions<List<SimpleTelegramBotClientOptions>>>().Value;
//
// if (!listBotOptions.Any())
// {
// appSettingsDbContext.BotSettings.Add(new BotSettingsEntity() {
// Name = "BOT_NAME",
// Token = "BOT_TOKEN",
// Status = (int)EventStatus.InProgress
// });
//
// await appSettingsDbContext.SaveChangesAsync();
//
// throw new ApplicationException("No bot data found. Please ensure config for 'BotSettings'");
// }
//
// var validBot = listBotOptions
// .Where(x => x.Name != "BOT_NAME")
// .ToList();
//
// if (!validBot.Any())
// {
// throw new ApplicationException("Please add your Bot to 'BotSettings'");
// }
//
// var telegramManager = provider.GetRequiredService<ITelegramManager>();
// await telegramManager.Start(validBot.Select(options => TelegramBotClientFactory.CreateClient(options)));
//
// return app;
// }
public async static Task<IApplicationBuilder> RunTelegramBot(this IApplicationBuilder app)
{
var provider = app.ApplicationServices;
var appSettingsDbContext = provider.GetRequiredService<MongoEfContext>();

var listBotOptions = provider.GetRequiredService<List<BotTokenConfig>>();

if (listBotOptions.Count == 0)
{
appSettingsDbContext.BotSettings.Add(new BotSettingsEntity() {
Name = "BOT_NAME",
Token = "BOT_TOKEN",
Status = EventStatus.InProgress
});

await appSettingsDbContext.SaveChangesAsync();

throw new ApplicationException("No bot data found. Please ensure config for 'BotSettings'");
}

var validBot = listBotOptions
.Where(x => x.Name != "BOT_NAME")
.ToList();

if (validBot.Count == 0)
{
throw new ApplicationException("Please add your Bot to 'BotSettings'");
}

await app.UseZiziBotTelegramBot();

return app;
}
}

0 comments on commit 3b9c6ba

Please sign in to comment.