Skip to content

Commit 80b3650

Browse files
committed
added connection string support and removed dependency to bot username in the configuration
1 parent 458efc2 commit 80b3650

File tree

5 files changed

+109
-9
lines changed

5 files changed

+109
-9
lines changed

Deployf.Botf/BotfOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public string? Username
1616
}
1717

1818
public string? WebhookUrl { get; set; }
19-
public bool AutoSend { get; set; }
19+
public bool AutoSend { get; set; } = true;
2020
public bool HandleOnlyMentionedInGroups { get; set; }
2121
public string ApiBaseUrl { get; set; }
2222

Deployf.Botf/BotfProgram.cs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ public static void StartBot(
66
string[] args,
77
bool skipHello = false,
88
Action<IServiceCollection, IConfiguration>? onConfigure = null,
9-
Action<IApplicationBuilder, IConfiguration>? onRun = null)
9+
Action<IApplicationBuilder, IConfiguration>? onRun = null,
10+
BotfOptions options = null)
1011
{
1112
if (!skipHello)
1213
{
@@ -22,7 +23,19 @@ public static void StartBot(
2223

2324
var builder = WebApplication.CreateBuilder(args);
2425

25-
var botOptions = builder.Configuration.GetSection("bot").Get<BotfOptions>();
26+
var botOptions = options;
27+
28+
if(botOptions == null && builder.Configuration["bot"] != null)
29+
{
30+
botOptions = builder.Configuration.GetSection("bot").Get<BotfOptions>();
31+
}
32+
33+
var connectionString = builder.Configuration["botf"];
34+
if (botOptions == null && connectionString != null)
35+
{
36+
botOptions = ConnectionString.Parse(connectionString);
37+
}
38+
2639
builder.Services.AddBotf(botOptions);
2740
builder.Services.AddHttpClient();
2841

Deployf.Botf/ConnectionString.cs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
namespace Deployf.Botf;
2+
3+
public class ConnectionString
4+
{
5+
/// <summary>
6+
/// token?key=value&key=value...
7+
/// </summary>
8+
/// <param name="value"></param>
9+
/// <returns></returns>
10+
/// <exception cref="ArgumentNullException"></exception>
11+
/// <exception cref="Exception"></exception>
12+
public static BotfOptions Parse(string value)
13+
{
14+
if(string.IsNullOrEmpty(value))
15+
{
16+
throw new ArgumentNullException("value");
17+
}
18+
19+
var options = new BotfOptions();
20+
21+
var main = value.Split('?', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
22+
23+
if(main.Length > 0)
24+
{
25+
options.Token = main[0];
26+
}
27+
else
28+
{
29+
throw new ArgumentException();
30+
}
31+
32+
if(main.Length == 1)
33+
{
34+
return options;
35+
}
36+
37+
var values = main[1].Split('&', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
38+
foreach (var kv in values)
39+
{
40+
var cortage = kv.Split('=', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
41+
if(cortage == null || cortage.Length != 2)
42+
{
43+
throw new Exception("Botf connection string is wrong");
44+
}
45+
46+
switch (cortage[0])
47+
{
48+
case "botname":
49+
options.Username = cortage[1];
50+
break;
51+
case "autosend":
52+
if(bool.TryParse(cortage[1], out var autosend))
53+
{
54+
options.AutoSend = autosend;
55+
}
56+
break;
57+
case "group_mode":
58+
if (bool.TryParse(cortage[1], out var groupMode))
59+
{
60+
options.HandleOnlyMentionedInGroups = groupMode;
61+
}
62+
break;
63+
case "webhook":
64+
options.WebhookUrl = cortage[1];
65+
break;
66+
case "api":
67+
options.ApiBaseUrl = cortage[1];
68+
break;
69+
}
70+
}
71+
72+
return options;
73+
}
74+
}

Deployf.Botf/StartupExtensions.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,25 @@ public static IApplicationBuilder UseBotf(this IApplicationBuilder app, Action<B
5757
return app;
5858
}
5959

60+
public static IServiceCollection AddBotf(this IServiceCollection services, string connectionString)
61+
{
62+
var options = ConnectionString.Parse(connectionString);
63+
services.AddBotf(options);
64+
return services;
65+
}
66+
6067
public static IServiceCollection AddBotf(this IServiceCollection services, BotfOptions options)
6168
{
69+
if(string.IsNullOrEmpty(options.Username))
70+
{
71+
var telegramClient = new TelegramBotClient(options.Token, baseUrl: options.ApiBaseUrl);
72+
var botUser = telegramClient.GetMeAsync()
73+
.ConfigureAwait(false)
74+
.GetAwaiter()
75+
.GetResult();
76+
options.Username = botUser.Username;
77+
}
78+
6279
var routes = BotControllerFactory.MakeRoutes(RouteStateSkipFunction.SkipFunctionFactory);
6380

6481
var states = BotControllerFactory.MakeStates();

README.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ class Program : BotfProgram
6969
PushL("You know.. it's very hard to recognize your command!");
7070
PushL("Please, write a correct text. Or use /start command");
7171

72-
// And finally, send buffered message
72+
// And finally send buffered message
7373
await Send();
7474
}
7575
}
@@ -79,11 +79,7 @@ And replace content of `appsettings.json` with your bot username and token:
7979

8080
```
8181
{
82-
"bot": {
83-
"Token": "123456778990:YourToken",
84-
"Username": "username_bot",
85-
"AutoSend": true
86-
}
82+
"botf": "123456778990:YourToken"
8783
}
8884
```
8985

0 commit comments

Comments
 (0)