-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathYourBotController.cs
31 lines (26 loc) · 1.1 KB
/
YourBotController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#if ASPNETCORE // in a Webhook setup, remove this #if, and YourBot.Main method, and place this file in your Controllers folder
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace YourEasyBot.Controllers
{
// This is a quick & dirty ASP.NET Core controller. In a serious environment, you would use IConfiguration, ILogger and a Singleton
// (teaching you how to correctly build an ASP.NET website is beyond the scope of this example)
// Visit the controller base URL in a browser at least once to register the webhook
[ApiController][Route("[controller]")]
public class YourBotController : ControllerBase
{
const string BOT_TOKEN = "PASTE_YOUR_BOT_TOKEN_HERE";
static YourBot bot;
public YourBotController() => bot ??= new YourBot(BOT_TOKEN);
[HttpGet]
public Task<string> Get() => bot.CheckWebhook($"{Request.Scheme}://{Request.Host}{Request.Path}?token={BOT_TOKEN}");
[HttpPost]
public IActionResult Post(string token, [FromBody] Telegram.Bot.Types.Update update)
{
if (token != BOT_TOKEN) return Forbid();
bot.HandleUpdate(update);
return Ok();
}
}
}
#endif