Base commit

This commit is contained in:
Samuele Lorefice
2024-12-24 23:08:08 +01:00
commit 0ba298b955
13 changed files with 201 additions and 0 deletions

1
TelegramBot/.env Normal file
View File

@@ -0,0 +1 @@
TELEGRAM_BOT_TOKEN=yourTokenHere

21
TelegramBot/Dockerfile Normal file
View File

@@ -0,0 +1,21 @@
FROM mcr.microsoft.com/dotnet/aspnet:8.0 AS base
USER $APP_UID
WORKDIR /app
FROM mcr.microsoft.com/dotnet/sdk:8.0 AS build
ARG BUILD_CONFIGURATION=Release
WORKDIR /src
COPY ["TelegramBot/TelegramBot.csproj", "TelegramBot/"]
RUN dotnet restore "TelegramBot/TelegramBot.csproj"
COPY . .
WORKDIR "/src/TelegramBot"
RUN dotnet build "TelegramBot.csproj" -c $BUILD_CONFIGURATION -o /app/build
FROM build AS publish
ARG BUILD_CONFIGURATION=Release
RUN dotnet publish "TelegramBot.csproj" -c $BUILD_CONFIGURATION -o /app/publish
FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "TelegramBot.dll"]

31
TelegramBot/Program.cs Normal file
View File

@@ -0,0 +1,31 @@
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using var cts = new CancellationTokenSource();
var bot = new TelegramBotClient(Environment.GetEnvironmentVariable("TELEGRAM_BOT_TOKEN"), cancellationToken:cts.Token);
var me = bot.GetMe();
bot.OnMessage += OnMessage;
Console.ReadLine();
cts.Cancel();
async Task OnMessage(Message msg, UpdateType type)
{
//Discard any message that is not a text message
if (msg.Type != MessageType.Text) return;
//Check if the message contains the bot's username
if (msg.Text!.Contains(me.Result.Username!, StringComparison.OrdinalIgnoreCase))
await bot.SendMessage(msg.Chat, "You mentioned me!");
//Check if the message is a reply to a message sent by the bot
else if(msg.ReplyToMessage != null && msg.ReplyToMessage.From!.Id == me.Result.Id){
await bot.SendMessage(msg.Chat, "You replied to a message!");
}
//We can safely return otherwise
}
HttpClient AiClient = new HttpClient();
string getNemesisMsg() {
//AiClient.GetAsync()
return "";
}

View File

@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<Content Include="..\.dockerignore">
<Link>.dockerignore</Link>
</Content>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Telegram.Bot" Version="22.2.0" />
</ItemGroup>
</Project>