Skip to content

Commit

Permalink
Merge pull request #68 from teknologi-umum/dotnet-8
Browse files Browse the repository at this point in the history
Upgrade to .NET 8
  • Loading branch information
ronnygunawan authored Nov 28, 2023
2 parents 1b7ed9b + 1b4b90b commit 2a4ba74
Show file tree
Hide file tree
Showing 39 changed files with 353 additions and 514 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
with:
submodules: 'true'
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.100
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
with:
submodules: 'true'
- name: Setup .NET
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v3
with:
dotnet-version: 7.0.100
dotnet-version: 8.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
Expand Down
10 changes: 5 additions & 5 deletions BotNet.GrainInterfaces/BotNet.GrainInterfaces.csproj
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Maui.Graphics" Version="7.0.59" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="7.1.1" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.1.1" />
<PackageReference Include="Telegram.Bot" Version="18.0.0" />
<PackageReference Include="Microsoft.Maui.Graphics" Version="8.0.3" />
<PackageReference Include="Microsoft.Orleans.Core.Abstractions" Version="7.2.3" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.2.3" />
<PackageReference Include="Telegram.Bot" Version="19.0.0" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 3 additions & 3 deletions BotNet.Grains/BotNet.Grains.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Orleans.EventSourcing" Version="7.1.1" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.1.1" />
<PackageReference Include="Microsoft.Orleans.EventSourcing" Version="7.2.3" />
<PackageReference Include="Microsoft.Orleans.Sdk" Version="7.2.3" />
</ItemGroup>

<ItemGroup>
Expand Down
84 changes: 42 additions & 42 deletions BotNet.Grains/TrackedMessageGrain.cs
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
using System;
using System.Collections.Immutable;
using System.Threading.Tasks;
using BotNet.GrainInterfaces;
using Orleans;

namespace BotNet.Grains {
public class TrackedMessageGrain : Grain, ITrackedMessageGrain {
private string? _sender;
private string? _text;
private long? _replyToMessageId;

public Task TrackMessageAsync(string sender, string text, long? replyToMessageId) {
_sender = sender;
_text = text;
_replyToMessageId = replyToMessageId;
DelayDeactivation(TimeSpan.FromHours(1));
return Task.CompletedTask;
}

public async Task<(string? Sender, string? Text, long? ReplyToMessageId)> GetMessageAsync() {
DelayDeactivation(TimeSpan.FromHours(1));
return (_sender, _text, _replyToMessageId);
}

public async Task<ImmutableList<(string Sender, string Text)>> GetThreadAsync(int maxLines) {
if (_sender is null || _text is null) return ImmutableList<(string Sender, string Text)>.Empty;

ImmutableList<(string Sender, string Text)>.Builder builder = ImmutableList.Create((_sender, _text)).ToBuilder();

long? messageId = _replyToMessageId;
for (int i = 1; messageId.HasValue && i < maxLines; i++) {
ITrackedMessageGrain repledToMessageGrain = GrainFactory.GetGrain<ITrackedMessageGrain>(messageId.Value);
(string? sender, string? text, messageId) = await repledToMessageGrain.GetMessageAsync();
if (sender is not null && text is not null) {
builder.Add((sender, text));
}
}

DelayDeactivation(TimeSpan.FromHours(1));
return builder.ToImmutableList();
}
}
}
using System.Threading.Tasks;
using BotNet.GrainInterfaces;
using Orleans;

namespace BotNet.Grains {
public class TrackedMessageGrain : Grain, ITrackedMessageGrain {
private string? _sender;
private string? _text;
private long? _replyToMessageId;

public Task TrackMessageAsync(string sender, string text, long? replyToMessageId) {
_sender = sender;
_text = text;
_replyToMessageId = replyToMessageId;
DelayDeactivation(TimeSpan.FromHours(1));
return Task.CompletedTask;
}

public Task<(string? Sender, string? Text, long? ReplyToMessageId)> GetMessageAsync() {
DelayDeactivation(TimeSpan.FromHours(1));
return Task.FromResult((_sender, _text, _replyToMessageId));
}

public async Task<ImmutableList<(string Sender, string Text)>> GetThreadAsync(int maxLines) {
if (_sender is null || _text is null) return [];

ImmutableList<(string Sender, string Text)>.Builder builder = ImmutableList.Create((_sender, _text)).ToBuilder();

long? messageId = _replyToMessageId;
for (int i = 1; messageId.HasValue && i < maxLines; i++) {
ITrackedMessageGrain repledToMessageGrain = GrainFactory.GetGrain<ITrackedMessageGrain>(messageId.Value);
(string? sender, string? text, messageId) = await repledToMessageGrain.GetMessageAsync();
if (sender is not null && text is not null) {
builder.Add((sender, text));
}
}

DelayDeactivation(TimeSpan.FromHours(1));
return builder.ToImmutableList();
}
}
}
14 changes: 7 additions & 7 deletions BotNet.Services/BotCommands/Art.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -11,7 +10,6 @@
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;

namespace BotNet.Services.BotCommands {
public static class Art {
Expand All @@ -32,15 +30,16 @@ public static async Task GetRandomArtAsync(ITelegramBotClient botClient, IServic

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(imageStream, "art.jpg"),
photo: new InputFileStream(imageStream, "art.jpg"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "<code>Could not generate art</code>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId);
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
}
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
await botClient.SendTextMessageAsync(
Expand All @@ -60,7 +59,7 @@ await botClient.SendTextMessageAsync(

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(imageStream, "art.jpg"),
photo: new InputFileStream(imageStream, "art.jpg"),
cancellationToken: cancellationToken);
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
await botClient.SendTextMessageAsync(
Expand Down Expand Up @@ -95,15 +94,16 @@ public static async Task ModifyArtAsync(ITelegramBotClient botClient, IServicePr

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(imageStream, "art.jpg"),
photo: new InputFileStream(imageStream, "art.jpg"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} catch {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: "<code>Could not generate art</code>",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId);
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
}
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
await botClient.SendTextMessageAsync(
Expand Down
6 changes: 3 additions & 3 deletions BotNet.Services/BotCommands/BMKG.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ public static async Task GetLatestEarthQuakeAsync(ITelegramBotClient botClient,
try {
RATE_LIMITER.ValidateActionRate(message.Chat.Id, message.From!.Id);

(string Text, string ShakemapUrl) = await serviceProvider.GetRequiredService<LatestEarthQuake>().GetLatestAsync();
(string text, string shakemapUrl) = await serviceProvider.GetRequiredService<LatestEarthQuake>().GetLatestAsync();

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: ShakemapUrl,
caption: Text,
photo: new InputFileUrl(shakemapUrl),
caption: text,
replyToMessageId: message.MessageId,
parseMode: ParseMode.Html,
cancellationToken: cancellationToken);
Expand Down
3 changes: 1 addition & 2 deletions BotNet.Services/BotCommands/Cat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;

namespace BotNet.Services.BotCommands {
public static class Cat {
Expand All @@ -22,7 +21,7 @@ public static async Task GetRandomCatAsync(ITelegramBotClient botClient, IServic

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(imageStream, "cat.jpg"),
photo: new InputFileStream(imageStream, "cat.jpg"),
cancellationToken: cancellationToken);
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
await botClient.SendTextMessageAsync(
Expand Down
9 changes: 4 additions & 5 deletions BotNet.Services/BotCommands/FlipFlop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;

namespace BotNet.Services.BotCommands {
public static class FlipFlop {
Expand Down Expand Up @@ -44,7 +43,7 @@ await botClient.SendTextMessageAsync(

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(flippedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
photo: new InputFileStream(flippedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
replyToMessageId: message.ReplyToMessage.MessageId,
cancellationToken: cancellationToken);
}
Expand Down Expand Up @@ -83,7 +82,7 @@ await botClient.SendTextMessageAsync(

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(floppedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
photo: new InputFileStream(floppedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
replyToMessageId: message.ReplyToMessage.MessageId,
cancellationToken: cancellationToken);
}
Expand Down Expand Up @@ -122,7 +121,7 @@ await botClient.SendTextMessageAsync(

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(flappedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
photo: new InputFileStream(flappedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
replyToMessageId: message.ReplyToMessage.MessageId,
cancellationToken: cancellationToken);
}
Expand Down Expand Up @@ -161,7 +160,7 @@ await botClient.SendTextMessageAsync(

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(fleppedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
photo: new InputFileStream(fleppedImageStream, new string(fileInfo.FileId.Reverse().ToArray()) + ".png"),
replyToMessageId: message.ReplyToMessage.MessageId,
cancellationToken: cancellationToken);
}
Expand Down
12 changes: 11 additions & 1 deletion BotNet.Services/BotCommands/Idea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ public static async Task GetRandomIdeaAsync(ITelegramBotClient botClient, IServi
try {
RATE_LIMITER.ValidateActionRate(message.Chat.Id, message.From!.Id);

string idea = await serviceProvider.GetRequiredService<ThisIdeaDoesNotExist>().GetRandomIdeaAsync(cancellationToken);
string? idea = await serviceProvider.GetRequiredService<ThisIdeaDoesNotExist>().GetRandomIdeaAsync(cancellationToken);

if (idea is null) {
await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
text: $"Bentar ya saya mikir dulu idenya. Coba lagi nanti.",
parseMode: ParseMode.Html,
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
return;
}

await botClient.SendTextMessageAsync(
chatId: message.Chat.Id,
Expand Down
3 changes: 1 addition & 2 deletions BotNet.Services/BotCommands/Joke.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;

namespace BotNet.Services.BotCommands {
public static class Joke {
Expand All @@ -22,7 +21,7 @@ public static async Task GetRandomJokeAsync(ITelegramBotClient botClient, IServi

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(imageStream, "joke.webp"),
photo: new InputFileStream(imageStream, "joke.webp"),
caption: title,
cancellationToken: cancellationToken);
} catch (RateLimitExceededException exc) when (exc is { Cooldown: var cooldown }) {
Expand Down
3 changes: 1 addition & 2 deletions BotNet.Services/BotCommands/Meme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using Telegram.Bot.Types.InputFiles;

namespace BotNet.Services.BotCommands {
public static class Meme {
Expand All @@ -20,7 +19,7 @@ public static async Task HandleRamadAsync(ITelegramBotClient botClient, IService

await botClient.SendPhotoAsync(
chatId: message.Chat.Id,
photo: new InputOnlineFile(floppedImageStream, "ramad.png"),
photo: new InputFileStream(floppedImageStream, "ramad.png"),
replyToMessageId: message.MessageId,
cancellationToken: cancellationToken);
} else {
Expand Down
Loading

0 comments on commit 2a4ba74

Please sign in to comment.