diff --git a/Controllers/ExampleController.cs b/Controllers/ExampleController.cs
index 781ff24..b209d80 100644
--- a/Controllers/ExampleController.cs
+++ b/Controllers/ExampleController.cs
@@ -2,10 +2,23 @@ using Microsoft.AspNetCore.Mvc;
namespace TG_Bot_Template.Controllers
{
+ ///
+ /// Example controller
+ ///
[ApiController]
[Route("[controller]")]
public class ExampleController : ControllerBase
{
-
+ ///
+ /// Just example endpoing
+ ///
+ ///
+ [HttpGet("ping")]
+ [Produces("application/json")]
+ [ProducesResponseType(200)]
+ public async Task Pong()
+ {
+ return Ok("Pong!");
+ }
}
}
diff --git a/Program.cs b/Program.cs
index e676b43..b593410 100644
--- a/Program.cs
+++ b/Program.cs
@@ -1,15 +1,25 @@
+using Microsoft.AspNetCore;
+
namespace TG_Bot_Template
{
public class Program
{
- public static void Main(string[] args)
+ public static void Main()
{
- new WebHostBuilder()
- .UseKestrel(k => k.ListenAnyIP(80))
- .UseStartup()
+ CreateHostBuilder()
.Build()
.Run();
}
+ private static IHostBuilder CreateHostBuilder()
+ {
+ return Host.CreateDefaultBuilder()
+ .ConfigureWebHostDefaults(webHost => {
+ webHost.UseStartup();
+ webHost.UseStaticWebAssets();
+ webHost.UseKestrel(kestrelOptions => { kestrelOptions.ListenAnyIP(80); });
+ });
+
+ }
}
}
diff --git a/Startup.cs b/Startup.cs
index 6650ef4..e4c7174 100644
--- a/Startup.cs
+++ b/Startup.cs
@@ -3,52 +3,54 @@ using System;
namespace TG_Bot_Template
{
+ ///
+ /// Startup class for ASP.NET with setting and configuration
+ ///
public class Startup
{
- public class Startup
- {
- private readonly IConfiguration configuration;
+
+ private readonly IConfiguration configuration;
- public Startup()
+ public Startup()
+ {
+ configuration = new ConfigurationBuilder()
+ .AddEnvironmentVariables()
+ .AddJsonFile("appsettings.json", optional: true)
+ .Build();
+ }
+
+ public void ConfigureServices(IServiceCollection services)
+ {
+ services.AddControllers();
+ services
+ .AddSwaggerGen();
+
+ services
+ .AddSingleton(configuration)
+ .AddSingleton(x => x.CreateScope())
+ .AddHostedService();
+
+ }
+
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
+ {
+ if (env.IsDevelopment())
{
- configuration = new ConfigurationBuilder()
- .AddEnvironmentVariables()
- .AddJsonFile("appsettings.json", optional: true)
- .Build();
- }
-
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddControllers();
- services
- .AddSwaggerGen();
-
- services
- .AddSingleton(configuration)
- .AddSingleton(x => x.CreateScope())
- .AddHostedService();
-
- }
-
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
- {
- if (env.IsDevelopment())
+ app.UseDeveloperExceptionPage();
+ app.UseSwagger();
+ app.UseSwagger(c =>
{
- app.UseDeveloperExceptionPage();
- app.UseSwagger();
- app.UseSwagger(c =>
- {
- c.RouteTemplate = "/swagger/v1/swagger.json";
- });
- }
- app.UseStaticFiles();
- app.UseRouting();
- app.UseCors(k => { k.AllowAnyHeader(); k.AllowAnyMethod(); k.AllowAnyOrigin(); k.WithMethods("POST", "GET"); });
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapControllers();
+ c.RouteTemplate = "/swagger/v1/swagger.json";
});
}
+ app.UseStaticFiles();
+ app.UseRouting();
+ app.UseCors(k => { k.AllowAnyHeader(); k.AllowAnyMethod(); k.AllowAnyOrigin(); k.WithMethods("POST", "GET"); });
+ app.UseEndpoints(endpoints =>
+ {
+ endpoints.MapControllers();
+ });
}
+
}
}
diff --git a/TG-Bot-Template.csproj b/TG-Bot-Template.csproj
index 3195c59..a390f0d 100644
--- a/TG-Bot-Template.csproj
+++ b/TG-Bot-Template.csproj
@@ -8,8 +8,11 @@
ccd791a8-a7f8-4a86-bcef-1a318801afa4
Linux
.
+ true
-
+
+
+
diff --git a/TelegramBotService.cs b/TelegramBotService.cs
index a416770..63648a2 100644
--- a/TelegramBotService.cs
+++ b/TelegramBotService.cs
@@ -3,13 +3,14 @@ using Telegram.Bot.Types;
using Telegram.Bot;
using Telegram.Bot.Polling;
using Telegram.Bot.Types.Enums;
+using System.Runtime.CompilerServices;
+using Telegram.Bot.Types.InlineQueryResults;
namespace TG_Bot_Template
{
public class TelegramBotService(IConfiguration conf, ILogger logger) : TelegramBotClient(conf.GetValue("tg-token")), IHostedService
{
private static Dictionary _userStates = new Dictionary();
- private static Dictionary _giveawaySettingMessages = new();
// For add any property, u should to add it in StartAsync func, like this:
// _PROPERTY_NAME = PROPERTY_NAME_FROM_CLASS
@@ -40,13 +41,69 @@ namespace TG_Bot_Template
}
private static async Task HandleUpdateAsync(ITelegramBotClient botClient, Message message)
{
+ try
+ {
+ string messageText = message.Text ?? message.Caption;
+ switch (messageText)
+ {
+ case "/start":
+ await botClient.SendTextMessageAsync(message.From.Id, "Start");
+ break;
+
+ default:
+ break;
+ }
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError($"{ex.Message}");
+ _logger.LogError($"Stack: {ex.StackTrace}");
+ }
}
private static async Task QueryUpdateHandler(ITelegramBotClient botClient, CallbackQuery callbackQuery)
- { }
+ {
+ try
+ {
+ string queryData = callbackQuery.Data;
+ switch (queryData)
+ {
+ case "test":
+ await botClient.AnswerCallbackQueryAsync(callbackQuery.Id, "Tested");
+ break;
+
+ default:
+ break;
+ }
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError($"{ex.Message}");
+ _logger.LogError($"Stack: {ex.StackTrace}");
+ }
+ }
private static async Task InlineUpdateHandler(ITelegramBotClient botClient, InlineQuery inlineQuery)
- { }
+ {
+ try
+ {
+ string inlineData = inlineQuery.Query;
+ switch (inlineData)
+ {
+ case "test":
+ await botClient.AnswerInlineQueryAsync(inlineQuery.Id, new InlineQueryResult[] { });
+ break;
+
+ default:
+ break;
+ }
+ }
+ catch (Exception ex)
+ {
+ _logger.LogError($"{ex.Message}");
+ _logger.LogError($"Stack: {ex.StackTrace}");
+ }
+ }
public async Task StartAsync(CancellationToken cancellationToken)
@@ -55,7 +112,7 @@ namespace TG_Bot_Template
// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
ReceiverOptions receiverOptions = new()
{
- AllowedUpdates = [UpdateType.Message, UpdateType.CallbackQuery] // receive all update types except ChatMember related updates
+ AllowedUpdates = [UpdateType.Message, UpdateType.CallbackQuery, UpdateType.InlineQuery] // receive all update types except ChatMember related updates
};
_logger = logger;
@@ -76,7 +133,7 @@ namespace TG_Bot_Template
public async Task StopAsync(CancellationToken cancellationToken)
{
- logger.Log(LogLevel.Information, $"Stopping service {this.GetType().Name}");
+ logger.Log(LogLevel.Information, $"Stopping service");
await this.LogOutAsync(cancellationToken);
}