| 1 | using System.Text; |
| 2 | using Spectre.Console; |
| 3 | using Spectre.Console.Cli; |
| 4 | using TelegramNotifier; |
| 5 | using TelegramNotifier.Commands; |
| 6 | using WTelegram; |
| 7 | |
| 8 | var currDir = Directory.GetCurrentDirectory(); |
| 9 | |
| 10 | if (args.Length == 0) |
| 11 | { |
| 12 | var usagePath = Path.Combine(currDir, "APP_USAGE.txt"); |
| 13 | if (File.Exists(usagePath)) |
| 14 | { |
| 15 | var usage = File.ReadAllText(usagePath); |
| 16 | Console.WriteLine(usage); |
| 17 | } |
| 18 | else |
| 19 | { |
| 20 | var app = new CommandApp(); |
| 21 | ConfigureCommands(app); |
| 22 | return await app.RunAsync(["--help"]).ConfigureAwait(false); |
| 23 | } |
| 24 | return 0; |
| 25 | } |
| 26 | |
| 27 | var ca = new ConfigurationAccessor(); |
| 28 | var tgSettings = ca.Settings; |
| 29 | using var client = new Client(tgSettings.API_ID, tgSettings.API_HASH, tgSettings.SessionPathName); |
| 30 | var logPath = Path.Combine(currDir, tgSettings.LogFileName); |
| 31 | await using (var logStream = new StreamWriter(logPath, true, Encoding.UTF8) { AutoFlush = true }) |
| 32 | { |
| 33 | WTelegram.Helpers.Log = (lvl, str) => logStream.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} [{"TDIWE!"[lvl]}] {str}"); |
| 34 | var tm = new TelegramManager(client); |
| 35 | await tm.DoLogin(tgSettings.AccountPhone).ConfigureAwait(false); |
| 36 | RunContext.Config = ca; |
| 37 | RunContext.TelegramManager = tm; |
| 38 | |
| 39 | var commandApp = new CommandApp(); |
| 40 | ConfigureCommands(commandApp); |
| 41 | var result = await commandApp.RunAsync(args).ConfigureAwait(false); |
| 42 | WTelegram.Helpers.Log = (_, _) => { }; // client.Dispose() may log; stream is about to close |
| 43 | return result; |
| 44 | } |
| 45 | |
| 46 | static void ConfigureCommands(ICommandApp app) |
| 47 | { |
| 48 | app.Configure(config => |
| 49 | { |
| 50 | config.AddCommand<SendMessageToUserCommand>("send-message-to-user"); |
| 51 | config.AddCommand<CreateChatCommand>("create-chat"); |
| 52 | config.AddCommand<DeleteChatCommand>("delete-chat"); |
| 53 | config.AddCommand<InviteUserCommand>("invite-user"); |
| 54 | config.AddCommand<DeleteUserFromChatCommand>("delete-user-from-chat"); |
| 55 | config.AddCommand<ListChatsCommand>("list-chats"); |
| 56 | config.AddCommand<GetMessagesCommand>("get-messages"); |
| 57 | config.AddCommand<GetMediaCommand>("get-media"); |
| 58 | config.AddCommand<CreateTopicCommand>("create-topic"); |
| 59 | config.AddCommand<SendMessageToChatCommand>("send-message-to-chat"); |
| 60 | config.AddCommand<EditMessageCommand>("edit-message"); |
| 61 | config.AddCommand<DeleteMessageCommand>("delete-message"); |
| 62 | config.AddCommand<UpdateOldMessagesCommand>("update-old-messages"); |
| 63 | }); |
| 64 | } |
| 65 | |