-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTaskBot.cs
More file actions
180 lines (159 loc) · 7.04 KB
/
TaskBot.cs
File metadata and controls
180 lines (159 loc) · 7.04 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
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 Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
namespace TelegramTaskBot
{
public class TaskBot
{
private readonly TelegramBotClient _botClient;
private readonly Dictionary<long, List<string>> _userMessages;
public TaskBot(string token)
{
_botClient = new TelegramBotClient(token);
_userMessages = new Dictionary<long, List<string>>();
}
public async Task StartAsync()
{
var me = await _botClient.GetMe(); // Ensure the correct method name is used
Console.WriteLine($"Bot {me.Username} is starting...");
// set up receiving options
var receiverOptions = new ReceiverOptions
{
AllowedUpdates = Array.Empty<UpdateType>()
};
_botClient.StartReceiving(
updateHandler: HandleUpdateAsync,
errorHandler: HandleErrorAsync,
receiverOptions: receiverOptions
);
Console.WriteLine("Press any key to stop the bot.");
}
private async Task HandleUpdateAsync(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
{
if (update.Message is not { } message)
return;
if (message.Text is not { } messageText)
return;
var ChatId = message.Chat.Id;
var username = message.From?.Username ?? "Unknown User";
Console.WriteLine($"Received a message from {username}: {messageText}");
//// echo the message back
//await botClient.SendMessage(
// chatId: ChatId,
// text: $"You said: {messageText}",
// cancellationToken: cancellationToken
//);
// handle commands
if (messageText.StartsWith("/")) {
await HandleCommandAsync(ChatId, messageText, cancellationToken);
return;
}
// store the commands
if (!_userMessages.ContainsKey(ChatId))
{
_userMessages[ChatId] = new List<string>();
}
_userMessages[ChatId].Add(messageText);
await botClient.SendMessage(
chatId: ChatId,
text: $"✅ Message stored! Total: {_userMessages[ChatId].Count}\n" +
$"Use /preview to see or /generate to create Excel.",
cancellationToken: cancellationToken);
}
private async Task HandleCommandAsync(long chatId, string command, CancellationToken cancellationToken) {
string response = command.ToLower();
switch(response)
{
case "/start":
response = "👋 Welcome! Send me task messages and I'll convert them to Excel!\n\n" +
"Format:\n" +
"person name: John\n" +
"task one: 10\n" +
"task two: 5\n\n" +
"Use /help for more info.";
break;
case "/help":
response = "📚 Available Commands:\n" +
"/start - Welcome message\n" +
"/help - Show this help\n" +
"/preview - Preview stored messages\n" +
"/generate - Create Excel file\n" +
"/clear - Clear all messages";
break;
case "/preview":
if (!_userMessages.ContainsKey(chatId) || _userMessages[chatId].Count == 0)
{
response = "No messages stored yet.";
}
else {
var messages = string.Join("\n\n", _userMessages[chatId]);
response = $"📝 Stored Messages ({_userMessages[chatId].Count}):\n\n{messages}";
}
break;
case "/clear":
if (_userMessages.ContainsKey(chatId)) {
_userMessages[chatId].Clear();
}
response = "✅ All messages cleared!";
break;
case "/generate":
if (!_userMessages.ContainsKey(chatId) || _userMessages[chatId].Count == 0)
{
response = "No messages to generate from. Send some task messages first!";
}
else {
GenerateExcelForUser(chatId, cancellationToken);
return;
}
break;
default:
response = "❌ Unknown command. Use /help to see available commands.";
break;
}
;
await _botClient.SendMessage(chatId: chatId,
text: response,
cancellationToken: cancellationToken
);
}
private async Task GenerateExcelForUser(long chatId, CancellationToken cancellationToken)
{
try
{
await _botClient.SendMessage(chatId: chatId, "⏳ Generating Excel file...", cancellationToken: cancellationToken);
var taskData = MessageParser.ParseMessages(_userMessages[chatId]);
if (taskData.Count == 0) {
await _botClient.SendMessage(chatId, "No valid task data found, check your message FORMAT.", cancellationToken: cancellationToken);
return;
}
var fileName = $"Tasks_{chatId}_{DateTime.Now:yyyyMMdd_HHmmss}.xlsx";
ExcelGenerator.GenerateExcel(taskData, fileName);
// send the file
using (var stream = System.IO.File.OpenRead(fileName)) {
InputFile file = InputFile.FromStream(stream, fileName);
await _botClient.SendDocument(chatId,file, caption: $"✅ Excel file created!\n\n" +
$"📊 {taskData.Count} tasks from {taskData.Select(t => t.Person).Distinct().Count()} people",
cancellationToken: cancellationToken);
}
System.IO.File.Delete(fileName);
await _botClient.SendMessage(
chatId: chatId,
text: "Want to add more data or start fresh? Use /clear to reset.",
cancellationToken: cancellationToken);
}
catch (Exception ex)
{
await _botClient.SendMessage(
chatId: chatId,
text: $"❌ Error generating Excel: {ex.Message}",
cancellationToken: cancellationToken);
}
}
private Task HandleErrorAsync(ITelegramBotClient botClient, Exception exception, CancellationToken cancellationToken)
{
Console.WriteLine($"Error occurred: {exception.Message}");
return Task.CompletedTask;
}
}
}