-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
180 lines (161 loc) · 4.38 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
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Telegram.Bot;
using Telegram.Bot.Exceptions;
using Telegram.Bot.Extensions.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace BotTest
{
class Program
{
static TelegramBotClient botClient;
async static Task Main()
{
botClient = new TelegramBotClient(
"INSERT BOT TOKEN HERE"
);
Subscribers.ReadSubscribersFromFile();
var wednesdayInvestigator = new WednesdayInvestigator();
wednesdayInvestigator.ItIsWednesdayEvent += Bot_OnWednesday;
wednesdayInvestigator.BeginInvestigation();
var me = await botClient.GetMeAsync();
Console.WriteLine(
$"Hello, World! I am user {me.Id}" +
$"and my name is {me.FirstName}."
);
var receiverOptions = new ReceiverOptions
{
// receive all update types
AllowedUpdates = { }
};
using var cts = new CancellationTokenSource();
botClient.StartReceiving(
HandleUpdateAsync,
HandleError,
receiverOptions,
cts.Token);
// Bot Command Menu
uint input = 0U;
do
{
Console.WriteLine("0...Exit");
Console.WriteLine("1...Show Subscribers");
Console.WriteLine("2...Send Message");
Console.WriteLine("3...Remove All Subscribers");
Console.WriteLine("4...Time until Wednesday");
input = uint.Parse(Console.ReadLine());
switch (input)
{
case 0U:
break;
case 1U:
Subscribers.PrintSubscribers();
break;
case 2U:
Console.Write("ChatID: ");
var id = Console.ReadLine();
Console.WriteLine("Message: ");
var text = Console.ReadLine();
await botClient.SendTextMessageAsync(id, text);
break;
case 3U:
previousSubscriberOperation.Wait();
previousSubscriberOperation = Subscribers.RemoveAll();
break;
case 4U:
Console.WriteLine(
WednesdayInvestigator.WhenIsWednesdayMyDude()
);
break;
}
} while (input != 0U);
cts.Cancel();
await previousSubscriberOperation;
wednesdayInvestigator.EndInvestigation();
}
private static async Task HandleUpdateAsync(
ITelegramBotClient client,
Update update,
CancellationToken cancellationToken)
{
if (update.Type == UpdateType.Message &&
update.Message!.Type == MessageType.Text)
{
var chatId = update.Message.Chat.Id;
var messageText = update.Message.Text;
Console.WriteLine($"Received text message: {messageText}");
await HandleTextMessage(messageText, chatId);
}
}
private static async Task HandleTextMessage(string messageText, long chatId)
{
// Es handelt sich um eine Textnachricht
if (messageText.StartsWith("/"))
{
// Es wird ein Kommando erwartet
switch (messageText)
{
case "/start":
case "/subscribe":
await previousSubscriberOperation;
previousSubscriberOperation =
Subscribers.AddSubscriber(chatId);
break;
case "/unsubscribe":
await previousSubscriberOperation;
previousSubscriberOperation =
Subscribers.RemoveSubscriber(chatId);
break;
case "/whensdaymydude":
await botClient.SendTextMessageAsync(
chatId,
WednesdayInvestigator
.WhenIsWednesdayMyDude()
.ToString());
break;
}
}
else
{
await botClient.SendTextMessageAsync(
chatId,
$"It is {messageText}, my dude!"
);
}
}
private static Task HandleError(
ITelegramBotClient botClient,
Exception exception,
CancellationToken cancellation)
{
var ErrorMessage = exception switch
{
ApiRequestException apiRequestException
=> $"Telegram API Error:\n" +
$"[{apiRequestException.ErrorCode}]" +
$"\n{apiRequestException.Message}",
_ => exception.ToString()
};
Console.Error.WriteLine(ErrorMessage);
return Task.CompletedTask;
}
private static void Bot_OnWednesday()
{
var sendTasks = new List<Task<Message>>();
foreach (var id in Subscribers.ChatIDs)
{
sendTasks.Add(
botClient.SendTextMessageAsync(
id,
"IT IS WEDNESDAY, MY DUDE!"
)
);
}
Task.WaitAll(sendTasks.ToArray());
}
private static Task previousSubscriberOperation = Task.CompletedTask;
}
}