forked from retran/psnbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MessagePoller.cs
185 lines (164 loc) · 6.08 KB
/
MessagePoller.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
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using PSNBot.Telegram;
using System.Collections.Generic;
using PSNBot.Services;
using PSNBot.Commands;
using PSNBot.Process;
using PSNBot.Model;
namespace PSNBot
{
public class MessagePoller : IDisposable
{
private TelegramClient _client;
private long _offset = 0;
private CancellationTokenSource _cancellationTokenSource;
private bool _disposed = false;
private PSNService _psnService;
private AccountService _accounts;
private DateTime _lastCheckDateTime = DateTime.Now;
private IEnumerable<Command> _commands;
private long _chatId;
private DatabaseService _databaseService;
private RegistrationProcess _registrationProcess;
public MessagePoller(DatabaseService databaseService, TelegramClient client, PSNService psnService, AccountService accounts,
RegistrationProcess registrationProcess, long chatId)
{
_chatId = chatId;
_client = client;
_psnService = psnService;
_accounts = accounts;
_databaseService = databaseService;
_registrationProcess = registrationProcess;
_commands = new Command[]
{
new TopCommand(_psnService, client, _accounts),
new SearchCommand(_psnService, client, _accounts),
new ListCommand(_psnService, client, _accounts),
new StartCommand(_psnService, client, _accounts, _registrationProcess),
new HelpCommand(_psnService, client, _accounts),
new RulesCommand(_psnService, client, _accounts),
new OnlineCommand(_psnService, client, _accounts),
new DeleteCommand(_psnService, client, _accounts, _registrationProcess),
new SetInterestsCommand(_psnService, client, _accounts, _registrationProcess),
new SetTrophiesCommand(_psnService, client, _accounts, _registrationProcess),
};
}
private async Task Poll()
{
try
{
var updates = await _client.GetUpdates(new GetUpdatesQuery()
{
Offset = _offset
});
if (updates.Result.Any())
{
_offset = updates.Result.Last().UpdateId + 1;
}
foreach (var update in updates.Result)
{
if (update.Message != null && (!string.IsNullOrEmpty(update.Message.Text) || update.Message.NewChatMember != null) && (update.Message.Chat.Id == _chatId || update.Message.Chat.Type == "private"))
{
Handle(update.Message);
}
}
}
catch (Exception e)
{
Console.WriteLine(string.Format("{0}\t{1}", DateTime.Now, e.Message));
}
}
private async void Handle(Message message)
{
if (message.From != null)
{
Console.WriteLine(string.Format("{0} {1}: {2}", DateTime.Now, message.From.Username, message.Text));
}
var account = _accounts.GetById(message.From.Id);
if (account != null && account.Status != Status.Ok)
{
await _registrationProcess.HandleCurrentStep(account, message);
return;
}
if (account != null && account.TelegramName != (message.From.Username ?? string.Empty))
{
account.TelegramName = message.From.Username ?? string.Empty;
_accounts.Update(account);
}
if (message.NewChatMember != null && account == null)
{
await _client.SendMessage(new SendMessageQuery()
{
ChatId = message.Chat.Id,
ReplyToMessageId = message.MessageId,
Text = Messages.GetWelcomeMessage(message.NewChatMember)
});
return;
}
if (string.IsNullOrEmpty(message.Text))
{
return;
}
message.Text = message.Text.ToLower();
if (message.Chat.Type != "private" && !message.Text.Contains("@clankbot"))
{
return;
}
message.Text = message.Text.Replace("@clankbot", "");
var command = _commands.FirstOrDefault(c => c.IsApplicable(message));
if (command != null)
{
if (command.IsPrivateOnly() && message.Chat.Type != "private")
{
await _client.SendMessage(new SendMessageQuery()
{
ChatId = message.Chat.Id,
ReplyToMessageId = message.MessageId,
Text = Messages.PrivateOnlyCommand
});
return;
}
await command.Handle(message);
}
}
public void Start()
{
_cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = _cancellationTokenSource.Token;
Task.Factory.StartNew(() =>
{
while (!cancellationToken.IsCancellationRequested)
{
var task = Poll();
task.Wait();
Thread.Sleep(1000);
}
});
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
// ...
}
_cancellationTokenSource.Cancel();
_cancellationTokenSource.Dispose();
_disposed = true;
}
}
~MessagePoller()
{
Dispose(false);
}
}
}