-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
74 lines (63 loc) · 2.41 KB
/
Program.cs
File metadata and controls
74 lines (63 loc) · 2.41 KB
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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using TL;
namespace WTelegramClient_session_Sqllite
{
//t.me/nader5
static class Program
{
static WTelegram.Client Client;
static User My;
static Dictionary<long, User> Users = new();
static Dictionary<long, ChatBase> Chats = new();
static async Task Main(string[] _)
{
var exit = new SemaphoreSlim(0);
AppDomain.CurrentDomain.ProcessExit += (s, e) => exit.Release();
var store = new SqliteStore("SQLLiteDatabase.db", "session1");
Client = new WTelegram.Client(what => Config(what), store);
await using (Client)
{
Client.OnUpdates += Client_OnUpdates;
My = await Client.LoginUserIfNeeded();
Console.WriteLine($"We are logged-in as {My.username ?? My.first_name + " " + My.last_name} (id {My.id})");
var dialogs = await Client.Messages_GetAllDialogs();
dialogs.CollectUsersChats(Users, Chats);
await exit.WaitAsync();
}
}
private static async Task Client_OnUpdates(UpdatesBase updates)
{
updates.CollectUsersChats(Users, Chats);
foreach (var update in updates.UpdateList)
{
Console.WriteLine(update.GetType().Name);
if (update is UpdateNewMessage { message: Message { peer_id: PeerUser { user_id: var user_id } } msg }) // private message
if (!msg.flags.HasFlag(Message.Flags.out_))
if (Users.TryGetValue(user_id, out var user))
{
Console.WriteLine($"New message from {user}: {msg.message}");
if (msg.message.Equals("Ping", StringComparison.OrdinalIgnoreCase))
await Client.SendMessageAsync(user, "Pong");
}
}
}
public static string Config(string what)
{
switch (what)
{
case "api_id": return "api_id";
case "api_hash": return "phone_number";
case "phone_number": return "+phone_number";
case "first_name": return "nader";
case "verification_code": Console.Write("Code: "); return Console.ReadLine();
case "last_name": return "last_name";
// if sign-up is required
case "password": return "1426"; // if user has enabled 2FA
default: return null; // let WTelegramClient decide the default config
}
}
}
}