-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from MajMcCloud/development
Integrating development branch into master
- Loading branch information
Showing
22 changed files
with
381 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.