Migrate to Telegram.Bot v22 and some minor improvements
This commit is contained in:
@@ -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
|
||||
|
Reference in New Issue
Block a user