-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dcd74c8
commit 19b38f6
Showing
19 changed files
with
236 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using BotNet.Services.Meme; | ||
using Microsoft.Extensions.DependencyInjection; | ||
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 { | ||
public static async Task HandleRamadAsync(ITelegramBotClient botClient, IServiceProvider serviceProvider, Message message, CancellationToken cancellationToken) { | ||
if (message.Entities is { Length: 1 } entities | ||
&& entities[0] is { Type: MessageEntityType.BotCommand, Offset: 0, Length: int commandLength } | ||
&& message.Text![commandLength..].Trim() is string commandArgument) { | ||
byte[] generatedImage = serviceProvider.GetRequiredService<MemeGenerator>().CaptionRamad(commandArgument); | ||
using MemoryStream floppedImageStream = new(generatedImage); | ||
|
||
await botClient.SendPhotoAsync( | ||
chatId: message.Chat.Id, | ||
photo: new InputOnlineFile(floppedImageStream, "ramad.png"), | ||
replyToMessageId: message.MessageId, | ||
cancellationToken: cancellationToken); | ||
} else { | ||
await botClient.SendTextMessageAsync( | ||
chatId: message.Chat.Id, | ||
text: "Untuk menyuruh Riza presentasi, silakan ketik /ramad diikuti judul presentasi.", | ||
parseMode: ParseMode.Html, | ||
replyToMessageId: message.MessageId, | ||
cancellationToken: cancellationToken); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using BotNet.Services.Typography; | ||
using SkiaSharp; | ||
|
||
namespace BotNet.Services.Meme { | ||
public class MemeGenerator { | ||
private readonly BotNetFontService _botNetFontService; | ||
|
||
public MemeGenerator( | ||
BotNetFontService botNetFontService | ||
) { | ||
_botNetFontService = botNetFontService; | ||
} | ||
|
||
public byte[] CaptionRamad(string text) { | ||
using Stream templateStream = typeof(MemeGenerator).Assembly.GetManifestResourceStream(Templates.RAMAD.ImageResourceName)!; | ||
using SKBitmap template = SKBitmap.Decode(templateStream); | ||
using SKSurface surface = SKSurface.Create(new SKImageInfo(template.Width, template.Height)); | ||
using SKCanvas canvas = surface.Canvas; | ||
|
||
SKRect templateRect = SKRect.Create(template.Width, template.Height); | ||
canvas.DrawBitmap( | ||
bitmap: template, | ||
source: templateRect, | ||
dest: templateRect | ||
); | ||
|
||
canvas.Save(); | ||
canvas.RotateDegrees(1.4f); | ||
using Stream fontStream = _botNetFontService.GetFontStyleById("Inter-Regular").OpenStream(); | ||
using SKTypeface typeface = SKTypeface.FromStream(fontStream); | ||
using SKPaint paint = new() { | ||
TextAlign = SKTextAlign.Left, | ||
Color = new SKColor(0x00, 0x00, 0x00, 0xcc), | ||
Typeface = typeface, | ||
TextSize = 17f, | ||
IsAntialias = true | ||
}; | ||
float offset = 0f; | ||
foreach (string line in WrapWords(text, 110f, paint)) { | ||
canvas.DrawText( | ||
text: line, | ||
x: 120f, | ||
y: 100f + offset, | ||
paint: paint | ||
); | ||
offset += 20f; // line height | ||
} | ||
canvas.Restore(); | ||
|
||
using SKImage image = surface.Snapshot(); | ||
using SKData data = image.Encode(SKEncodedImageFormat.Png, 100); | ||
|
||
using MemoryStream memoryStream = new(); | ||
data.SaveTo(memoryStream); | ||
|
||
return memoryStream.ToArray(); | ||
} | ||
|
||
private static List<string> WrapWords(string text, float maxWidth, SKPaint paint) { | ||
string[] words = text.Split(' ', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); | ||
List<string> lines = new(); | ||
bool firstWord = true; | ||
string line = ""; | ||
foreach (string word in words) { | ||
// Do not wrap first word | ||
if (firstWord) { | ||
line = word; | ||
firstWord = false; | ||
continue; | ||
} | ||
|
||
// Measure how wide will it take if we append this word to the current line | ||
string testLine = line + " " + word; | ||
SKRect bound = new(); | ||
paint.MeasureText(testLine, ref bound); | ||
|
||
// Wrap | ||
if (bound.Width > maxWidth) { | ||
lines.Add(line); | ||
line = word; | ||
continue; | ||
} | ||
|
||
// Append | ||
line += " " + word; | ||
} | ||
lines.Add(line); | ||
|
||
return lines; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace BotNet.Services.Meme { | ||
public static class ServiceCollectionExtensions { | ||
public static IServiceCollection AddMemeGenerator(this IServiceCollection services) { | ||
services.AddTransient<MemeGenerator>(); | ||
return services; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace BotNet.Services.Meme { | ||
internal sealed record Template( | ||
string ImageResourceName | ||
); | ||
|
||
internal static class Templates { | ||
public static readonly Template RAMAD = new("BotNet.Services.Meme.Images.Ramad.jpg"); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#if DEBUG | ||
using BotNet.Services.Meme; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
namespace BotNet.Controllers { | ||
[Route("meme")] | ||
public class MemeGeneratorTestController : Controller { | ||
private readonly MemeGenerator _memeGenerator; | ||
|
||
public MemeGeneratorTestController( | ||
MemeGenerator memeGenerator | ||
) { | ||
_memeGenerator = memeGenerator; | ||
} | ||
|
||
[Route("ramad")] | ||
public IActionResult RenderRamad() { | ||
byte[] memePng = _memeGenerator.CaptionRamad("Melakukan TDD, meski situasi sulit"); | ||
return File(memePng, "image/png", true); | ||
} | ||
} | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters