mirror of
https://github.com/yawaflua/Telegram-Bot-Template.git
synced 2025-12-09 03:49:30 +02:00
Fix errors and add commas,
This commit is contained in:
@@ -2,10 +2,23 @@ using Microsoft.AspNetCore.Mvc;
|
|||||||
|
|
||||||
namespace TG_Bot_Template.Controllers
|
namespace TG_Bot_Template.Controllers
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Example controller
|
||||||
|
/// </summary>
|
||||||
[ApiController]
|
[ApiController]
|
||||||
[Route("[controller]")]
|
[Route("[controller]")]
|
||||||
public class ExampleController : ControllerBase
|
public class ExampleController : ControllerBase
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Just example endpoing
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
[HttpGet("ping")]
|
||||||
|
[Produces("application/json")]
|
||||||
|
[ProducesResponseType(200)]
|
||||||
|
public async Task<IActionResult> Pong()
|
||||||
|
{
|
||||||
|
return Ok("Pong!");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
18
Program.cs
18
Program.cs
@@ -1,15 +1,25 @@
|
|||||||
|
|
||||||
|
using Microsoft.AspNetCore;
|
||||||
|
|
||||||
namespace TG_Bot_Template
|
namespace TG_Bot_Template
|
||||||
{
|
{
|
||||||
public class Program
|
public class Program
|
||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main()
|
||||||
{
|
{
|
||||||
new WebHostBuilder()
|
CreateHostBuilder()
|
||||||
.UseKestrel(k => k.ListenAnyIP(80))
|
|
||||||
.UseStartup<Startup>()
|
|
||||||
.Build()
|
.Build()
|
||||||
.Run();
|
.Run();
|
||||||
}
|
}
|
||||||
|
private static IHostBuilder CreateHostBuilder()
|
||||||
|
{
|
||||||
|
return Host.CreateDefaultBuilder()
|
||||||
|
.ConfigureWebHostDefaults(webHost => {
|
||||||
|
webHost.UseStartup<Startup>();
|
||||||
|
webHost.UseStaticWebAssets();
|
||||||
|
webHost.UseKestrel(kestrelOptions => { kestrelOptions.ListenAnyIP(80); });
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
80
Startup.cs
80
Startup.cs
@@ -3,52 +3,54 @@ using System;
|
|||||||
|
|
||||||
namespace TG_Bot_Template
|
namespace TG_Bot_Template
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Startup class for ASP.NET with setting and configuration
|
||||||
|
/// </summary>
|
||||||
public class Startup
|
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<TelegramBotService>();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||||
|
{
|
||||||
|
if (env.IsDevelopment())
|
||||||
{
|
{
|
||||||
configuration = new ConfigurationBuilder()
|
app.UseDeveloperExceptionPage();
|
||||||
.AddEnvironmentVariables()
|
app.UseSwagger();
|
||||||
.AddJsonFile("appsettings.json", optional: true)
|
app.UseSwagger(c =>
|
||||||
.Build();
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ConfigureServices(IServiceCollection services)
|
|
||||||
{
|
|
||||||
services.AddControllers();
|
|
||||||
services
|
|
||||||
.AddSwaggerGen();
|
|
||||||
|
|
||||||
services
|
|
||||||
.AddSingleton(configuration)
|
|
||||||
.AddSingleton(x => x.CreateScope())
|
|
||||||
.AddHostedService<TelegramBotService>();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
|
||||||
{
|
|
||||||
if (env.IsDevelopment())
|
|
||||||
{
|
{
|
||||||
app.UseDeveloperExceptionPage();
|
c.RouteTemplate = "/swagger/v1/swagger.json";
|
||||||
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();
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
app.UseStaticFiles();
|
||||||
|
app.UseRouting();
|
||||||
|
app.UseCors(k => { k.AllowAnyHeader(); k.AllowAnyMethod(); k.AllowAnyOrigin(); k.WithMethods("POST", "GET"); });
|
||||||
|
app.UseEndpoints(endpoints =>
|
||||||
|
{
|
||||||
|
endpoints.MapControllers();
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,8 +8,11 @@
|
|||||||
<UserSecretsId>ccd791a8-a7f8-4a86-bcef-1a318801afa4</UserSecretsId>
|
<UserSecretsId>ccd791a8-a7f8-4a86-bcef-1a318801afa4</UserSecretsId>
|
||||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||||
<DockerfileContext>.</DockerfileContext>
|
<DockerfileContext>.</DockerfileContext>
|
||||||
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
||||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||||
|
|||||||
@@ -3,13 +3,14 @@ using Telegram.Bot.Types;
|
|||||||
using Telegram.Bot;
|
using Telegram.Bot;
|
||||||
using Telegram.Bot.Polling;
|
using Telegram.Bot.Polling;
|
||||||
using Telegram.Bot.Types.Enums;
|
using Telegram.Bot.Types.Enums;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
|
using Telegram.Bot.Types.InlineQueryResults;
|
||||||
|
|
||||||
namespace TG_Bot_Template
|
namespace TG_Bot_Template
|
||||||
{
|
{
|
||||||
public class TelegramBotService(IConfiguration conf, ILogger<TelegramBotService> logger) : TelegramBotClient(conf.GetValue<string>("tg-token")), IHostedService
|
public class TelegramBotService(IConfiguration conf, ILogger<TelegramBotService> logger) : TelegramBotClient(conf.GetValue<string>("tg-token")), IHostedService
|
||||||
{
|
{
|
||||||
private static Dictionary<long, string> _userStates = new Dictionary<long, string>();
|
private static Dictionary<long, string> _userStates = new Dictionary<long, string>();
|
||||||
private static Dictionary<int, string> _giveawaySettingMessages = new();
|
|
||||||
|
|
||||||
// For add any property, u should to add it in StartAsync func, like this:
|
// For add any property, u should to add it in StartAsync func, like this:
|
||||||
// _PROPERTY_NAME = PROPERTY_NAME_FROM_CLASS
|
// _PROPERTY_NAME = PROPERTY_NAME_FROM_CLASS
|
||||||
@@ -40,13 +41,69 @@ namespace TG_Bot_Template
|
|||||||
}
|
}
|
||||||
private static async Task HandleUpdateAsync(ITelegramBotClient botClient, Message message)
|
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)
|
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)
|
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)
|
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.
|
// StartReceiving does not block the caller thread. Receiving is done on the ThreadPool.
|
||||||
ReceiverOptions receiverOptions = new()
|
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;
|
_logger = logger;
|
||||||
@@ -76,7 +133,7 @@ namespace TG_Bot_Template
|
|||||||
|
|
||||||
public async Task StopAsync(CancellationToken cancellationToken)
|
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);
|
await this.LogOutAsync(cancellationToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user