mirror of
https://github.com/Bubuni-Team/telegram-bot.git
synced 2026-07-31 00:29:24 +03:00
[quote] Code cleanup.
This commit is contained in:
@@ -4,12 +4,15 @@ using Microsoft.Extensions.Configuration;
|
||||
using Newtonsoft.Json;
|
||||
using Telegram.Bot.Types;
|
||||
using West.TelegramBot.ChatQuotes.Model;
|
||||
using West.TelegramBot.ChatQuotes.Model.Quote;
|
||||
using Message = Telegram.Bot.Types.Message;
|
||||
|
||||
namespace West.TelegramBot.ChatQuotes;
|
||||
|
||||
public class QuoteGeneratorService
|
||||
{
|
||||
private readonly HttpClient _httpClient;
|
||||
private readonly MediaTypeHeaderValue _mediaType;
|
||||
|
||||
public QuoteGeneratorService(IConfiguration configuration)
|
||||
{
|
||||
@@ -17,22 +20,24 @@ public class QuoteGeneratorService
|
||||
{
|
||||
BaseAddress = new Uri(configuration.GetValue<string>("QuoteApiUrl"))
|
||||
};
|
||||
|
||||
_mediaType = MediaTypeHeaderValue.Parse("application/json");
|
||||
}
|
||||
|
||||
public async Task<Stream> GenerateQuoteImage(Message msg)
|
||||
public async Task<Stream?> GenerateQuoteImage(Message msg)
|
||||
{
|
||||
var request = new QuoteRequest(msg);
|
||||
var request = new Request(msg);
|
||||
var content = new ByteArrayContent(Encoding.UTF8.GetBytes(request.ToJson().ToCharArray()))
|
||||
{
|
||||
Headers =
|
||||
{
|
||||
ContentType = MediaTypeHeaderValue.Parse("application/json")
|
||||
ContentType = _mediaType
|
||||
}
|
||||
};
|
||||
|
||||
var resp = await _httpClient.PostAsync("/generate", content);
|
||||
var quoteResponse = JsonConvert.DeserializeObject<QuoteResponse>(await resp.Content.ReadAsStringAsync());
|
||||
var quoteResponse = JsonConvert.DeserializeObject<Response>(await resp.Content.ReadAsStringAsync());
|
||||
|
||||
return new MemoryStream(Convert.FromBase64String(quoteResponse.Result.Image));
|
||||
return quoteResponse.Ok ? new MemoryStream(Convert.FromBase64String(quoteResponse.Result.Image)) : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user