mirror of
https://github.com/yawaflua/Telegram-Bot-Template.git
synced 2025-12-08 19:39:32 +02:00
Add project files.
This commit is contained in:
30
.dockerignore
Normal file
30
.dockerignore
Normal file
@@ -0,0 +1,30 @@
|
||||
**/.classpath
|
||||
**/.dockerignore
|
||||
**/.env
|
||||
**/.git
|
||||
**/.gitignore
|
||||
**/.project
|
||||
**/.settings
|
||||
**/.toolstarget
|
||||
**/.vs
|
||||
**/.vscode
|
||||
**/*.*proj.user
|
||||
**/*.dbmdl
|
||||
**/*.jfm
|
||||
**/azds.yaml
|
||||
**/bin
|
||||
**/charts
|
||||
**/docker-compose*
|
||||
**/Dockerfile*
|
||||
**/node_modules
|
||||
**/npm-debug.log
|
||||
**/obj
|
||||
**/secrets.dev.yaml
|
||||
**/values.dev.yaml
|
||||
LICENSE
|
||||
README.md
|
||||
!**/.gitignore
|
||||
!.git/HEAD
|
||||
!.git/config
|
||||
!.git/packed-refs
|
||||
!.git/refs/heads/**
|
||||
11
Controllers/ExampleController.cs
Normal file
11
Controllers/ExampleController.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace TG_Bot_Template.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class ExampleController : ControllerBase
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
24
Dockerfile
Normal file
24
Dockerfile
Normal file
@@ -0,0 +1,24 @@
|
||||
#See https://aka.ms/customizecontainer to learn how to customize your debug container and how Visual Studio uses this Dockerfile to build your images for faster debugging.
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
|
||||
USER app
|
||||
WORKDIR /app
|
||||
EXPOSE 80
|
||||
|
||||
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
WORKDIR /src
|
||||
COPY ["TG-Bot-Template.csproj", "."]
|
||||
RUN dotnet restore "./TG-Bot-Template.csproj"
|
||||
COPY . .
|
||||
WORKDIR "/src/."
|
||||
RUN dotnet build "./TG-Bot-Template.csproj" -c $BUILD_CONFIGURATION -o /app/build
|
||||
|
||||
FROM build AS publish
|
||||
ARG BUILD_CONFIGURATION=Release
|
||||
RUN dotnet publish "./TG-Bot-Template.csproj" -c $BUILD_CONFIGURATION -o /app/publish /p:UseAppHost=false
|
||||
|
||||
FROM base AS final
|
||||
WORKDIR /app
|
||||
COPY --from=publish /app/publish .
|
||||
ENTRYPOINT ["dotnet", "TG-Bot-Template.dll"]
|
||||
15
Program.cs
Normal file
15
Program.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
|
||||
namespace TG_Bot_Template
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
new WebHostBuilder()
|
||||
.UseKestrel(k => k.ListenAnyIP(80))
|
||||
.UseStartup<Startup>()
|
||||
.Build()
|
||||
.Run();
|
||||
}
|
||||
}
|
||||
}
|
||||
52
Properties/launchSettings.json
Normal file
52
Properties/launchSettings.json
Normal file
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "http://localhost:5111"
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
},
|
||||
"dotnetRunMessages": true,
|
||||
"applicationUrl": "https://localhost:7134;http://localhost:5111"
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"Container (Dockerfile)": {
|
||||
"commandName": "Docker",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "{Scheme}://{ServiceHost}:{ServicePort}/swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_HTTPS_PORTS": "8081",
|
||||
"ASPNETCORE_HTTP_PORTS": "8080"
|
||||
},
|
||||
"publishAllPorts": true,
|
||||
"useSSL": true
|
||||
}
|
||||
},
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:47947",
|
||||
"sslPort": 44317
|
||||
}
|
||||
}
|
||||
}
|
||||
54
Startup.cs
Normal file
54
Startup.cs
Normal file
@@ -0,0 +1,54 @@
|
||||
using Microsoft.AspNetCore.Hosting.Server;
|
||||
using System;
|
||||
|
||||
namespace TG_Bot_Template
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
public class Startup
|
||||
{
|
||||
private readonly IConfiguration configuration;
|
||||
|
||||
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())
|
||||
{
|
||||
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();
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
19
TG-Bot-Template.csproj
Normal file
19
TG-Bot-Template.csproj
Normal file
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<RootNamespace>TG_Bot_Template</RootNamespace>
|
||||
<UserSecretsId>ccd791a8-a7f8-4a86-bcef-1a318801afa4</UserSecretsId>
|
||||
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
|
||||
<DockerfileContext>.</DockerfileContext>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.6" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
|
||||
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
25
TG-Bot-Template.sln
Normal file
25
TG-Bot-Template.sln
Normal file
@@ -0,0 +1,25 @@
|
||||
|
||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||
# Visual Studio Version 17
|
||||
VisualStudioVersion = 17.9.34728.123
|
||||
MinimumVisualStudioVersion = 10.0.40219.1
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TG-Bot-Template", "TG-Bot-Template.csproj", "{A715056E-CFED-40D3-906D-AD1BF8C8E5FD}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
Release|Any CPU = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||
{A715056E-CFED-40D3-906D-AD1BF8C8E5FD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{A715056E-CFED-40D3-906D-AD1BF8C8E5FD}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{A715056E-CFED-40D3-906D-AD1BF8C8E5FD}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{A715056E-CFED-40D3-906D-AD1BF8C8E5FD}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {20E221A5-4AF6-46FE-AF6C-C8BBDB7663C1}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
84
TelegramBotService.cs
Normal file
84
TelegramBotService.cs
Normal file
@@ -0,0 +1,84 @@
|
||||
using System;
|
||||
using Telegram.Bot.Types;
|
||||
using Telegram.Bot;
|
||||
using Telegram.Bot.Polling;
|
||||
using Telegram.Bot.Types.Enums;
|
||||
|
||||
namespace TG_Bot_Template
|
||||
{
|
||||
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<int, string> _giveawaySettingMessages = new();
|
||||
|
||||
// For add any property, u should to add it in StartAsync func, like this:
|
||||
// _PROPERTY_NAME = PROPERTY_NAME_FROM_CLASS
|
||||
|
||||
private static ILogger<TelegramBotService> _logger { get; set; }
|
||||
|
||||
|
||||
private static async Task MainHandler(ITelegramBotClient botClient, Update update, CancellationToken cancellationToken)
|
||||
{
|
||||
_logger.Log(
|
||||
LogLevel.Information,
|
||||
$"Received an update: {update.Type}, Caller ID: {update.Message?.From?.Id ?? update.CallbackQuery?.From.Id ?? update.InlineQuery?.From.Id}"
|
||||
);
|
||||
var handler = update switch
|
||||
{
|
||||
|
||||
{ Message: { } message } => HandleUpdateAsync(botClient, message),
|
||||
//{ EditedMessage: { } message } => BotOnMessageReceived(message, cancellationToken),
|
||||
{ CallbackQuery: { } callbackQuery } => QueryUpdateHandler(botClient, callbackQuery),
|
||||
{ InlineQuery: { } inlineQuery } => InlineUpdateHandler(botClient, inlineQuery),
|
||||
//{ ChosenInlineResult: { } chosenInlineResult } => BotOnChosenInlineResultReceived(chosenInlineResult, cancellationToken),
|
||||
_ => Task.CompletedTask
|
||||
};
|
||||
|
||||
await handler;
|
||||
|
||||
|
||||
}
|
||||
private static async Task HandleUpdateAsync(ITelegramBotClient botClient, Message message)
|
||||
{
|
||||
}
|
||||
|
||||
private static async Task QueryUpdateHandler(ITelegramBotClient botClient, CallbackQuery callbackQuery)
|
||||
{ }
|
||||
|
||||
private static async Task InlineUpdateHandler(ITelegramBotClient botClient, InlineQuery inlineQuery)
|
||||
{ }
|
||||
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
logger.Log(LogLevel.Information, $"Starting build {this.GetType().Name}");
|
||||
// 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
|
||||
};
|
||||
|
||||
_logger = logger;
|
||||
|
||||
this.StartReceiving(
|
||||
updateHandler: MainHandler,
|
||||
pollingErrorHandler: (k, ex, ctx) => {
|
||||
Console.WriteLine(ex.Message);
|
||||
|
||||
return Task.CompletedTask;
|
||||
},
|
||||
receiverOptions: receiverOptions,
|
||||
cancellationToken: cancellationToken
|
||||
);
|
||||
var me = await this.GetMeAsync(cancellationToken: cancellationToken);
|
||||
logger.Log(LogLevel.Information, $"Start listening bot @{me.Username}");
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
logger.Log(LogLevel.Information, $"Stopping service {this.GetType().Name}");
|
||||
await this.LogOutAsync(cancellationToken);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
8
appsettings.Development.json
Normal file
8
appsettings.Development.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
10
appsettings.json
Normal file
10
appsettings.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*",
|
||||
"tg-token": "TOKEN"
|
||||
}
|
||||
Reference in New Issue
Block a user