Compare commits
2 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| d60bef7070 | |||
| 7556b3d638 |
@@ -17,8 +17,6 @@ internal sealed class CommandsRegistry
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public CommandsRegistry Map<TPage>(string commandTemplate, bool publish = false, string? description = null) where TPage : Page
|
public CommandsRegistry Map<TPage>(string commandTemplate, bool publish = false, string? description = null) where TPage : Page
|
||||||
{
|
{
|
||||||
var pattern = ToRegex(commandTemplate);
|
|
||||||
|
|
||||||
return Map(commandTemplate, (ctx, args, ct) => ctx.Navigation.GoToAsync<TPage>(ctx, ct), publish, description);
|
return Map(commandTemplate, (ctx, args, ct) => ctx.Navigation.GoToAsync<TPage>(ctx, ct), publish, description);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -67,15 +65,25 @@ internal sealed class CommandsRegistry
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private static Regex ToRegex(string template)
|
private static Regex ToRegex(string template)
|
||||||
{
|
{
|
||||||
var escaped = Regex.Escape(template)
|
// Заменяем все {name} и {name?} на регулярные группы
|
||||||
.Replace("\\{", "{").Replace("\\}", "}");
|
var pattern = "^" + Regex.Replace(template, @"\s*\{(\w+)(\?)?\}", m =>
|
||||||
|
|
||||||
var pattern = "^" + Regex.Replace(escaped, @"\{(\w+)(\?)?\}", m =>
|
|
||||||
{
|
{
|
||||||
var name = m.Groups[1].Value;
|
var name = m.Groups[1].Value;
|
||||||
var optional = m.Groups[2].Success;
|
var optional = m.Groups[2].Success;
|
||||||
return optional ? $"(?<{name}>\\S+)?" : $"(?<{name}>\\S+)";
|
|
||||||
}) + "$";
|
var argPattern = $"(?:\"(?<{name}>[^\"]+)\"|(?<{name}>\\S+))";
|
||||||
|
|
||||||
|
if (optional)
|
||||||
|
{
|
||||||
|
// необязательный параметр: пробел + значение целиком необязательны
|
||||||
|
return $"(?:\\s+{argPattern})?";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// обязательный параметр: пробел обязателен
|
||||||
|
return $"\\s+{argPattern}";
|
||||||
|
}
|
||||||
|
}) + "\\s*$"; // допускаем пробелы/переносы в конце
|
||||||
|
|
||||||
return new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
return new Regex(pattern, RegexOptions.Compiled | RegexOptions.IgnoreCase);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,6 +66,10 @@ public sealed class TelegramAdapter : IMessangerAdapterSetup
|
|||||||
var mapped = TelegramUpdateMapper.Map(MessengerType, update, _client);
|
var mapped = TelegramUpdateMapper.Map(MessengerType, update, _client);
|
||||||
if (mapped is not null)
|
if (mapped is not null)
|
||||||
await onUpdate(mapped);
|
await onUpdate(mapped);
|
||||||
|
if (update.CallbackQuery is not null)
|
||||||
|
{
|
||||||
|
await _.AnswerCallbackQuery(update.CallbackQuery.Id);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
errorHandler: async (_, ex, ct2) =>
|
errorHandler: async (_, ex, ct2) =>
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ public sealed class DetailsPage : StatefullPage<string>
|
|||||||
await ctx.Navigation.GoToAsync<FilesPage>(ctx, ct);
|
await ctx.Navigation.GoToAsync<FilesPage>(ctx, ct);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string Command => "/create_request {title}";
|
internal static string Command => "/create_request {title?}";
|
||||||
internal static string CommandDescription => "создание заявки /create_request {title";
|
internal static string CommandDescription => "создание заявки /create_request {title}";
|
||||||
internal static CommandHandler CommandHandler = async (ctx, args, ct) =>
|
internal static CommandHandler CommandHandler = async (ctx, args, ct) =>
|
||||||
{
|
{
|
||||||
string? title = "";
|
string? title = "";
|
||||||
|
|||||||
Reference in New Issue
Block a user