Adding minimal example for WebApplication and TelegramBot side-by-side
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
using TelegramBotBase.Base;
|
||||
using TelegramBotBase.Controls.Hybrid;
|
||||
using TelegramBotBase.Form;
|
||||
|
||||
namespace BotAndWebApplication.BotStuff
|
||||
{
|
||||
public class StartForm : FormBase
|
||||
{
|
||||
ButtonGrid _grid = null;
|
||||
|
||||
int MyCounter { get; set; } = 0;
|
||||
|
||||
public StartForm()
|
||||
{
|
||||
this.Opened += StartForm_Opened;
|
||||
}
|
||||
|
||||
private async Task StartForm_Opened(object sender, EventArgs e)
|
||||
{
|
||||
await Device.Send("Welcome to StartForm !");
|
||||
|
||||
var form = new ButtonForm();
|
||||
|
||||
form.AddButtonRow(new ButtonBase("Increase!", "increase"));
|
||||
|
||||
form.AddButtonRow(new ButtonBase("Decrease!", "decrease"));
|
||||
|
||||
_grid = new ButtonGrid(form);
|
||||
_grid.KeyboardType = TelegramBotBase.Enums.EKeyboardType.InlineKeyBoard;
|
||||
_grid.ButtonClicked += _grid_ButtonClicked;
|
||||
|
||||
_grid.Title = $"Increase your counter! (current {MyCounter})";
|
||||
|
||||
AddControl(_grid);
|
||||
}
|
||||
|
||||
private async Task _grid_ButtonClicked(object sender, TelegramBotBase.Args.ButtonClickedEventArgs e)
|
||||
{
|
||||
if (e.Button == null || e.Button.Value == null)
|
||||
return;
|
||||
|
||||
switch (e.Button.Value)
|
||||
{
|
||||
case "increase":
|
||||
|
||||
MyCounter++;
|
||||
|
||||
_grid.Title = $"Increase your counter! (current {MyCounter})";
|
||||
_grid.Updated();
|
||||
|
||||
break;
|
||||
case "decrease":
|
||||
|
||||
MyCounter--;
|
||||
|
||||
_grid.Title = $"Increase your counter! (current {MyCounter})";
|
||||
_grid.Updated();
|
||||
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
using TelegramBotBase;
|
||||
using TelegramBotBase.Builder;
|
||||
|
||||
namespace BotAndWebApplication.BotStuff.Tasks
|
||||
{
|
||||
public class BotBackgroundTask : IHostedService
|
||||
{
|
||||
BotBackgroundTask()
|
||||
{
|
||||
BotBaseInstance = BotBaseBuilder.Create()
|
||||
.WithAPIKey(Environment.GetEnvironmentVariable("API_KEY") ??
|
||||
throw new Exception("API_KEY is not set"))
|
||||
.DefaultMessageLoop()
|
||||
.WithStartForm<StartForm>()
|
||||
.NoProxy()
|
||||
.DefaultCommands()
|
||||
.NoSerialization()
|
||||
.UseEnglish()
|
||||
.Build();
|
||||
}
|
||||
|
||||
public BotBackgroundTask(ILogger<BotBackgroundTask> logger) : this()
|
||||
{
|
||||
Logger = logger;
|
||||
}
|
||||
|
||||
public ILogger<BotBackgroundTask>? Logger { get; }
|
||||
|
||||
public BotBase BotBaseInstance { get; private set; }
|
||||
|
||||
|
||||
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (BotBaseInstance == null)
|
||||
return;
|
||||
|
||||
Logger?.LogInformation("Bot is starting.");
|
||||
|
||||
await BotBaseInstance.UploadBotCommands();
|
||||
|
||||
await BotBaseInstance.Start();
|
||||
|
||||
|
||||
Logger?.LogInformation("Bot has been started.");
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (BotBaseInstance == null)
|
||||
return;
|
||||
|
||||
Logger?.LogInformation("Bot will shut down.");
|
||||
|
||||
//Let all users know that the bot will shut down.
|
||||
|
||||
await BotBaseInstance.SentToAll("Bot will shut down now.");
|
||||
|
||||
await BotBaseInstance.Stop();
|
||||
|
||||
Logger?.LogInformation("Bot has shutted down.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user