-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMilkshakeCupTelegramBotClient.cs
More file actions
64 lines (56 loc) · 2.19 KB
/
MilkshakeCupTelegramBotClient.cs
File metadata and controls
64 lines (56 loc) · 2.19 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
namespace MilkshakeCup
{
using System;
using System.Collections.Generic;
using System.Linq;
using MilkshakeCup.Commands;
using MilkshakeCup.Models;
using Telegram.Bot;
using Telegram.Bot.Args;
using CommandInfo = System.Collections.Generic.KeyValuePair<string, Commands.Command>;
public class MilkshakeCupTelegramBotClient : TelegramBotClient
{
private static readonly Dictionary<string, Command> _commands =
new Dictionary<string, Command>
{
{ "/tablas", GroupsCommand.Execute },
{ "/tabla", SingleGroupCommand.Execute },
{ "/marcador", MatchCommand.Execute },
{ "/borrar", RevertCommand.Execute }
};
private static readonly CommandInfo _notFound = default(CommandInfo);
private readonly IGroupsRepository _groupsRepository;
public MilkshakeCupTelegramBotClient(string token, IGroupsRepository groupsRepository)
: base(token)
{
_groupsRepository = groupsRepository;
OnMessage += HandleCommand;
}
private async void HandleCommand(object sender, MessageEventArgs eventArgs)
{
try
{
if (!CommandInfo(eventArgs).Equals(_notFound))
{
await CommandInfo(eventArgs).Value(CommandContext(sender, eventArgs));
}
}
catch (Exception ex)
{
// fail silently
Console.WriteLine($"There was an exception with message e: {ex.Message}");
Console.WriteLine(ex.StackTrace);
}
}
private static CommandInfo CommandInfo(MessageEventArgs eventArgs) =>
_commands.FirstOrDefault(commandInfo => Text(eventArgs).StartsWith(commandInfo.Key));
private static string Text(MessageEventArgs e) => e?.Message?.Text ?? "";
private CommandContext CommandContext(object sender, MessageEventArgs eventArgs) =>
new CommandContext(
_groupsRepository,
this,
eventArgs,
sender,
Text(eventArgs).Split(' '));
}
}