Skip to content

Commit 988b048

Browse files
committed
Enhance text command processing in TextCommandHandler.cs
- Add System.Linq namespace for LINQ functionalities. - Use StringSplitOptions.RemoveEmptyEntries in Split method. - Implement handling for quoted text within commands. - Filter out empty or whitespace parts after processing. - Add early return conditions for empty parts array.
1 parent 4a7eee7 commit 988b048

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

Sources/TelegramBot/Handlers/TextCommandHandler.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using TelegramBot.Helpers;
44
using TelegramBot.Attributes;
55
using System.Collections.Generic;
6+
using System.Linq;
67

78
namespace TelegramBot.Handlers
89
{
@@ -26,7 +27,35 @@ public TextCommandHandler(IReadOnlyCollection<MethodInfo> controllerMethods, Upd
2627
return null;
2728
}
2829
var message = update.Message;
29-
var parts = message.Text.Split(' ');
30+
// /mail Vadik vadbex@gmail.com Test "Hello! It's me..."
31+
string[] parts = message.Text.Split(' ', System.StringSplitOptions.RemoveEmptyEntries);
32+
if (parts.Length == 0)
33+
{
34+
return null;
35+
}
36+
// if part is quoted, then join it with the next part
37+
for (int i = 0; i < parts.Length; i++)
38+
{
39+
if (parts[i].StartsWith('"'))
40+
{
41+
for (int j = i + 1; j < parts.Length; j++)
42+
{
43+
if (parts[j].EndsWith('"'))
44+
{
45+
parts[i] = parts[i][1..] + " " + parts[j][..^1];
46+
parts[j] = string.Empty;
47+
break;
48+
}
49+
}
50+
}
51+
}
52+
parts = parts
53+
.Where(p => !string.IsNullOrWhiteSpace(p))
54+
.ToArray();
55+
if (parts.Length == 0)
56+
{
57+
return null;
58+
}
3059
string command = parts[0];
3160
if (!command.StartsWith('/'))
3261
{

0 commit comments

Comments
 (0)