added /start message handling
This commit is contained in:
0
.gitea/workflows/gitea-ci.yml
Normal file → Executable file
0
.gitea/workflows/gitea-ci.yml
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
0
.gitignore
vendored
Normal file → Executable file
0
.gitlab-ci.yml
Normal file → Executable file
0
.gitlab-ci.yml
Normal file → Executable file
0
Models/Post.cs
Normal file → Executable file
0
Models/Post.cs
Normal file → Executable file
55
Program.cs
Normal file → Executable file
55
Program.cs
Normal file → Executable file
@@ -1,7 +1,9 @@
|
|||||||
using Microsoft.Extensions.Logging;
|
using CatsOfMastodonBot.Services;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using mstdnCats.Services;
|
using mstdnCats.Services;
|
||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Types;
|
using Telegram.Bot.Types;
|
||||||
|
using Telegram.Bot.Types.Enums;
|
||||||
|
|
||||||
public class MastodonBot
|
public class MastodonBot
|
||||||
{
|
{
|
||||||
@@ -19,7 +21,8 @@ public class MastodonBot
|
|||||||
});
|
});
|
||||||
var logger = loggerFactory.CreateLogger<MastodonBot>();
|
var logger = loggerFactory.CreateLogger<MastodonBot>();
|
||||||
|
|
||||||
if(!CheckEnv.IsValid()){
|
if (!CheckEnv.IsValid())
|
||||||
|
{
|
||||||
logger.LogCritical("Error reading envinonment variables, either some values are missing or no .env file was found");
|
logger.LogCritical("Error reading envinonment variables, either some values are missing or no .env file was found");
|
||||||
throw new Exception("Error reading envinonment variables, either some values are missing or no .env file was found");
|
throw new Exception("Error reading envinonment variables, either some values are missing or no .env file was found");
|
||||||
}
|
}
|
||||||
@@ -34,31 +37,41 @@ public class MastodonBot
|
|||||||
|
|
||||||
// Setup bot
|
// Setup bot
|
||||||
|
|
||||||
var bot = new TelegramBotClient(DotNetEnv.Env.GetString("BOT_TOKEN"));
|
var bot = new TelegramBotClient(DotNetEnv.Env.GetString("BOT_TOKEN"));
|
||||||
|
|
||||||
var me = await bot.GetMeAsync();
|
var me = await bot.GetMeAsync();
|
||||||
await bot.DropPendingUpdatesAsync();
|
await bot.DropPendingUpdatesAsync();
|
||||||
bot.OnUpdate += OnUpdate;
|
bot.OnUpdate += OnUpdate;
|
||||||
|
bot.OnMessage += OnMessage;
|
||||||
|
|
||||||
logger.LogInformation($"Bot is running as {me.FirstName}.");
|
logger.LogInformation($"Bot is running as {me.FirstName}.");
|
||||||
|
|
||||||
|
// Handle bot updates
|
||||||
|
async Task OnUpdate(Update update)
|
||||||
// Handle bot updates
|
{
|
||||||
async Task OnUpdate(Update update)
|
switch (update)
|
||||||
{
|
{
|
||||||
switch (update)
|
case { CallbackQuery: { } callbackQuery }: {
|
||||||
{
|
if(callbackQuery.Data == "new_random") await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger);
|
||||||
case { CallbackQuery: { } callbackQuery }: 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;
|
|
||||||
};
|
}
|
||||||
|
default: logger.LogInformation($"Received unhandled update {update.Type}"); break;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle bot messages
|
||||||
|
async Task OnMessage(Message message, UpdateType type)
|
||||||
|
{
|
||||||
|
if (message.Text == "/start")
|
||||||
|
{
|
||||||
|
await HandleStartMessage.HandleStartMessageAsync(message,bot, db, logger);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Set a timer to fetch and process posts every 15 minutes
|
// Set a timer to fetch and process posts every 15 minutes
|
||||||
_timer = new Timer(async _ => await RunCheck.runAsync(db, bot, DotNetEnv.Env.GetString("TAG"), logger, DotNetEnv.Env.GetString("CUSTOM_INSTANCE")), null, TimeSpan.Zero, TimeSpan.FromMinutes(15));
|
_timer = new Timer(async _ => await RunCheck.runAsync(db, bot, DotNetEnv.Env.GetString("TAG"), logger, DotNetEnv.Env.GetString("CUSTOM_INSTANCE")), null, TimeSpan.Zero, TimeSpan.FromMinutes(15));
|
||||||
Console.ReadLine();
|
Console.ReadLine();
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
0
Services/CheckEnv.cs
Normal file → Executable file
0
Services/CheckEnv.cs
Normal file → Executable file
0
Services/DbInitializer.cs
Normal file → Executable file
0
Services/DbInitializer.cs
Normal file → Executable file
0
Services/HandlePostAction.cs
Normal file → Executable file
0
Services/HandlePostAction.cs
Normal file → Executable file
34
Services/HandleStartMessage.cs
Executable file
34
Services/HandleStartMessage.cs
Executable file
@@ -0,0 +1,34 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using JsonFlatFileDataStore;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using mstdnCats.Models;
|
||||||
|
using Telegram.Bot;
|
||||||
|
using Telegram.Bot.Types;
|
||||||
|
using Telegram.Bot.Types.Enums;
|
||||||
|
using Telegram.Bot.Types.ReplyMarkups;
|
||||||
|
|
||||||
|
namespace CatsOfMastodonBot.Services
|
||||||
|
{
|
||||||
|
public class HandleStartMessage
|
||||||
|
{
|
||||||
|
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection<Post> _db, ILogger<MastodonBot>? logger)
|
||||||
|
{
|
||||||
|
// choose all media attachments that are approved
|
||||||
|
var mediaAttachmentsToSelect = _db.AsQueryable().Where(p => p.MediaAttachments.Any(m => m.Approved == true)).ToList();
|
||||||
|
// select random approved media attachment
|
||||||
|
var selectedMediaAttachment = mediaAttachmentsToSelect[new Random().Next(mediaAttachmentsToSelect.Count)];
|
||||||
|
// send media attachment
|
||||||
|
await _bot.SendPhotoAsync(message.Chat.Id, selectedMediaAttachment.MediaAttachments.FirstOrDefault(m => m.Approved == true).Url,
|
||||||
|
caption: $"<a href=\"" + selectedMediaAttachment.Url + "\">" + $"Here is your cat!" + "</br>" + "View on Mastodon 🐈" + " </a>", parseMode: ParseMode.Html
|
||||||
|
, replyMarkup: new InlineKeyboardMarkup().AddButton(InlineKeyboardButton.WithUrl("Join channel 😺", selectedMediaAttachment.Url))
|
||||||
|
.AddNewRow()
|
||||||
|
.AddButton(InlineKeyboardButton.WithCallbackData("Send me another one!", $"new_random")));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
0
Services/PostResolver.cs
Normal file → Executable file
0
Services/PostResolver.cs
Normal file → Executable file
0
Services/ProcessPosts.cs
Normal file → Executable file
0
Services/ProcessPosts.cs
Normal file → Executable file
0
Services/RunCheck.cs
Normal file → Executable file
0
Services/RunCheck.cs
Normal file → Executable file
0
mstdnCats.csproj
Normal file → Executable file
0
mstdnCats.csproj
Normal file → Executable file
Reference in New Issue
Block a user