Finish migration to MongoDb and general improvements

This commit is contained in:
2024-12-15 21:22:36 +03:30
parent 29011e34f9
commit dc38ce927f
13 changed files with 370 additions and 416 deletions

View File

@@ -12,30 +12,21 @@ public class MastodonBot
private static async Task Main()
{
// Configure logging
using var loggerFactory = LoggerFactory.Create(builder =>
{
builder.AddConsole();
});
using var loggerFactory = LoggerFactory.Create(builder => { builder.AddConsole(); });
var logger = loggerFactory.CreateLogger<MastodonBot>();
// Read environment variables
var config = ConfigData.fetchData();
if (config==null)
if (config == null)
{
logger.LogCritical("Error reading environment variables, either some values are missing or no .env file was found");
throw new Exception("Error reading environment variables, either some values are missing or no .env file was found");
logger.LogCritical(
"Error reading environment variables, either some values are missing or no .env file was found");
throw new Exception(
"Error reading environment variables, either some values are missing or no .env file was found");
}
// Setup DB
var backupDb = await DbInitializer.SetupJsonDb(config.DB_NAME);
if (backupDb == null)
{
logger.LogCritical("Unable to setup json DB");
throw new Exception("Unable to setup json DB");
}
var db = await DbInitializer.SetupDb(config.MONGODB_CONNECTION_STRING, config.DB_NAME);
logger.LogInformation("DB setup done");
@@ -55,40 +46,47 @@ public class MastodonBot
{
switch (update)
{
case { CallbackQuery: { } callbackQuery }: {
if(callbackQuery.Data == "new_random"){ await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger,callbackQuery); break;}
case { CallbackQuery: { } callbackQuery }:
{
if (callbackQuery.Data == "new_random")
{
await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger,
callbackQuery);
break;
}
else {await HandlePostAction.HandleCallbackQuery(callbackQuery, db, bot, logger); break;}
else
{
await HandlePostAction.HandleCallbackQuery(callbackQuery, db, bot, logger);
break;
}
}
default: logger.LogInformation($"Received unhandled update {update.Type}"); break;
};
}
;
}
// Handle bot messages
async Task OnMessage(Message message, UpdateType type)
{
if (message.Text == "/start" && message.Chat.Type == ChatType.Private)
{
await HandleStartMessage.HandleStartMessageAsync(message,bot, db, logger);
}
await HandleStartMessage.HandleStartMessageAsync(message, bot, db, logger);
else if (message.Text == "/backup")
{
await HandleDbBackup.HandleDbBackupAsync(bot, logger, config.DB_NAME, config.ADMIN_NUMID, backupDb, db);
}
await HandleDbBackup.HandleDbBackupAsync(bot, logger, config.DB_NAME, config.ADMIN_NUMID, db);
// Send a message to prompt user to send /start and recieve their cat photo only if its from a telegram user and not a channel
else if (message.Chat.Type == ChatType.Private)
{
await HandleStartMessage.HandleStartMessageAsync(message, bot, db, logger);
}
}
// Set a timer to fetch and process posts every 15 minutes
_postFetchTimer = new Timer(async _ => await RunCheck.runAsync(db, bot, config.TAG, logger, config.INSTANCE), null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
// Another timer to automatically backup the DB every 1 hour
_backupTimer = new Timer(async _ => await HandleDbBackup.HandleDbBackupAsync(bot, logger, config.DB_NAME, config.ADMIN_NUMID, backupDb,db), null, TimeSpan.Zero, TimeSpan.FromHours(6));
_postFetchTimer = new Timer(async _ => await RunCheck.runAsync(db, bot, config.TAG, logger, config.INSTANCE),
null, TimeSpan.Zero, TimeSpan.FromMinutes(10));
// Another timer to automatically backup the DB every 6 hour
_backupTimer =
new Timer(
async _ => await HandleDbBackup.HandleDbBackupAsync(bot, logger, config.DB_NAME, config.ADMIN_NUMID, db), null, TimeSpan.Zero, TimeSpan.FromHours(6));
// Keep the bot running
await Task.Delay(-1);
}
}