File tree Expand file tree Collapse file tree 6 files changed +88
-1
lines changed
RedmineTelegramBot.Standalone Expand file tree Collapse file tree 6 files changed +88
-1
lines changed Original file line number Diff line number Diff line change @@ -13,5 +13,7 @@ public class BotOptions
1313 public string RedmineUrl { get ; set ; }
1414
1515 public string RedmineSecret { get ; set ; }
16+
17+ public string DataDirectory { get ; set ; }
1618 }
1719}
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ namespace RedmineTelegramBot . Core . Data
8+ {
9+ public interface IUserSettingsRepository
10+ {
11+ void StoreSettings ( string username , UserSettings userSettings ) ;
12+
13+ UserSettings GetSettings ( string username ) ;
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+
7+ namespace RedmineTelegramBot . Core . Data
8+ {
9+ public class UserSettings
10+ {
11+ public string Username { get ; set ; }
12+
13+ public string RedmineSecret { get ; set ; }
14+ }
15+ }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
5+ using System . Text ;
6+ using System . Text . Json ;
7+ using System . Threading . Tasks ;
8+
9+ namespace RedmineTelegramBot . Core . Data
10+ {
11+ public class UsersSettingsFileRepository : IUserSettingsRepository
12+ {
13+ private readonly string _dataDirectoryPath ;
14+
15+ public UsersSettingsFileRepository ( string dataDirectoryPath )
16+ {
17+ _dataDirectoryPath = dataDirectoryPath ;
18+ }
19+
20+ public UserSettings GetSettings ( string username )
21+ {
22+ var path = MakePath ( username ) ;
23+ if ( File . Exists ( path ) )
24+ {
25+ var text = File . ReadAllText ( path ) ;
26+ return JsonSerializer . Deserialize < UserSettings > ( text ) ;
27+ }
28+
29+ return null ;
30+ }
31+
32+ public void StoreSettings ( string username , UserSettings userSettings )
33+ {
34+ var path = MakePath ( username ) ;
35+ var json = JsonSerializer . Serialize ( userSettings ) ;
36+ File . WriteAllText ( path , json ) ;
37+ }
38+
39+ private string MakePath ( string username )
40+ {
41+ if ( ! Directory . Exists ( _dataDirectoryPath ) )
42+ {
43+ Directory . CreateDirectory ( _dataDirectoryPath ) ;
44+ }
45+
46+ return Path . Combine ( _dataDirectoryPath , $ "user_settings_{ username } .json") ;
47+ }
48+ }
49+ }
Original file line number Diff line number Diff line change 11using Microsoft . Extensions . DependencyInjection ;
22using RedmineTelegramBot . Core . Config ;
3+ using RedmineTelegramBot . Core . Data ;
34using Telegram . Bot ;
45
56namespace RedmineTelegramBot . Core . Modules
@@ -15,6 +16,10 @@ public RedmineBotModule(BotOptions configuration)
1516
1617 public void Load ( IServiceCollection services )
1718 {
19+ // repositories
20+ services . AddTransient < IUserSettingsRepository > ( c => new UsersSettingsFileRepository ( _options . DataDirectory ) ) ;
21+
22+ // app services
1823 services . AddTransient < IRedmineBot , RedmineBot > ( ) ;
1924 services . AddSingleton < ITelegramBotClient > ( c => new TelegramBotClient ( _options . Token ) ) ;
2025 services . AddTransient < IConversationFactory , ConversationFactory > ( ) ;
Original file line number Diff line number Diff line change 22 "TelegramBot" : {
33 "Token" : " " ,
44 "RedmineUrl" : " " ,
5- "RedmineSecret" : " "
5+ "RedmineSecret" : " " ,
6+ "DataDirectory" : " "
67 }
78}
You can’t perform that action at this time.
0 commit comments