Stage 1 of migrating to MongoDb - fully added mongodb
This commit is contained in:
11
Program.cs
11
Program.cs
@@ -21,6 +21,7 @@ public class MastodonBot
|
|||||||
});
|
});
|
||||||
var logger = loggerFactory.CreateLogger<MastodonBot>();
|
var logger = loggerFactory.CreateLogger<MastodonBot>();
|
||||||
|
|
||||||
|
// Read environment variables
|
||||||
var config = ConfigData.fetchData();
|
var config = ConfigData.fetchData();
|
||||||
if (config==null)
|
if (config==null)
|
||||||
{
|
{
|
||||||
@@ -29,7 +30,6 @@ public class MastodonBot
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Setup DB
|
// Setup DB
|
||||||
Console.WriteLine("DB name: " + config.DB_NAME);
|
|
||||||
var backupDb = await DbInitializer.SetupJsonDb(config.DB_NAME);
|
var backupDb = await DbInitializer.SetupJsonDb(config.DB_NAME);
|
||||||
if (backupDb == null)
|
if (backupDb == null)
|
||||||
{
|
{
|
||||||
@@ -40,7 +40,6 @@ public class MastodonBot
|
|||||||
logger.LogInformation("DB setup done");
|
logger.LogInformation("DB setup done");
|
||||||
|
|
||||||
// Setup bot
|
// Setup bot
|
||||||
|
|
||||||
var bot = new TelegramBotClient(config.BOT_TOKEN);
|
var bot = new TelegramBotClient(config.BOT_TOKEN);
|
||||||
|
|
||||||
var me = await bot.GetMe();
|
var me = await bot.GetMe();
|
||||||
@@ -57,7 +56,7 @@ public class MastodonBot
|
|||||||
switch (update)
|
switch (update)
|
||||||
{
|
{
|
||||||
case { CallbackQuery: { } callbackQuery }: {
|
case { CallbackQuery: { } callbackQuery }: {
|
||||||
if(callbackQuery.Data == "new_random"){ await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, backupDb, logger,callbackQuery); break;}
|
if(callbackQuery.Data == "new_random"){ await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger,callbackQuery); break;}
|
||||||
|
|
||||||
else {await HandlePostAction.HandleCallbackQuery(callbackQuery, backupDb, bot, logger); break;}
|
else {await HandlePostAction.HandleCallbackQuery(callbackQuery, backupDb, bot, logger); break;}
|
||||||
|
|
||||||
@@ -69,9 +68,9 @@ public class MastodonBot
|
|||||||
// Handle bot messages
|
// Handle bot messages
|
||||||
async Task OnMessage(Message message, UpdateType type)
|
async Task OnMessage(Message message, UpdateType type)
|
||||||
{
|
{
|
||||||
if (message.Text == "/start")
|
if (message.Text == "/start" && message.Chat.Type == ChatType.Private)
|
||||||
{
|
{
|
||||||
await HandleStartMessage.HandleStartMessageAsync(message,bot, backupDb, logger);
|
await HandleStartMessage.HandleStartMessageAsync(message,bot, db, logger);
|
||||||
}
|
}
|
||||||
else if (message.Text == "/backup")
|
else if (message.Text == "/backup")
|
||||||
{
|
{
|
||||||
@@ -80,7 +79,7 @@ public class MastodonBot
|
|||||||
// 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
|
// 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)
|
else if (message.Chat.Type == ChatType.Private)
|
||||||
{
|
{
|
||||||
await HandleStartMessage.HandleStartMessageAsync(message, bot, backupDb, logger);
|
await HandleStartMessage.HandleStartMessageAsync(message, bot, db, logger);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -14,7 +14,7 @@ namespace mstdnCats.Services
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
// Initialize Backup DB
|
// Initialize Backup DB
|
||||||
var store = new DataStore($"{_dbname + "_BK"}.json", minifyJson: true);
|
var store = new DataStore($"./data/{_dbname + "_BK"}.json", minifyJson: true);
|
||||||
collection = store.GetCollection<Post>();
|
collection = store.GetCollection<Post>();
|
||||||
}
|
}
|
||||||
catch
|
catch
|
||||||
@@ -26,7 +26,7 @@ namespace mstdnCats.Services
|
|||||||
return Task.FromResult(collection);
|
return Task.FromResult(collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Task<IMongoDatabase> SetupDb(string mongoDbConnectionString, string dbName)
|
public static Task<IMongoCollection<Post>> SetupDb(string mongoDbConnectionString, string dbName)
|
||||||
{
|
{
|
||||||
if (mongoDbConnectionString == null)
|
if (mongoDbConnectionString == null)
|
||||||
{
|
{
|
||||||
@@ -36,7 +36,7 @@ namespace mstdnCats.Services
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var client = new MongoClient(mongoDbConnectionString);
|
var client = new MongoClient(mongoDbConnectionString);
|
||||||
var database = client.GetDatabase(dbName);
|
var database = client.GetDatabase(dbName).GetCollection<Post>("posts");
|
||||||
return Task.FromResult(database);
|
return Task.FromResult(database);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
|
@@ -13,8 +13,8 @@ public class HandleDbBackup
|
|||||||
{
|
{
|
||||||
logger?.LogInformation("Backup requested");
|
logger?.LogInformation("Backup requested");
|
||||||
|
|
||||||
await using Stream stream = System.IO.File.OpenRead("./" + dbname+".json");
|
await using Stream stream = System.IO.File.OpenRead("./data/" + dbname+"_BK.json");
|
||||||
var message = await _bot.SendDocument(adminId, document: InputFile.FromStream(stream, dbname+".json"),
|
var message = await _bot.SendDocument(adminId, document: InputFile.FromStream(stream, dbname+"_BK.json"),
|
||||||
caption: "Backup of " + dbname + "\nCreated at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + "\nCurrent post count: " + _db.AsQueryable().Count()), parseMode: ParseMode.Html);
|
caption: "Backup of " + dbname + "\nCreated at " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss" + "\nCurrent post count: " + _db.AsQueryable().Count()), parseMode: ParseMode.Html);
|
||||||
|
|
||||||
logger?.LogInformation("Backup sent");
|
logger?.LogInformation("Backup sent");
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
using JsonFlatFileDataStore;
|
using JsonFlatFileDataStore;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using MongoDB.Driver;
|
||||||
using mstdnCats.Models;
|
using mstdnCats.Models;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
@@ -10,7 +11,7 @@ namespace CatsOfMastodonBot.Services
|
|||||||
{
|
{
|
||||||
public class HandleStartMessage
|
public class HandleStartMessage
|
||||||
{
|
{
|
||||||
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection<Post> _db, ILogger<MastodonBot>? logger, CallbackQuery callbackQuery = null)
|
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IMongoCollection<Post> _db, ILogger<MastodonBot>? logger, CallbackQuery callbackQuery = null)
|
||||||
{
|
{
|
||||||
logger?.LogInformation("Start message received, trigger source: " + (callbackQuery != null ? "Callback" : "Start command"));
|
logger?.LogInformation("Start message received, trigger source: " + (callbackQuery != null ? "Callback" : "Start command"));
|
||||||
|
|
||||||
|
@@ -12,7 +12,7 @@
|
|||||||
<PackageReference Include="DotNetEnv" Version="3.1.1" />
|
<PackageReference Include="DotNetEnv" Version="3.1.1" />
|
||||||
<PackageReference Include="JsonFlatFileDataStore" Version="2.4.2" />
|
<PackageReference Include="JsonFlatFileDataStore" Version="2.4.2" />
|
||||||
<PackageReference Include="MongoDB.Driver" Version="3.1.0" />
|
<PackageReference Include="MongoDB.Driver" Version="3.1.0" />
|
||||||
<PackageReference Include="Telegram.Bot" Version="22.0.2" />
|
<PackageReference Include="Telegram.Bot" Version="22.2.0" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
Reference in New Issue
Block a user