Skip to content

Commit

Permalink
Merge pull request #53 from MajMcCloud/development
Browse files Browse the repository at this point in the history
Integrating development branch into master
  • Loading branch information
MajMcCloud committed Dec 6, 2023
2 parents 0cb4f02 + f401216 commit 2a873cc
Show file tree
Hide file tree
Showing 22 changed files with 381 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Examples/DependencyInjection/Database/BotDbContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using Microsoft.EntityFrameworkCore;

namespace DependencyInjection.Database;

public class BotDbContext : DbContext
{
public BotDbContext(DbContextOptions options) : base(options)
{
}

public DbSet<User> Users { get; set; }
}
7 changes: 7 additions & 0 deletions Examples/DependencyInjection/Database/User.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace DependencyInjection.Database;

public class User
{
public long Id { get; set; }
public string LastMessage { get; set; }
}
19 changes: 19 additions & 0 deletions Examples/DependencyInjection/DependencyInjection.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.11" />
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.11" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\TelegramBotBase\TelegramBotBase.csproj" />
</ItemGroup>

</Project>
67 changes: 67 additions & 0 deletions Examples/DependencyInjection/Forms/ConfirmationForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using DependencyInjection.Database;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.DependencyInjection;

namespace DependencyInjection.Forms
{
public class ConfirmationForm : FormBase
{
private readonly BotDbContext _dbContext;

public ConfirmationForm(BotDbContext dbContext)
{
_dbContext = dbContext;
}

public override async Task Load(MessageResult message)
{
var user = await _dbContext.Users.FindAsync(Device.DeviceId);

if (user == null)
{
await this.NavigateTo<StartForm>();
return;
}
}

public override async Task Action(MessageResult message)
{
await message.ConfirmAction("Go back");

switch (message.RawData)
{

case "back":

await this.NavigateTo<StartForm>();

break;

}


}

public override async Task Render(MessageResult message)
{
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
if (user == null)
return;

var bf = new ButtonForm();
bf.AddButtonRow("Back", "back");

await Device.Send($"ConfirmationForm: Your last message was: {user.LastMessage}. Click \"Back\" to get back.", bf);
}




}
}
35 changes: 35 additions & 0 deletions Examples/DependencyInjection/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using DependencyInjection.Database;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using TelegramBotBase.Builder;

namespace DependencyInjection
{
internal class Program
{
static async Task Main(string[] args)
{


var serviceCollection = new ServiceCollection()
.AddDbContext<BotDbContext>(x => x.UseInMemoryDatabase("TelegramBotBase"));

var serviceProvider = serviceCollection.BuildServiceProvider();

var bot = BotBaseBuilder.Create()
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ??
throw new Exception("API_KEY is not set"))
.DefaultMessageLoop()
.WithServiceProvider<StartForm>(serviceProvider)
.NoProxy()
.NoCommands()
.NoSerialization()
.DefaultLanguage()
.Build();

await bot.Start();
await Task.Delay(-1);

}
}
}
79 changes: 79 additions & 0 deletions Examples/DependencyInjection/StartForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using DependencyInjection.Database;
using DependencyInjection.Forms;
using TelegramBotBase.Base;
using TelegramBotBase.Form;
using TelegramBotBase.DependencyInjection;

namespace DependencyInjection;

public class StartForm : FormBase
{
private readonly BotDbContext _dbContext;

public StartForm(BotDbContext dbContext)
{
_dbContext = dbContext;
}

public override async Task Load(MessageResult message)
{
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
if (user is null)
{
user = new User
{
Id = Device.DeviceId,
LastMessage = "<unknown>"
};

_dbContext.Users.Add(user);
await _dbContext.SaveChangesAsync();
}

if (message.IsAction)
return;


user.LastMessage = string.IsNullOrWhiteSpace(message.MessageText) ? "<unknown>" : message.MessageText;
await _dbContext.SaveChangesAsync();
}

public override async Task Action(MessageResult message)
{
await message.ConfirmAction("Ok");

switch(message.RawData)
{

case "open":

await this.NavigateTo(typeof(ConfirmationForm));

var new_form = await this.NavigateTo<ConfirmationForm>();

if (new_form == null)
{
await Device.Send("Cant open ConfirmationForm");
}

break;

}


}

public override async Task Render(MessageResult message)
{
var user = await _dbContext.Users.FindAsync(Device.DeviceId);
if (user == null)
return;

var bf = new ButtonForm();

bf.AddButtonRow("Open confirmation", "open");

await Device.Send($"Your last message's text was: `{user.LastMessage}`", bf);
}

}
25 changes: 25 additions & 0 deletions Examples/InlineAndReplyCombination/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ static async Task Main(string[] args)

await BotBaseInstance.UploadBotCommands();

BotBaseInstance.BotCommand += BotBaseInstance_BotCommand;


await BotBaseInstance.Start();

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

await BotBaseInstance.Stop();
}

private static async Task BotBaseInstance_BotCommand(object sender, TelegramBotBase.Args.BotCommandEventArgs e)
{

switch(e.Command)
{
case "/start":


var start = new StartForm();

await e.Device.ActiveForm.NavigateTo(start);


break;



}



}
}
}
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# .NET Telegram Bot Framework - Context based addon

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

[![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)
Expand Down Expand Up @@ -1083,3 +1083,8 @@ Having already a web application and want to add a TelegramBot side-by-side with
Want to use Inline- and ReplyMarkup at the same time ? Here is an example how you can do that:

- [Examples/InlineAndReplyCombination](Examples/InlineAndReplyCombination)


Alpha: Full Dependency Injection example within this framework.

- [Examples/DependencyInjection](Examples/DependencyInjection)
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public MssqlSerializer(string connectionString, string tablePrefix = "tgb_", Typ

if (FallbackStateForm != null && !FallbackStateForm.IsSubclassOf(typeof(FormBase)))
{
throw new ArgumentException("FallbackStateForm is not a subclass of FormBase");
throw new ArgumentException($"{nameof(FallbackStateForm)} is not a subclass of {nameof(FormBase)}");
}
}

Expand Down
2 changes: 2 additions & 0 deletions TelegramBotBase/Base/FormBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class FormBase : IDisposable

public MessageClient Client { get; set; }

IServiceProvider _serviceProvider = null;

/// <summary>
/// has this formular already been disposed ?
/// </summary>
Expand Down
5 changes: 4 additions & 1 deletion TelegramBotBase/Builder/BotBaseBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ private BotBaseBuilder()
/// </summary>
private Dictionary<BotCommandScope, List<BotCommand>> BotCommandScopes { get; } = new();


/// <summary>
/// Creates a full BotBase instance with all parameters previously set.
/// </summary>
/// <returns></returns>
public BotBase Build()
{
var bot = new BotBase(_apiKey, _client)
Expand Down
4 changes: 4 additions & 0 deletions TelegramBotBase/Builder/Interfaces/IBuildingStage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,9 @@

public interface IBuildingStage
{
/// <summary>
/// Creates a full BotBase instance with all parameters previously set.
/// </summary>
/// <returns></returns>
BotBase Build();
}

0 comments on commit 2a873cc

Please sign in to comment.