Migrate to Telegram.Bot v22 and some minor improvements
This commit is contained in:
13
.idea/.idea.CatsOfMastodonBot.dir/.idea/.gitignore
generated
vendored
Executable file
13
.idea/.idea.CatsOfMastodonBot.dir/.idea/.gitignore
generated
vendored
Executable file
@@ -0,0 +1,13 @@
|
||||
# Default ignored files
|
||||
/shelf/
|
||||
/workspace.xml
|
||||
# Rider ignored files
|
||||
/modules.xml
|
||||
/projectSettingsUpdater.xml
|
||||
/.idea.CatsOfMastodonBot.iml
|
||||
/contentModel.xml
|
||||
# Editor-based HTTP Client requests
|
||||
/httpRequests/
|
||||
# Datasource local storage ignored files
|
||||
/dataSources/
|
||||
/dataSources.local.xml
|
8
.idea/.idea.CatsOfMastodonBot.dir/.idea/indexLayout.xml
generated
Executable file
8
.idea/.idea.CatsOfMastodonBot.dir/.idea/indexLayout.xml
generated
Executable file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="UserContentModel">
|
||||
<attachedFolders />
|
||||
<explicitIncludes />
|
||||
<explicitExcludes />
|
||||
</component>
|
||||
</project>
|
6
.idea/.idea.CatsOfMastodonBot.dir/.idea/vcs.xml
generated
Executable file
6
.idea/.idea.CatsOfMastodonBot.dir/.idea/vcs.xml
generated
Executable file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
@@ -14,7 +14,7 @@ namespace CatsOfMastodonBot.Models
|
||||
string tag = DotNetEnv.Env.GetString("TAG") ?? Environment.GetEnvironmentVariable("TAG");
|
||||
string channelNumId = DotNetEnv.Env.GetString("CHANNEL_NUMID") ?? Environment.GetEnvironmentVariable("CHANNEL_NUMID");
|
||||
string adminNumId = DotNetEnv.Env.GetString("ADMIN_NUMID") ?? Environment.GetEnvironmentVariable("ADMIN_NUMID");
|
||||
string instance = DotNetEnv.Env.GetString("INSTANCE") ?? Environment.GetEnvironmentVariable("INSTANCE");
|
||||
string? instance = DotNetEnv.Env.GetString("INSTANCE") ?? Environment.GetEnvironmentVariable("INSTANCE");
|
||||
|
||||
// Check if any of the values are still null or empty
|
||||
if (string.IsNullOrEmpty(dbName) || string.IsNullOrEmpty(botToken) || string.IsNullOrEmpty(tag)
|
||||
|
@@ -42,8 +42,8 @@ public class MastodonBot
|
||||
|
||||
var bot = new TelegramBotClient(config.BOT_TOKEN);
|
||||
|
||||
var me = await bot.GetMeAsync();
|
||||
await bot.DropPendingUpdatesAsync();
|
||||
var me = await bot.GetMe();
|
||||
await bot.DropPendingUpdates();
|
||||
bot.OnMessage += OnMessage;
|
||||
bot.OnUpdate += OnUpdate;
|
||||
|
||||
@@ -56,7 +56,7 @@ public class MastodonBot
|
||||
switch (update)
|
||||
{
|
||||
case { CallbackQuery: { } callbackQuery }: {
|
||||
if(callbackQuery.Data == "new_random"){ await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger); break;}
|
||||
if(callbackQuery.Data == "new_random"){ await HandleStartMessage.HandleStartMessageAsync(callbackQuery.Message, bot, db, logger,callbackQuery); break;}
|
||||
|
||||
else {await HandlePostAction.HandleCallbackQuery(callbackQuery, db, bot, logger); break;}
|
||||
|
||||
@@ -79,7 +79,7 @@ public class MastodonBot
|
||||
else
|
||||
{
|
||||
// Send a help message to prompt user to send /start and recieve their cat photo
|
||||
await bot.SendTextMessageAsync(message.Chat.Id, "Send /start to get a random cat!");
|
||||
await bot.SendMessage(message.Chat.Id, "Send /start to get a random cat!");
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -9,12 +9,11 @@ namespace mstdnCats.Services
|
||||
{
|
||||
// Setup DB
|
||||
IDocumentCollection<Post>? collection = null;
|
||||
if (_dbname != null)
|
||||
{
|
||||
|
||||
try
|
||||
{
|
||||
// Initialize DB
|
||||
var store = new DataStore($"{_dbname}.json", minifyJson: false);
|
||||
var store = new DataStore($"{_dbname}.json", minifyJson: true);
|
||||
collection = store.GetCollection<Post>();
|
||||
}
|
||||
catch
|
||||
@@ -24,8 +23,6 @@ namespace mstdnCats.Services
|
||||
|
||||
// Return collection
|
||||
return Task.FromResult(collection);
|
||||
}
|
||||
return Task.FromResult(collection);
|
||||
}
|
||||
}
|
||||
}
|
@@ -14,7 +14,7 @@ public class HandleDbBackup
|
||||
logger?.LogInformation("Backup requested");
|
||||
|
||||
await using Stream stream = System.IO.File.OpenRead("./" + dbname+".json");
|
||||
var message = await _bot.SendDocumentAsync(adminId, document: InputFile.FromStream(stream, dbname+".json"),
|
||||
var message = await _bot.SendDocument(adminId, document: InputFile.FromStream(stream, dbname+".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);
|
||||
|
||||
logger?.LogInformation("Backup sent");
|
||||
|
@@ -43,7 +43,7 @@ namespace mstdnCats.Services
|
||||
// Check if the media attachment is already approved
|
||||
bool isAlreadyApproved = mediaAttachment.Approved;
|
||||
if (isAlreadyApproved){
|
||||
await _bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Media attachment is already approved.",true);
|
||||
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment is already approved.",true);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -55,11 +55,11 @@ namespace mstdnCats.Services
|
||||
if (updated)
|
||||
{
|
||||
// Send the media attachment to the channel
|
||||
await _bot.SendPhotoAsync(config.CHANNEL_NUMID, post.MediaAttachments.First().Url, caption: $"Post from " + $"<a href=\"" + post.Account.Url + "\">" + post.Account.DisplayName + " </a>", parseMode: ParseMode.Html
|
||||
await _bot.SendPhoto(config.CHANNEL_NUMID, post.MediaAttachments.First().Url, caption: $"Post from " + $"<a href=\"" + post.Account.Url + "\">" + post.Account.DisplayName + " </a>", parseMode: ParseMode.Html
|
||||
, replyMarkup: new InlineKeyboardMarkup(InlineKeyboardButton.WithUrl("View on Mastodon", post.Url)));
|
||||
|
||||
await _bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Media attachment approved and sent to channel.");
|
||||
await _bot.DeleteMessageAsync(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);
|
||||
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment approved and sent to channel.");
|
||||
await _bot.DeleteMessage(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);
|
||||
|
||||
logger?.LogTrace($"Media attachment {mediaId} approved.");
|
||||
|
||||
@@ -83,8 +83,8 @@ namespace mstdnCats.Services
|
||||
// Check if the post has only one attachment, if so, do not delete it, else delete the associated attachment
|
||||
if (post.MediaAttachments.Count == 1 && post.MediaAttachments.First().MediaId == mediaId)
|
||||
{
|
||||
await _bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Post has only one attachment. No deletion performed.");
|
||||
await _bot.DeleteMessageAsync(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);
|
||||
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Post has only one attachment. No deletion performed.");
|
||||
await _bot.DeleteMessage(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);
|
||||
|
||||
logger?.LogTrace($"Post {post.mstdnPostId} has only one attachment. No deletion performed.");
|
||||
}
|
||||
@@ -92,8 +92,8 @@ namespace mstdnCats.Services
|
||||
{
|
||||
post.MediaAttachments.RemoveAll(m => m.MediaId == mediaId);
|
||||
await _db.UpdateOneAsync(p => p.mstdnPostId == post.mstdnPostId, post);
|
||||
await _bot.AnswerCallbackQueryAsync(callbackQuery.Id, "Media attachment rejected.");
|
||||
await _bot.DeleteMessageAsync(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);
|
||||
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Media attachment rejected.");
|
||||
await _bot.DeleteMessage(callbackQuery.Message.Chat.Id, callbackQuery.Message.MessageId);
|
||||
|
||||
logger?.LogTrace($"Media attachment {mediaId} removed from post {post.mstdnPostId}.");
|
||||
}
|
||||
|
@@ -1,8 +1,3 @@
|
||||
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;
|
||||
@@ -15,10 +10,10 @@ namespace CatsOfMastodonBot.Services
|
||||
{
|
||||
public class HandleStartMessage
|
||||
{
|
||||
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection<Post> _db, ILogger<MastodonBot>? logger)
|
||||
public static async Task HandleStartMessageAsync(Message message, TelegramBotClient _bot, IDocumentCollection<Post> _db, ILogger<MastodonBot>? logger, CallbackQuery callbackQuery = null)
|
||||
{
|
||||
logger?.LogInformation("Start message received");
|
||||
|
||||
|
||||
// choose all media attachments that are approved
|
||||
var mediaAttachmentsToSelect = _db.AsQueryable()
|
||||
.Where(post => post.MediaAttachments.Any(media => media.Approved))
|
||||
@@ -26,12 +21,15 @@ namespace CatsOfMastodonBot.Services
|
||||
// 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: $"Here is your cat!🐈\n"+"<a href=\"" + selectedMediaAttachment.Url + "\">" + $"View on Mastodon " + " </a>", parseMode: ParseMode.Html
|
||||
await _bot.SendPhoto(callbackQuery.Message.Chat.Id, selectedMediaAttachment.MediaAttachments.FirstOrDefault(m => m.Approved == true).Url,
|
||||
caption: $"Here is your cat!🐈\n" + "<a href=\"" + selectedMediaAttachment.Url + "\">" + $"View on Mastodon " + " </a>", parseMode: ParseMode.Html
|
||||
, replyMarkup: new InlineKeyboardMarkup().AddButton(InlineKeyboardButton.WithUrl("Join channel 😺", "https://t.me/catsofmastodon"))
|
||||
.AddNewRow()
|
||||
.AddButton(InlineKeyboardButton.WithCallbackData("Send me another one!", $"new_random")));
|
||||
|
||||
if (callbackQuery != null)
|
||||
{
|
||||
await _bot.AnswerCallbackQuery(callbackQuery.Id, "Catch your cat!", url: selectedMediaAttachment.Url);
|
||||
}
|
||||
logger?.LogInformation("Random cat sent!");
|
||||
|
||||
|
||||
|
@@ -32,7 +32,7 @@ namespace mstdnCats.Services
|
||||
{
|
||||
try
|
||||
{
|
||||
await _bot.SendPhotoAsync(config.ADMIN_NUMID, media.PreviewUrl, caption: $"<a href=\"" + post.Url + "\"> Mastodon </a>", parseMode: ParseMode.Html
|
||||
await _bot.SendPhoto(config.ADMIN_NUMID, media.PreviewUrl, caption: $"<a href=\"" + post.Url + "\"> Mastodon </a>", parseMode: ParseMode.Html
|
||||
, replyMarkup: new InlineKeyboardMarkup().AddButton("Approve", $"approve-{media.MediaId}").AddButton("Reject", $"reject-{media.MediaId}"));
|
||||
|
||||
// Insert post
|
||||
|
@@ -11,7 +11,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="DotNetEnv" Version="3.1.1" />
|
||||
<PackageReference Include="JsonFlatFileDataStore" Version="2.4.2" />
|
||||
<PackageReference Include="Telegram.Bot" Version="21.11.0" />
|
||||
<PackageReference Include="Telegram.Bot" Version="22.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
Reference in New Issue
Block a user