Skip to content

Commit 2a873cc

Browse files
authored
Merge pull request #53 from MajMcCloud/development
Integrating development branch into master
2 parents 0cb4f02 + f401216 commit 2a873cc

File tree

22 files changed

+381
-12
lines changed

22 files changed

+381
-12
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using Microsoft.EntityFrameworkCore;
2+
3+
namespace DependencyInjection.Database;
4+
5+
public class BotDbContext : DbContext
6+
{
7+
public BotDbContext(DbContextOptions options) : base(options)
8+
{
9+
}
10+
11+
public DbSet<User> Users { get; set; }
12+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace DependencyInjection.Database;
2+
3+
public class User
4+
{
5+
public long Id { get; set; }
6+
public string LastMessage { get; set; }
7+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net7.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
17+
</ItemGroup>
18+
19+
</Project>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using DependencyInjection.Database;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using TelegramBotBase.Base;
8+
using TelegramBotBase.Form;
9+
using TelegramBotBase.DependencyInjection;
10+
11+
namespace DependencyInjection.Forms
12+
{
13+
public class ConfirmationForm : FormBase
14+
{
15+
private readonly BotDbContext _dbContext;
16+
17+
public ConfirmationForm(BotDbContext dbContext)
18+
{
19+
_dbContext = dbContext;
20+
}
21+
22+
public override async Task Load(MessageResult message)
23+
{
24+
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
25+
26+
if (user == null)
27+
{
28+
await this.NavigateTo<StartForm>();
29+
return;
30+
}
31+
}
32+
33+
public override async Task Action(MessageResult message)
34+
{
35+
await message.ConfirmAction("Go back");
36+
37+
switch (message.RawData)
38+
{
39+
40+
case "back":
41+
42+
await this.NavigateTo<StartForm>();
43+
44+
break;
45+
46+
}
47+
48+
49+
}
50+
51+
public override async Task Render(MessageResult message)
52+
{
53+
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
54+
if (user == null)
55+
return;
56+
57+
var bf = new ButtonForm();
58+
bf.AddButtonRow("Back", "back");
59+
60+
await Device.Send($"ConfirmationForm: Your last message was: {user.LastMessage}. Click \"Back\" to get back.", bf);
61+
}
62+
63+
64+
65+
66+
}
67+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using DependencyInjection.Database;
2+
using Microsoft.EntityFrameworkCore;
3+
using Microsoft.Extensions.DependencyInjection;
4+
using TelegramBotBase.Builder;
5+
6+
namespace DependencyInjection
7+
{
8+
internal class Program
9+
{
10+
static async Task Main(string[] args)
11+
{
12+
13+
14+
var serviceCollection = new ServiceCollection()
15+
.AddDbContext<BotDbContext>(x => x.UseInMemoryDatabase("TelegramBotBase"));
16+
17+
var serviceProvider = serviceCollection.BuildServiceProvider();
18+
19+
var bot = BotBaseBuilder.Create()
20+
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ??
21+
throw new Exception("API_KEY is not set"))
22+
.DefaultMessageLoop()
23+
.WithServiceProvider<StartForm>(serviceProvider)
24+
.NoProxy()
25+
.NoCommands()
26+
.NoSerialization()
27+
.DefaultLanguage()
28+
.Build();
29+
30+
await bot.Start();
31+
await Task.Delay(-1);
32+
33+
}
34+
}
35+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
using DependencyInjection.Database;
2+
using DependencyInjection.Forms;
3+
using TelegramBotBase.Base;
4+
using TelegramBotBase.Form;
5+
using TelegramBotBase.DependencyInjection;
6+
7+
namespace DependencyInjection;
8+
9+
public class StartForm : FormBase
10+
{
11+
private readonly BotDbContext _dbContext;
12+
13+
public StartForm(BotDbContext dbContext)
14+
{
15+
_dbContext = dbContext;
16+
}
17+
18+
public override async Task Load(MessageResult message)
19+
{
20+
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
21+
if (user is null)
22+
{
23+
user = new User
24+
{
25+
Id = Device.DeviceId,
26+
LastMessage = "<unknown>"
27+
};
28+
29+
_dbContext.Users.Add(user);
30+
await _dbContext.SaveChangesAsync();
31+
}
32+
33+
if (message.IsAction)
34+
return;
35+
36+
37+
user.LastMessage = string.IsNullOrWhiteSpace(message.MessageText) ? "<unknown>" : message.MessageText;
38+
await _dbContext.SaveChangesAsync();
39+
}
40+
41+
public override async Task Action(MessageResult message)
42+
{
43+
await message.ConfirmAction("Ok");
44+
45+
switch(message.RawData)
46+
{
47+
48+
case "open":
49+
50+
await this.NavigateTo(typeof(ConfirmationForm));
51+
52+
var new_form = await this.NavigateTo<ConfirmationForm>();
53+
54+
if (new_form == null)
55+
{
56+
await Device.Send("Cant open ConfirmationForm");
57+
}
58+
59+
break;
60+
61+
}
62+
63+
64+
}
65+
66+
public override async Task Render(MessageResult message)
67+
{
68+
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
69+
if (user == null)
70+
return;
71+
72+
var bf = new ButtonForm();
73+
74+
bf.AddButtonRow("Open confirmation", "open");
75+
76+
await Device.Send($"Your last message's text was: `{user.LastMessage}`", bf);
77+
}
78+
79+
}

Examples/InlineAndReplyCombination/Program.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ static async Task Main(string[] args)
2727

2828
await BotBaseInstance.UploadBotCommands();
2929

30+
BotBaseInstance.BotCommand += BotBaseInstance_BotCommand;
31+
3032

3133
await BotBaseInstance.Start();
3234

@@ -37,5 +39,28 @@ static async Task Main(string[] args)
3739

3840
await BotBaseInstance.Stop();
3941
}
42+
43+
private static async Task BotBaseInstance_BotCommand(object sender, TelegramBotBase.Args.BotCommandEventArgs e)
44+
{
45+
46+
switch(e.Command)
47+
{
48+
case "/start":
49+
50+
51+
var start = new StartForm();
52+
53+
await e.Device.ActiveForm.NavigateTo(start);
54+
55+
56+
break;
57+
58+
59+
60+
}
61+
62+
63+
64+
}
4065
}
4166
}

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# .NET Telegram Bot Framework - Context based addon
22

3-
[![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/v/TelegramBotBase.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase/)
3+
[![NuGet version (TelegramBotBase)](https://img.shields.io/nuget/vpre/TelegramBotBase.svg?style=flat-square)](https://www.nuget.org/packages/TelegramBotBase/)
44
[![Telegram chat](https://img.shields.io/badge/Support_Chat-Telegram-blue.svg?style=flat-square)](https://www.t.me/tgbotbase)
55

66
[![License](https://img.shields.io/github/license/MajMcCloud/telegrambotframework.svg?style=flat-square&maxAge=2592000&label=License)](https://raw.githubusercontent.com/MajMcCloud/TelegramBotFramework/master/LICENCE.md)
@@ -1083,3 +1083,8 @@ Having already a web application and want to add a TelegramBot side-by-side with
10831083
Want to use Inline- and ReplyMarkup at the same time ? Here is an example how you can do that:
10841084

10851085
- [Examples/InlineAndReplyCombination](Examples/InlineAndReplyCombination)
1086+
1087+
1088+
Alpha: Full Dependency Injection example within this framework.
1089+
1090+
- [Examples/DependencyInjection](Examples/DependencyInjection)

TelegramBotBase.Extensions.Serializer.Database.MSSQL/MSSQLSerializer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public MssqlSerializer(string connectionString, string tablePrefix = "tgb_", Typ
3131

3232
if (FallbackStateForm != null && !FallbackStateForm.IsSubclassOf(typeof(FormBase)))
3333
{
34-
throw new ArgumentException("FallbackStateForm is not a subclass of FormBase");
34+
throw new ArgumentException($"{nameof(FallbackStateForm)} is not a subclass of {nameof(FormBase)}");
3535
}
3636
}
3737

TelegramBotBase/Base/FormBase.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class FormBase : IDisposable
3232

3333
public MessageClient Client { get; set; }
3434

35+
IServiceProvider _serviceProvider = null;
36+
3537
/// <summary>
3638
/// has this formular already been disposed ?
3739
/// </summary>

0 commit comments

Comments
 (0)