Skip to content

Commit 93f7009

Browse files
committed
Bridge some matrix stickers as telegram stickers
With maunium/stickerpicker#49 applied, Element Desktop users send enough information about telegram-imported stickers to bridge them as regular telegram stickers. This commit has a few caveats, most notably it only works with sticker sets that are currently added to the user’s telegram account, and it doesn’t cache either the sticker sets or the stickers within these sets. However I haven’t noticed any performance issues due to this in my own testing over at chir.rs.
1 parent 952c81e commit 93f7009

1 file changed

Lines changed: 43 additions & 2 deletions

File tree

mautrix_telegram/portal.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,6 +1626,35 @@ async def _handle_matrix_text(
16261626
msgtype=content.msgtype,
16271627
)
16281628

1629+
async def _find_telegram_sticker(self, client, metadata):
1630+
sticker_set_id = int(metadata["pack"]["id"])
1631+
from telethon.tl.functions.messages import GetAllStickersRequest
1632+
1633+
sticker_sets = await client(GetAllStickersRequest(0))
1634+
from telethon.tl.functions.messages import GetStickerSetRequest
1635+
from telethon.tl.types import InputStickerSetID
1636+
1637+
found_sticker_set = None
1638+
for sticker_set in sticker_sets.sets:
1639+
if sticker_set.id == sticker_set_id:
1640+
found_sticker_set = sticker_set
1641+
break
1642+
if not found_sticker_set:
1643+
return None
1644+
1645+
stickers = await client(
1646+
GetStickerSetRequest(
1647+
hash=0,
1648+
stickerset=InputStickerSetID(
1649+
id=found_sticker_set.id, access_hash=found_sticker_set.access_hash
1650+
),
1651+
)
1652+
)
1653+
1654+
for sticker in stickers.documents:
1655+
if sticker.id == int(metadata["id"]):
1656+
return sticker
1657+
16291658
async def _handle_matrix_file(
16301659
self,
16311660
sender: u.User,
@@ -1646,6 +1675,7 @@ async def _handle_matrix_file(
16461675
w = h = None
16471676
max_image_size = self.config["bridge.image_as_file_size"] * 1000**2
16481677
max_image_pixels = self.config["bridge.image_as_file_pixels"]
1678+
media = None
16491679

16501680
if self.config["bridge.parallel_file_transfer"] and content.url:
16511681
file_handle, file_size = await util.parallel_transfer_to_telegram(
@@ -1666,7 +1696,16 @@ async def _handle_matrix_file(
16661696
file = await self.main_intent.download_media(content.url)
16671697

16681698
if content.msgtype == MessageType.STICKER:
1669-
if mime != "image/gif":
1699+
tg_sticker = None
1700+
1701+
if "net.maunium.telegram.sticker" in content.info:
1702+
tg_sticker = await self._find_telegram_sticker(
1703+
client, content.info["net.maunium.telegram.sticker"]
1704+
)
1705+
1706+
if tg_sticker is not None:
1707+
media = tg_sticker
1708+
elif mime != "image/gif":
16701709
mime, file, w, h = util.convert_image(
16711710
file, source_mime=mime, target_type="webp"
16721711
)
@@ -1708,7 +1747,9 @@ async def _handle_matrix_file(
17081747
if "fi.mau.telegram.force_document" in content:
17091748
force_document = bool(content["fi.mau.telegram.force_document"])
17101749

1711-
if (mime == "image/png" or mime == "image/jpeg") and not force_document:
1750+
if media is not None:
1751+
pass
1752+
elif (mime == "image/png" or mime == "image/jpeg") and not force_document:
17121753
media = InputMediaUploadedPhoto(file_handle)
17131754
else:
17141755
media = InputMediaUploadedDocument(

0 commit comments

Comments
 (0)