35 lines
1.1 KiB
C#
35 lines
1.1 KiB
C#
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Telegram.Bot.SpaceApi.Backend;
|
|
using Telegram.Bot.SpaceApi.Backend.Database;
|
|
|
|
namespace Telegram.Bot.SpaceApi.Pages;
|
|
|
|
public class IndexModel : PageModel {
|
|
public void OnGet() { }
|
|
|
|
public async void OnPost() {
|
|
Response.Redirect("/");
|
|
|
|
await using var db = new DatabaseContext();
|
|
if (Request.Form["action"] == "add_bot") {
|
|
var bot = new Backend.Database.Tables.Bot {
|
|
Name = Request.Form["bot_name"]!, Token = Request.Form["bot_token"]!, ApiUrl = Request.Form["api_url"]!, Chat = Request.Form["bot_chat"]!
|
|
};
|
|
|
|
if (Request.Form.ContainsKey("api_override_address") && !string.IsNullOrWhiteSpace(Request.Form["api_override_address"])) {
|
|
bot.ApiOverrideAddress = Request.Form["api_override_address"]!;
|
|
}
|
|
|
|
db.Add(bot);
|
|
await db.SaveChangesAsync();
|
|
await Task.WhenAny(Task.Run(() => BotOrchestrator.AddBot(bot)), Task.Delay(1000)).Result;
|
|
}
|
|
else if (Request.Form["action"] == "delete_bot") {
|
|
var bot = await db.Bots.FindAsync(int.Parse(Request.Form["bot_id"]!));
|
|
BotOrchestrator.DelBot(bot!);
|
|
db.Remove(bot!);
|
|
await db.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|