-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
93 lines (80 loc) · 3.36 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
using GastroPimp.Commands;
using GastroPimp.Messaging;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace GastroPimp
{
class Program
{
private static TelegramBotClient _botClient;
private static MessagingBroker _messagingBroker;
private static CommandManager _commandManager;
private static Slavery _slavery;
static async Task Main(string[] args)
{
_botClient = new TelegramBotClient("2040809860:AAGVkRBZI7St9r9NufLdon3mWwnI2lbKWi8");
var me = await _botClient.GetMeAsync();
Console.WriteLine(
$"Hello, World! I am user {me.Id} and my name is {me.FirstName}."
);
_messagingBroker = new MessagingBroker(_botClient);
_commandManager = new CommandManager();
_slavery = new Slavery(_messagingBroker);
using var cts = new CancellationTokenSource();
_botClient.StartReceiving(
new DefaultUpdateHandler(OnSuccess, OnError),
cts.Token);
Console.WriteLine($"Start listening for @{me.Username}");
Console.ReadLine();
cts.Cancel();
}
static Task OnError(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
var errorMessage = exception.Message;
Console.WriteLine(errorMessage);
return Task.CompletedTask;
}
static async Task OnSuccess(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
if (update.Type == UpdateType.CallbackQuery)
{
await _slavery.AssignSlave(update.CallbackQuery);
return;
}
if (update.Type != UpdateType.Message)
return;
if (update.Message.Type != MessageType.Text)
return;
var chatId = update.Message.Chat.Id;
var isCommand = update.Message.Entities != null &&
update.Message.Entities.FirstOrDefault()?.Type == MessageEntityType.BotCommand;
if (!isCommand)
return;
var command = _commandManager.ProcessCommand(update.Message);
switch (command.Name)
{
case CommandType.done:
await _slavery.RetireSlave(update.Message, command.AttachedMessage);
break;
case CommandType.jrat:
await _slavery.QueenWantsSomeFood(command.AttachedMessage, update.Message.From.Id, chatId);
break;
case CommandType.money:
if (int.TryParse(command.AttachedMessage, out var reduceBy))
await _slavery.ShowHowMuchMoneyLeft(chatId, reduceBy);
else
await _slavery.ShowHowMuchMoneyLeft(chatId);
break;
case CommandType.start:
await _messagingBroker.SendInstructions(chatId);
break;
}
}
}
}