From b2af76e7dc90317369c767a686d87937aff54a20 Mon Sep 17 00:00:00 2001 From: Ben Wiederhake Date: Sun, 23 Aug 2020 22:34:28 +0200 Subject: Support Telegram animated stickers (tgs) format (#1173) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is half a fix for #874 This patch introduces a new config flag: - MediaConvertTgs These need to be treated independently from the existing MediaConvertWebPToPNG flag because Tgs→WebP results in an *animated* WebP, and the WebP→PNG converter can't handle animated WebP files yet. Furthermore, some platforms (like discord) don't even support animated WebP files, so the user may want to fall back to static PNGs (not APNGs). The final reason why this is only half a fix is that this introduces an external dependency, namely lottie, to be installed like this: $ pip3 install lottie cairosvg This patch works by writing the tgs to a temporary file in /tmp, calling lottie to convert it (this conversion may take several seconds!), and then deleting the temporary file. The temporary file is absolutely necessary, as lottie refuses to work on non-seekable files. If anyone comes up with a reasonable use case where /tmp is unavailable, I can add yet another config option for that, if desired. Telegram will bail out if the option is configured but lottie isn't found. --- bridge/telegram/telegram.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'bridge/telegram/telegram.go') diff --git a/bridge/telegram/telegram.go b/bridge/telegram/telegram.go index 29f2f291..f1c7168c 100644 --- a/bridge/telegram/telegram.go +++ b/bridge/telegram/telegram.go @@ -2,6 +2,7 @@ package btelegram import ( "html" + "log" "strconv" "strings" @@ -16,6 +17,8 @@ const ( HTMLFormat = "HTML" HTMLNick = "htmlnick" MarkdownV2 = "MarkdownV2" + FormatPng = "png" + FormatWebp = "webp" ) type Btelegram struct { @@ -25,6 +28,16 @@ type Btelegram struct { } func New(cfg *bridge.Config) bridge.Bridger { + tgsConvertFormat := cfg.GetString("MediaConvertTgs") + if tgsConvertFormat != "" { + err := helper.CanConvertTgsToX() + if err != nil { + log.Fatalf("Telegram bridge configured to convert .tgs files to '%s', but lottie does not appear to work:\n%#v", tgsConvertFormat, err) + } + if tgsConvertFormat != FormatPng && tgsConvertFormat != FormatWebp { + log.Fatalf("Telegram bridge configured to convert .tgs files to '%s', but only '%s' and '%s' are supported.", FormatPng, FormatWebp, tgsConvertFormat) + } + } return &Btelegram{Config: cfg, avatarMap: make(map[string]string)} } -- cgit v1.2.3