[quote] Crappy chat quote implementation.

This commit is contained in:
West14
2022-01-21 00:10:27 +02:00
parent d9d5221280
commit 7cd1a295bb
10 changed files with 331 additions and 10 deletions
+82
View File
@@ -0,0 +1,82 @@
#nullable enable
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Telegram.Bot.Types;
using File = Telegram.Bot.Types.File;
namespace West.TelegramBot.ChatQuotes.Model;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class QuoteRequest
{
public QuoteRequest(Message msg)
{
// TODO: move default values to config
Format = "webp";
BackgroundColor = "#1b1429";
Width = 512;
Height = 768;
Scale = 2;
Messages = new List<QuoteMessage> {QuoteMessage.FromMessage(msg)};
}
public QuoteType Type { get; set; }
public string Format { get; set; }
public string BackgroundColor { get; set; }
public int Width { get; set; }
public int Height { get; set; }
public int Scale { get; set; }
public List<QuoteMessage>? Messages { get; set; }
public string ToJson()
{
return JsonConvert.SerializeObject(this, ChatQuotes.SerializerSettings);
}
}
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class QuoteMessage
{
public List<File>? Media { get; set; }
public QuoteMediaType? MediaType { get; set; }
public long ChatId { get; set; }
public bool Avatar { get; set; } = true;
public User? From { get; set; }
public string? Text { get; set; }
public Message? ReplyMessage { get; set; }
public static QuoteMessage FromMessage(Message msg)
{
var quoteMessage = new QuoteMessage();
if (msg.Text != null)
{
quoteMessage.Text = msg.Text;
}
var sticker = msg.Sticker;
if (sticker != null)
{
var file = new File
{
FileId = sticker.FileId,
FileSize = sticker.FileSize,
FileUniqueId = sticker.FileUniqueId
};
quoteMessage.Media = new List<File> {file};
quoteMessage.MediaType = QuoteMediaType.Sticker;
}
quoteMessage.ChatId = msg.Chat.Id;
quoteMessage.ReplyMessage = msg.ReplyToMessage;
quoteMessage.From = User.FromTgUser(msg.From!);
return quoteMessage;
}
}
public enum QuoteMediaType
{
Sticker = 0
}
+35
View File
@@ -0,0 +1,35 @@
#nullable disable
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace West.TelegramBot.ChatQuotes.Model;
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class QuoteResponse
{
public bool Ok { get; set; }
public Result Result { get; set; }
public static QuoteResponse FromJson(string json)
=> JsonConvert.DeserializeObject<QuoteResponse>(json, ChatQuotes.SerializerSettings);
}
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
public class Result
{
public string Image { get; set; }
public QuoteType Type { get; set; }
public long Width { get; set; }
public long Height { get; set; }
}
public enum QuoteType
{
Quote = 0,
Image,
Null
}
+22
View File
@@ -0,0 +1,22 @@
using Newtonsoft.Json;
namespace West.TelegramBot.ChatQuotes.Model;
public class User : Telegram.Bot.Types.User
{
[JsonProperty]
public string Name => LastName != null ? $"{FirstName} {LastName}" : FirstName;
public static User FromTgUser(Telegram.Bot.Types.User user)
{
return new User
{
Id = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
Username = user.Username,
LanguageCode = user.LanguageCode,
IsBot = user.IsBot
};
}
}