-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
305 lines (262 loc) · 10.8 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Td;
using Telegram.Td.Api;
namespace AutoReplyUserBot
{
public static class Program
{
public static readonly UpdatesHandler AppUpdatesHandler = new();
public static Client App = null;
public static User Me = null;
public static string TDLibVersion = "unknown";
public static DateTime AuthorizationDate;
public static Dictionary<string, FormattedText> AutoReplies = new Dictionary<string, FormattedText>();
public static string State = "none";
// For avoiding the duplicate updates of userbot messages
public static long LastMessageID = -1;
// Only temporary using
public static string currentCommand = null;
public static bool AllowOutput = true;
public static bool Exit = false;
private static DateTime StartTime;
private static Thread TDLibThread = new Thread(() =>
{
Thread.CurrentThread.IsBackground = true;
Client.Run();
});
public static TimeSpan RunTime
{
get
{
return DateTime.Now.Subtract(StartTime);
}
}
static void Main(string[] args)
{
StartTime = DateTime.Now;
Console.ResetColor();
UseEnvironmentVariables();
if (!ProcessConsoleInput(args))
{
Console.Error.WriteLine("App exited by an error!");
return;
}
if (AppData.AllowOutput)
{
Console.WriteLine("Auto-reply userbot!");
}
Client.Execute(new SetLogStream(new LogStreamFile(TDLibData.LogsFilePath, long.MaxValue, false)));
Client.Execute(new SetLogVerbosityLevel(TDLibData.LogLevel));
if (AppData.AllowOutput)
{
Console.WriteLine($"Log level: {TDLibData.LogLevel}");
Console.WriteLine($"Logs saved in {TDLibData.LogsFilePath}");
}
if (AppData.AllowCommands)
{
Console.CancelKeyPress += Console_CancelKeyPress;
}
// TDLib client & Multithreading managing
CreateNewClient();
TDLibThread.Start();
// No stop
while (!Exit) {}
}
private static void UseEnvironmentVariables()
{
// TELEGRAM_API_ID
string api_id_env = Environment.GetEnvironmentVariable("TELEGRAM_API_ID");
if (api_id_env != null)
{
if (int.TryParse(api_id_env, out int api_id))
{
TDLibData.API_ID = api_id;
}
else
{
Console.Error.WriteLine("TELEGRAM_API_ID must be an integer number");
Exit = true;
}
}
// TELEGRAM_API_HASH
string api_hash = Environment.GetEnvironmentVariable("TELEGRAM_API_HASH");
if (api_hash != null)
{
TDLibData.API_HASH = api_hash;
}
}
private static bool ProcessConsoleInput(string[] args)
{
string consoleInputOpt = "";
string currentInputText = "";
int consoleInputState = 0;
for (int i = 0; i < args.Length; i++)
{
string currentOpt = args[i].StartsWith('-') || args[i].StartsWith('/') ? args[i].ToUpper().Substring(1) : args[i].ToUpper();
switch (currentOpt)
{
case "DISABLE_COMMANDS":
AppData.AllowCommands = false;
break;
case "DISABLE_OUTPUT":
AppData.AllowOutput = false;
break;
case "NO_LOGS":
Console.WriteLine("Note: Logs will contains ONLY Fatal errors!");
TDLibData.LogLevel = 0;
break;
case "DB_ALL":
TDLibData.UseChatsDB = true;
TDLibData.UseFilesDB = true;
TDLibData.UseMessagesDB = true;
break;
case "REPLY_WHEN_ONLINE":
AppData.ReplyWhenOnline = true;
break;
case "NO_AUTO_REPLY_SIGNITURE":
AppData.UseAutoReplySigniture = false;
break;
case "TEST_DC":
TDLibData.UseTestDc = true;
break;
case "API_ID":
case "API_HASH":
case "TELEGRAM_API_ID":
case "TELEGRAM_API_HASH":
case "LOG_LEVEL":
case "NEW_AUTO_REPLY_SIGNITURE":
consoleInputState = 1;
consoleInputOpt = currentOpt;
break;
default:
if (consoleInputState > 0)
{
if (consoleInputState == 1)
{
if (consoleInputOpt == "API_ID" || consoleInputOpt == "TELEGRAM_API_ID")
{
if (!int.TryParse(currentOpt, out int api_id))
{
Console.Error.WriteLine($"{consoleInputOpt} isn\'t an integer number!");
return false;
}
else
{
TDLibData.API_ID = api_id;
}
}
else if (consoleInputOpt == "API_HASH" || consoleInputOpt == "TELEGRAM_API_HASH")
{
TDLibData.API_HASH = currentOpt.ToLower();
}
else if (consoleInputOpt == "LOG_LEVEL")
{
if (!int.TryParse(currentOpt, out int logLevel))
{
Console.Error.WriteLine($"{consoleInputOpt} isn\'t an integer number!");
return false;
}
else
{
TDLibData.LogLevel = logLevel;
}
}
else if (currentOpt.StartsWith('"'))
{
// To remove
currentInputText += currentOpt.Substring(1);
}
}
// In case of long text, Currently [NEW_AUTO_REPLY_SIGNITURE]
if (consoleInputState == 2)
{
if (currentOpt.EndsWith('"'))
{
currentOpt.Remove(currentOpt.Length - 1);
if (consoleInputOpt == "NEW_AUTO_REPLY_SIGNITURE")
{
AppData.AutoReplySignitureText = currentInputText;
}
currentInputText = "";
consoleInputState = 0;
}
currentInputText += currentOpt;
}
}
else
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine($"Invalid console argument ({args[i].ToUpper()})");
Console.ResetColor();
}
break;
}
}
// To make all info compitible
AppData.Refresh();
return true;
}
private static void CreateNewClient()
{
App = Client.Create(AppUpdatesHandler);
}
private static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)
{
EnterCmd:
if (AllowOutput)
{
Console.Write("Enter command: ");
}
Console.ForegroundColor = ConsoleColor.Cyan;
string cmdline = Console.ReadLine();
Console.ResetColor();
if (string.IsNullOrWhiteSpace(cmdline)) Environment.Exit(0);
string[] cmds = cmdline.Split(' ');
string cmd = cmds[0];
switch (cmd.ToLower())
{
case "logout":
App.Send(new LogOut(), null);
App.Send(new Close(), null);
Exit = true;
break;
case "close":
App.Send(new Close(), null);
CreateNewClient();
return;
case "info":
App.Send(new GetMe(), new UserUpdateHandler());
goto EnterCmd;
case "config":
Console.WriteLine("Sending GetApplicationConfig");
App.Send(new GetApplicationConfig(), new OutputHandler());
Console.WriteLine("GetApplicationConfig was sent");
goto EnterCmd;
case "getoption":
App.Send(new GetOption(cmds[1]), new OutputHandler());
Thread.Sleep(600);
goto EnterCmd;
case "tdlib_version":
case "tdlib":
case "tdlib_ver":
Console.WriteLine($"TDLib version: {TDLibVersion}");
break;
case "version":
case "v":
case "ver":
Console.WriteLine($"AutoReplyUserBot version: {Helper.GetAppVersion()}");
break;
case "":
case "\n":
Exit = true;
break;
default:
Console.Error.WriteLine("Command not found, If you don't need anything press Enter");
goto EnterCmd;
}
}
}
}