Minecraft-Console-Client/MinecraftClient/ChatBots/TelegramBridge.cs

376 lines
13 KiB
C#
Raw Normal View History

2022-10-24 15:35:24 +02:00
using System;
using System.IO;
using System.Linq;
2022-10-24 15:35:24 +02:00
using System.Threading;
using System.Threading.Tasks;
using Brigadier.NET;
using Brigadier.NET.Builder;
using MinecraftClient.CommandHandler;
using MinecraftClient.CommandHandler.Patch;
using MinecraftClient.Scripting;
2022-10-24 15:35:24 +02:00
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;
2022-10-24 15:35:24 +02:00
using Tomlet.Attributes;
using File = System.IO.File;
2022-10-24 15:35:24 +02:00
namespace MinecraftClient.ChatBots
{
public class TelegramBridge : ChatBot
{
public const string CommandName = "tgbridge";
2022-10-24 15:35:24 +02:00
private enum BridgeDirection
{
Both = 0,
Minecraft,
Telegram
}
private static TelegramBridge? instance = null;
public bool IsConnected { get; private set; }
private TelegramBotClient? botClient;
private CancellationTokenSource? cancellationToken;
private BridgeDirection bridgeDirection = BridgeDirection.Both;
public static Configs Config = new();
[TomlDoNotInlineObject]
public class Configs
{
[NonSerialized]
private const string BotName = "TelegramBridge";
public bool Enabled = false;
[TomlInlineComment("$ChatBot.TelegramBridge.Token$")]
2022-10-24 15:35:24 +02:00
public string Token = "your bot token here";
[TomlInlineComment("$ChatBot.TelegramBridge.ChannelId$")]
2022-10-24 15:35:24 +02:00
public string ChannelId = "";
[TomlInlineComment("$ChatBot.TelegramBridge.Authorized_Chat_Ids$")]
public long[] Authorized_Chat_Ids = Array.Empty<long>();
[TomlInlineComment("$ChatBot.TelegramBridge.MessageSendTimeout$")]
2022-10-24 15:35:24 +02:00
public int Message_Send_Timeout = 3;
[TomlPrecedingComment("$ChatBot.TelegramBridge.Formats$")]
public string PrivateMessageFormat = "*(Private Message)* {username}: {message}";
2022-10-24 15:35:24 +02:00
public string PublicMessageFormat = "{username}: {message}";
public string TeleportRequestMessageFormat = "A new Teleport Request from **{username}**!";
public void OnSettingUpdate()
{
Message_Send_Timeout = Message_Send_Timeout <= 0 ? 3 : Message_Send_Timeout;
}
}
public TelegramBridge()
{
instance = this;
}
public override void Initialize()
{
2022-12-06 20:32:46 +08:00
Handler.dispatcher.Register(l => l.Literal("help")
.Then(l => l.Literal(CommandName)
.Executes(r => OnCommandHelp(r.Source, string.Empty))
)
);
2022-12-06 20:32:46 +08:00
Handler.dispatcher.Register(l => l.Literal(CommandName)
.Then(l => l.Literal("direction")
.Then(l => l.Literal("both")
.Executes(r => OnCommandDirection(r.Source, BridgeDirection.Both)))
.Then(l => l.Literal("mc")
.Executes(r => OnCommandDirection(r.Source, BridgeDirection.Minecraft)))
.Then(l => l.Literal("telegram")
.Executes(r => OnCommandDirection(r.Source, BridgeDirection.Telegram))))
.Then(l => l.Literal("_help")
2022-12-06 20:32:46 +08:00
.Redirect(Handler.dispatcher.GetRoot().GetChild("help").GetChild(CommandName)))
);
2022-10-24 15:35:24 +02:00
Task.Run(async () => await MainAsync());
}
2022-12-06 20:32:46 +08:00
public override void OnUnload()
2022-10-24 15:35:24 +02:00
{
2022-12-06 20:32:46 +08:00
Handler.dispatcher.Unregister(CommandName);
Handler.dispatcher.GetRoot().GetChild("help").RemoveChild(CommandName);
2022-10-24 15:35:24 +02:00
Disconnect();
}
private int OnCommandHelp(CmdResult r, string? cmd)
{
return r.SetAndReturn(cmd switch
{
#pragma warning disable format // @formatter:off
_ => Translations.error_usage + ": /tgbridge direction <both|mc|telegram>"
2022-12-06 20:32:46 +08:00
+ '\n' + Handler.dispatcher.GetAllUsageString(CommandName, false),
#pragma warning restore format // @formatter:on
});
}
private int OnCommandDirection(CmdResult r, BridgeDirection direction)
{
string bridgeName;
switch (direction)
{
case BridgeDirection.Both:
bridgeName = Translations.bot_TelegramBridge_direction_both;
bridgeDirection = BridgeDirection.Both;
break;
case BridgeDirection.Minecraft:
bridgeName = Translations.bot_TelegramBridge_direction_minecraft;
bridgeDirection = BridgeDirection.Minecraft;
break;
case BridgeDirection.Telegram:
bridgeName = Translations.bot_TelegramBridge_direction_Telegram;
bridgeDirection = BridgeDirection.Telegram;
break;
default:
goto case BridgeDirection.Both;
}
return r.SetAndReturn(CmdResult.Status.Done, string.Format(Translations.bot_TelegramBridge_direction, bridgeName));
}
~TelegramBridge()
2022-10-24 15:35:24 +02:00
{
Disconnect();
}
private void Disconnect()
{
if (botClient != null)
{
try
{
SendMessage(Translations.bot_TelegramBridge_disconnected);
2022-10-24 15:35:24 +02:00
cancellationToken?.Cancel();
botClient = null;
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.bot_TelegramBridge_canceled_sending);
2022-10-24 15:35:24 +02:00
LogDebugToConsole(e);
}
IsConnected = false;
}
}
public static TelegramBridge? GetInstance()
{
return instance;
}
public override void GetText(string text)
{
if (!CanSendMessages())
return;
text = GetVerbatim(text).Trim();
// Stop the crash when an empty text is recived somehow
if (string.IsNullOrEmpty(text))
return;
string message = "";
string username = "";
if (IsPrivateMessage(text, ref message, ref username))
message = Config.PrivateMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim();
else if (IsChatMessage(text, ref message, ref username))
message = Config.PublicMessageFormat.Replace("{username}", username).Replace("{message}", message).Replace("{timestamp}", GetTimestamp()).Trim();
else if (IsTeleportRequest(text, ref username))
message = Config.TeleportRequestMessageFormat.Replace("{username}", username).Replace("{timestamp}", GetTimestamp()).Trim();
else message = text;
SendMessage(message);
}
public void SendMessage(string message)
{
if (!CanSendMessages() || string.IsNullOrEmpty(message))
return;
try
{
botClient!.SendTextMessageAsync(Config.ChannelId.Trim(), message, ParseMode.Markdown).Wait(Config.Message_Send_Timeout);
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.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);
2022-10-24 15:35:24 +02:00
}
catch (Exception e)
{
LogToConsole("§w§l§f" + Translations.bot_TelegramBridge_canceled_sending);
2022-10-24 15:35:24 +02:00
LogDebugToConsole(e);
}
}
private bool CanSendMessages()
{
return botClient != null && !string.IsNullOrEmpty(Config.ChannelId.Trim()) && bridgeDirection != BridgeDirection.Minecraft;
}
async Task MainAsync()
{
try
{
if (string.IsNullOrEmpty(Config.Token.Trim()))
{
LogToConsole(Translations.bot_TelegramBridge_missing_token);
2022-10-24 15:35:24 +02:00
UnloadBot();
return;
}
if (string.IsNullOrEmpty(Config.ChannelId.Trim()))
LogToConsole("§w§l§f" + Translations.bot_TelegramBridge_missing_channel_id);
2022-10-24 15:35:24 +02:00
botClient = new TelegramBotClient(Config.Token.Trim());
cancellationToken = new CancellationTokenSource();
botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
pollingErrorHandler: HandlePollingErrorAsync,
receiverOptions: new ReceiverOptions
{
// receive all update types
AllowedUpdates = Array.Empty<UpdateType>()
},
cancellationToken: cancellationToken.Token
);
IsConnected = true;
SendMessage($"✅ {Translations.bot_TelegramBridge_connected}");
LogToConsole($"§y§l§f{Translations.bot_TelegramBridge_connected}");
if (Config.Authorized_Chat_Ids.Length == 0)
{
SendMessage($"⚠️ *{Translations.bot_TelegramBridge_missing_authorized_channels}* ⚠️");
LogToConsole($"§w§l§f{Translations.bot_TelegramBridge_missing_authorized_channels}");
return;
}
2022-10-24 15:35:24 +02:00
await Task.Delay(-1);
}
catch (Exception e)
{
LogToConsole($"§w§l§f{Translations.bot_TelegramBridge_unknown_error}");
2022-10-24 15:35:24 +02:00
LogToConsole(e);
return;
}
}
private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken _cancellationToken)
{
// Only process Message updates: https://core.telegram.org/bots/api#message
if (update.Message is not { } message)
return;
// Only process text messages
if (message.Text is not { } messageText)
return;
var chatId = message.Chat.Id;
var text = message.Text;
if (string.IsNullOrEmpty(text) || string.IsNullOrWhiteSpace(text))
return;
if (text.ToLower().Contains("/start"))
return;
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.bot_TelegramBridge_unauthorized,
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
return;
}
LogDebugToConsole($"Received a '{messageText}' message in a chat with with an ID: {chatId} .");
2022-10-24 15:35:24 +02:00
if (bridgeDirection == BridgeDirection.Telegram)
{
if (!text.StartsWith(".dscbridge"))
return;
}
if (text.StartsWith("."))
{
var command = text[1..];
CmdResult result = new();
2022-10-24 15:35:24 +02:00
PerformInternalCommand(command, ref result);
await botClient.SendTextMessageAsync(
chatId: chatId,
replyToMessageId:
message.MessageId,
text: $"{Translations.bot_TelegramBridge_command_executed}:\n\n{result}",
cancellationToken: _cancellationToken,
parseMode: ParseMode.Markdown);
2022-10-24 15:35:24 +02:00
}
else SendText(text);
}
private Task HandlePollingErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken _cancellationToken)
{
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n[{apiRequestException.ErrorCode}]\n{apiRequestException.Message}",
_ => exception.ToString()
};
LogToConsole("§w§l§f" + ErrorMessage);
return Task.CompletedTask;
}
}
}