67 lines
1.6 KiB
C#
67 lines
1.6 KiB
C#
using Telegram.Bot;
|
|
using Telegram.Bot.Types.Enums;
|
|
|
|
var homeDir = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
|
|
var configFile = Path.Join(homeDir, ".telegram-send.conf");
|
|
string config;
|
|
|
|
if (args.Length == 0)
|
|
PrintSyntaxAndExit();
|
|
|
|
if (args[0] == "--config") {
|
|
if (args.Length != 3)
|
|
PrintSyntaxAndExit();
|
|
|
|
config = $"""
|
|
token={args[1]}
|
|
chatId={args[2]}
|
|
""";
|
|
|
|
File.WriteAllText(configFile, config);
|
|
Environment.Exit(0);
|
|
}
|
|
|
|
if (args.Length > 3)
|
|
PrintSyntaxAndExit();
|
|
|
|
config = File.ReadAllText(configFile);
|
|
var token = config.Split("\n")[0].Replace("token=", "");
|
|
var chatId = config.Split("\n")[1].Replace("chatId=", "");
|
|
if (args[0] == "-" && args.Contains("--linefeed")) {
|
|
var bot = new TelegramBotClient(token);
|
|
while (Console.In.ReadLine() is { } line) {
|
|
var message = line.Trim();
|
|
if (message.Length > 0) {
|
|
if (args.Contains("--pre"))
|
|
message = $"```\n{message}\n```";
|
|
|
|
await bot.SendTextMessageAsync(chatId, EscapeText(message), ParseMode.MarkdownV2);
|
|
}
|
|
}
|
|
}
|
|
else {
|
|
var message = args[0] == "-" ? Console.In.ReadToEnd() : args[0];
|
|
var bot = new TelegramBotClient(token);
|
|
|
|
if (args.Contains("--pre"))
|
|
message = $"```\n{message}\n```";
|
|
|
|
await bot.SendTextMessageAsync(chatId, EscapeText(message), ParseMode.MarkdownV2);
|
|
}
|
|
|
|
void PrintSyntaxAndExit() {
|
|
Console.WriteLine("""
|
|
Syntax:
|
|
telegram-send --config <token> <chatid>
|
|
telegram-send <message> [--pre]
|
|
|
|
To read from stdin, pass '-' as message.
|
|
""");
|
|
Environment.Exit(1);
|
|
}
|
|
|
|
string EscapeText(string message) {
|
|
return message
|
|
.Replace(".", "\\.")
|
|
.Replace("=", "\\=");
|
|
} |