-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (75 loc) · 3.22 KB
/
Program.cs
File metadata and controls
85 lines (75 loc) · 3.22 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
using System.Text.RegularExpressions;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InlineQueryResults;
const int MaxEntries = 20;
// replace YOUR_BOT_TOKEN below, or set your TOKEN in Project Properties > Debug > Launch profiles UI > Environment variables
var token = Environment.GetEnvironmentVariable("TOKEN") ?? "YOUR_BOT_TOKEN";
// load all articles, sorted alphabetically, in memory (not optimal if you have a huge base of articles)
var files = Directory.GetFiles("Articles", "*.html");
Array.Sort(files, StringComparer.CurrentCultureIgnoreCase);
var articleNames = new string[files.Length];
<
7462
div id="LC16" class="react-file-line html-div" data-testid="code-cell" data-line-number="16" style="position:relative">var articleContents = new string[files.Length];
for (int i = 0; i < files.Length; i++)
{
articleNames[i] = Path.GetFileNameWithoutExtension(files[i]);
articleContents[i] = System.IO.File.ReadAllText(files[i]);
}
// start the bot
using var cts = new CancellationTokenSource();
var bot = new TelegramBotClient(token, cancellationToken: cts.Token);
var me = await bot.GetMe();
bot.OnUpdate += OnUpdate;
Console.WriteLine($"Start listening for @{me.Username}");
Console.ReadLine();
cts.Cancel(); // stop the bot
async Task OnUpdate(Update update)
{
try
{
switch (update.Type)
{
case UpdateType.InlineQuery: await OnInlineQuery(bot, update.InlineQuery!); break;
case UpdateType.ChosenInlineResult: await OnChosenInlineResult(bot, update.ChosenInlineResult!); break;
};
}
#pragma warning disable CA1031
catch (Exception ex)
{
Console.WriteLine($"Exception while handling {update.Type}: {ex}");
}
#pragma warning restore CA1031
}
// for this method to be called, you need to enable "Inline mode" on in BotFather
async Task OnInlineQuery(ITelegramBotClient botClient, InlineQuery inlineQuery)
{
var results = new List<InlineQueryResult>();
// find and return articles that starts with the keyword
var index = Array.BinarySearch(articleNames, inlineQuery.Query, StringComparer.CurrentCultureIgnoreCase);
if (index < 0) index = ~index;
while (index < articleNames.Length && results.Count < MaxEntries &&
articleNames[index].StartsWith(inlineQuery.Query, StringComparison.CurrentCultureIgnoreCase))
{
results.Add(
new InlineQueryResultArticle($"{index}",
articleNames[index],
new InputTextMessageContent(articleContents[index])
{
ParseMode = ParseMode.Html // content is in HTML formatting
})
{
Description = Regex.Replace(articleContents[index], "<.*?>", "") // results will show beginning of article contents (without HTML tags)
});
index++;
}
await botClient.AnswerInlineQuery(inlineQuery.Id, results);
}
// for this method to be called, you need to enable "Inline feedback" in BotFather (100% if you want to know all your users choices)
async Task OnChosenInlineResult(ITelegramBotClient botClient, ChosenInlineResult chosenInlineResult)
{
if (uint.TryParse(chosenInlineResult.ResultId, out uint index) && index < articleNames.Length)
{
Console.WriteLine($"User {chosenInlineResult.From} has selected article: {articleNames[index]}");
}
}