/// <summary> /// Ensures that the <see cref="BotRoutingContext.ProcessTimes"/> has at least one segment. /// </summary> public static void EnsureSegment(this BotRoutingContext context) { if (context.ProcessTimes.Count == 0) { context.RecordGenerationSegmentTime(); } }
/// <summary> /// Search (non-case-sensitive) for gravestones per commanded inline query request. /// </summary> public static IEnumerable <Gravestone> SearchByCommand(this BotRoutingContext context) { try { return(context.Search(new CommandInlineQuery(context.GetInlineQuery()).Query)); } catch (ArgumentOutOfRangeException) { return(context.GraveKeeper.Gravestones); } }
public override async Task ProcessAsync(BotRoutingContext context) { var results = context .SearchByCommand() .Select(x => InlineQueryResponseComposer.GetArticleResult(x, true)) .AddSearchTips(ResultType.SearchShort) .Take(50); context.RecordGenerationSegmentTime(); await context.AnswerInlineQueryAsync(results); }
public override async Task ProcessAsync(BotRoutingContext context) { var results = context .SearchByCommand() .OrderByDescending(x => x.DateClose - x.DateOpen) .Select(x => InlineQueryResponseComposer.GetArticleResult(x)) .AddSearchTips(ResultType.Oldest) .Take(50); context.RecordGenerationSegmentTime(); await context.AnswerInlineQueryAsync(results); }
/// <summary> /// Answer to an inline query request. Only works when <see cref="UpdateType"/> is <see cref="UpdateType.InlineQuery"/>. /// </summary> public static async Task AnswerInlineQueryAsync( this BotRoutingContext context, IEnumerable <InlineQueryResultBase> results) { context.EnsureSegment(); await context.BotClient.AnswerInlineQueryAsync( inlineQueryId : context.Update.InlineQuery.Id, results : results, isPersonal : false ); context.RecordSendingSegmentTime(); }
/// <summary> /// Send text reply to a message. Only works when <see cref="UpdateType"/> is <see cref="UpdateType.Message"/>. /// </summary> public static async Task ReplyTextMessageAsync( this BotRoutingContext context, string text, ParseMode parseMode = ParseMode.Default, bool disableWebPagePreview = false, bool disableNotification = false) { context.EnsureSegment(); await context.BotClient.SendTextMessageAsync( context.Update.Message.Chat.Id, text, parseMode, disableWebPagePreview, disableNotification, context.Update.Message.MessageId ); context.RecordSendingSegmentTime(); }
/// <summary> /// Get sender classification flags. /// </summary> public static SenderClassification GetSenderClassification(this BotRoutingContext context) { var result = SenderClassification.None; if (context.IsFromBot()) { result |= SenderClassification.Bot; } if (context.IsFromRealPerson()) { result |= SenderClassification.User; } if (context.IsFromAdmin()) { result |= SenderClassification.Admin; } return(result); }
public override async Task ProcessAsync(BotRoutingContext context) { if (context.IsFromAdmin()) { var sb = new StringBuilder(); sb.Append("Hello! You're seeing this message because you're recognized as an *administrator*.\n"); sb.Append($"Bot *version* is `{Program.Version} ({Program.InternalVersion})`\n"); sb.Append($"*Uptime*: `{DateTime.UtcNow:hh:mm}, up {Program.AppStopwatch.Elapsed:d' day(s) 'hh':'mm}`\n\n"); sb.Append("*Stats*:\n"); foreach (var tag in context.Stats.RegisteredSafeInts) { sb.Append($"- *{tag.DisplayName}* (`{tag.Id}`): {tag.SafeInt}\n"); } context.RecordGenerationSegmentTime(); await context.ReplyTextMessageAsync(sb.ToString(), ParseMode.Markdown); } else { await context.ReplyTextMessageAsync("Hello! Command features are not developed yet but stay tuned there!"); } }
/// <summary> /// Search (non-case-sensitive) for gravestones per inline query request. /// </summary> public static IEnumerable <Gravestone> SearchByFullQuery(this BotRoutingContext context) => context.Search(context.GetInlineQuery());
/// <summary> /// Checks if the sender is a bot. /// </summary> public static bool IsFromBot(this BotRoutingContext context) => context.Update.Message.From.IsBot;
/// <summary> /// Search (non-case-sensitive) for gravestones. /// </summary> public static IEnumerable <Gravestone> Search(this BotRoutingContext context, string content) => context.GraveKeeper.Gravestones .Where(x => x.Name .ToLowerInvariant() .Contains(content.ToLowerInvariant()));
/// <summary> /// Get inline query content. /// </summary> public static string GetInlineQuery(this BotRoutingContext context) => context.Update.InlineQuery.Query;
/// <summary> /// Get message sender's Telegram ID. /// </summary> public static long GetSenderId(this BotRoutingContext context) => context.Update.Message.From.Id;
/// <summary> /// Checks if the sender is an administrator. /// </summary> public static bool IsFromAdmin(this BotRoutingContext context) => context.ConfigManager.Config.AdminIds.Contains(context.GetSenderId());
/// <summary> /// Records a segment as message sending is complete. /// </summary> public static void RecordSendingSegmentTime(this BotRoutingContext context) => context.RecordSegmentTime("net");
/// <summary> /// Records a segment as response generation is complete. /// </summary> public static void RecordGenerationSegmentTime(this BotRoutingContext context) => context.RecordSegmentTime("gen");
/// <summary> /// Checks if the sender is a real person. /// </summary> public static bool IsFromRealPerson(this BotRoutingContext context) => !context.IsFromBot();