Added authorization/security and option to send rendered maps to Telegram via Telegram Bridge chat bot.

This commit is contained in:
Milutinke 2022-10-24 18:39:07 +02:00
parent 1272ffda0b
commit 3c6de23d61
3 changed files with 139 additions and 34 deletions

View file

@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using ImageMagick;
using MinecraftClient.Mapping;
using Tomlet.Attributes;
@ -13,7 +15,7 @@ namespace MinecraftClient.ChatBots
{
public static Configs Config = new();
public struct DiscordMap
public struct QueuedMap
{
public string FileName;
public int MapId;
@ -48,8 +50,9 @@ namespace MinecraftClient.ChatBots
[TomlInlineComment("$config.ChatBot.Map.Resize_To$")]
public int Resize_To = 512;
[TomlPrecedingComment("$config.ChatBot.Map.Send_Rendered_To_Discord$")]
[TomlPrecedingComment("$config.ChatBot.Map.Send_Rendered_To_Bridges$")]
public bool Send_Rendered_To_Discord = false;
public bool Send_Rendered_To_Telegram = false;
public void OnSettingUpdate()
{
@ -62,7 +65,7 @@ namespace MinecraftClient.ChatBots
private readonly Dictionary<int, McMap> cachedMaps = new();
private readonly Queue<DiscordMap> discordQueue = new();
private readonly Queue<QueuedMap> discordQueue = new();
public override void Initialize()
{
@ -244,45 +247,75 @@ namespace MinecraftClient.ChatBots
}
}
if (Config.Send_Rendered_To_Discord)
if (Config.Send_Rendered_To_Discord || Config.Send_Rendered_To_Telegram)
{
// We need to queue up images because Discord Bridge is not ready immediatelly
if (DiscordBridge.Config.Enabled)
discordQueue.Enqueue(new DiscordMap { FileName = fileName, MapId = map.MapId });
// We need to queue up images because Discord/Telegram Bridge is not ready immediatelly
if (DiscordBridge.Config.Enabled || TelegramBridge.Config.Enabled)
discordQueue.Enqueue(new QueuedMap { FileName = fileName, MapId = map.MapId });
}
}
public override void Update()
{
if (!DiscordBridge.Config.Enabled)
return;
DiscordBridge? discordBridge = DiscordBridge.GetInstance();
TelegramBridge? telegramBridge = TelegramBridge.GetInstance();
if (discordBridge == null)
return;
if (Config.Send_Rendered_To_Discord)
{
if (discordBridge == null || (discordBridge != null && !discordBridge.IsConnected))
return;
}
if (!discordBridge.IsConnected)
return;
if (Config.Send_Rendered_To_Telegram)
{
if (telegramBridge == null || (telegramBridge != null && !telegramBridge.IsConnected))
return;
}
if (discordQueue.Count > 0)
{
DiscordMap discordMap = discordQueue.Dequeue();
string fileName = discordMap.FileName;
QueuedMap map = discordQueue.Dequeue();
string fileName = map.FileName;
// We must convert to a PNG in order to send to Discord, BMP does not work
string newFileName = fileName.Replace(".bmp", ".png");
using (var image = new MagickImage(fileName))
{
image.Write(newFileName);
discordBridge.SendImage(newFileName, $"> A render of the map with an id: **{discordMap.MapId}**");
if (Config.Send_Rendered_To_Discord)
discordBridge!.SendImage(newFileName, $"> A render of the map with an id: **{map.MapId}**");
if (Config.Send_Rendered_To_Telegram)
telegramBridge!.SendImage(newFileName, $"A render of the map with an id: *{map.MapId}*");
newFileName = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + newFileName;
// Delete the temporary file
if (File.Exists(newFileName))
File.Delete(newFileName);
if (Config.Send_Rendered_To_Discord)
LogToConsole(Translations.TryGet("bot.map.sent_to_discord", map.MapId));
LogToConsole(Translations.TryGet("bot.map.sent_to_discord", discordMap.MapId));
if (Config.Send_Rendered_To_Telegram)
LogToConsole(Translations.TryGet("bot.map.sent_to_telegram", map.MapId));
// Wait for 2 seconds and then try until file is free for deletion
// 10 seconds timeout
Task.Run(async () =>
{
await Task.Delay(2000);
var time = Stopwatch.StartNew();
while (time.ElapsedMilliseconds < 10000) // 10 seconds
{
try
{
// Delete the temporary file
if (File.Exists(newFileName))
File.Delete(newFileName);
}
catch (IOException e) { }
}
});
}
}
}

View file

@ -1,12 +1,18 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DSharpPlus.Entities;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;
using Tomlet.Attributes;
using File = System.IO.File;
namespace MinecraftClient.ChatBots
{
@ -42,11 +48,14 @@ namespace MinecraftClient.ChatBots
[TomlInlineComment("$config.ChatBot.TelegramBridge.ChannelId$")]
public string ChannelId = "";
[TomlInlineComment("$config.ChatBot.TelegramBridge.Authorized_Chat_Ids$")]
public long[] Authorized_Chat_Ids = Array.Empty<long>();
[TomlInlineComment("$config.ChatBot.TelegramBridge.MessageSendTimeout$")]
public int Message_Send_Timeout = 3;
[TomlPrecedingComment("$config.ChatBot.TelegramBridge.Formats$")]
public string PrivateMessageFormat = "**[Private Message]** {username}: {message}";
public string PrivateMessageFormat = "*(Private Message)* {username}: {message}";
public string PublicMessageFormat = "{username}: {message}";
public string TeleportRequestMessageFormat = "A new Teleport Request from **{username}**!";
@ -178,7 +187,30 @@ namespace MinecraftClient.ChatBots
try
{
botClient!.SendTextMessageAsync(Config.ChannelId.Trim(), message).Wait(Config.Message_Send_Timeout);
botClient!.SendTextMessageAsync(Config.ChannelId.Trim(), message, ParseMode.Markdown).Wait(Config.Message_Send_Timeout);
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.canceled_sending"));
LogDebugToConsole(e);
}
}
public void SendImage(string filePath, string? text = null)
{
if (!CanSendMessages())
return;
try
{
string fileName = filePath[(filePath.IndexOf(Path.DirectorySeparatorChar) + 1)..];
Stream stream = File.OpenRead(filePath);
botClient!.SendDocumentAsync(
Config.ChannelId.Trim(),
document: new InputOnlineFile(content: stream, fileName),
caption: text,
parseMode: ParseMode.Markdown).Wait(Config.Message_Send_Timeout * 1000);
}
catch (Exception e)
{
@ -204,7 +236,7 @@ namespace MinecraftClient.ChatBots
}
if (string.IsNullOrEmpty(Config.ChannelId.Trim()))
LogToConsole(x§l§4" + Translations.TryGet("bot.TelegramBridge.missing_channel_id"));
LogToConsole(w§l§f" + Translations.TryGet("bot.TelegramBridge.missing_channel_id"));
botClient = new TelegramBotClient(Config.Token.Trim());
cancellationToken = new CancellationTokenSource();
@ -220,11 +252,18 @@ namespace MinecraftClient.ChatBots
cancellationToken: cancellationToken.Token
);
var me = await botClient.GetMeAsync();
IsConnected = true;
SendMessage(Translations.TryGet("bot.TelegramBridge.connected"));
LogToConsole("§y§l§f" + Translations.TryGet("bot.TelegramBridge.connected", me.Username));
SendMessage("✅ " + Translations.TryGet("bot.TelegramBridge.connected"));
LogToConsole("§y§l§f" + Translations.TryGet("bot.TelegramBridge.connected"));
if (Config.Authorized_Chat_Ids.Length == 0)
{
SendMessage("⚠️ *" + Translations.TryGet("bot.TelegramBridge.missing_authorized_channels") + "* ⚠️");
LogToConsole("§w§l§f" + Translations.TryGet("bot.TelegramBridge.missing_authorized_channels"));
return;
}
await Task.Delay(-1);
}
catch (Exception e)
@ -254,7 +293,29 @@ namespace MinecraftClient.ChatBots
if (text.ToLower().Contains("/start"))
return;
LogDebugToConsole($"Received a '{messageText}' message in chat {chatId}.");
if (text.ToLower().Contains(".chatid"))
{
await botClient.SendTextMessageAsync(chatId: chatId,
replyToMessageId: message.MessageId,
text: $"Chat ID: {chatId}",
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
return;
}
if (Config.Authorized_Chat_Ids.Length > 0 && !Config.Authorized_Chat_Ids.Contains(chatId))
{
LogDebugToConsole($"Unauthorized message '{messageText}' received in a chat with with an ID: {chatId} !");
await botClient.SendTextMessageAsync(
chatId: chatId,
replyToMessageId: message.MessageId,
text: Translations.TryGet("bot.TelegramBridge.unauthorized"),
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
return;
}
LogDebugToConsole($"Received a '{messageText}' message in a chat with with an ID: {chatId} .");
if (bridgeDirection == BridgeDirection.Telegram)
{
@ -270,7 +331,13 @@ namespace MinecraftClient.ChatBots
PerformInternalCommand(command, ref result);
result = string.IsNullOrEmpty(result) ? "-" : result;
await botClient.SendTextMessageAsync(chatId: chatId, replyToMessageId: message.MessageId, text: $"{Translations.TryGet("bot.TelegramBridge.command_executed")}:\n\n{result}", cancellationToken: _cancellationToken);
await botClient.SendTextMessageAsync(
chatId: chatId,
replyToMessageId:
message.MessageId,
text: $"{Translations.TryGet("bot.TelegramBridge.command_executed")}:\n\n{result}",
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
}
else SendText(text);
}