Forge
csharp1a59f514
1using System.Text;
2using Spectre.Console;
3using Spectre.Console.Cli;
4using TelegramNotifier;
5using TelegramNotifier.Commands;
6using WTelegram;
7
8var currDir = Directory.GetCurrentDirectory();
9
10if (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
27var ca = new ConfigurationAccessor();
28var tgSettings = ca.Settings;
29using var client = new Client(tgSettings.API_ID, tgSettings.API_HASH, tgSettings.SessionPathName);
30var logPath = Path.Combine(currDir, tgSettings.LogFileName);
31await 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
46static 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
View only · write via MCP/CIDE