Add miskey preview
This commit is contained in:
parent
97d68569e0
commit
ca005326d6
3 changed files with 55 additions and 1 deletions
49
app/modules/misskey_preview.py
Normal file
49
app/modules/misskey_preview.py
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
"""
|
||||||
|
This module takes misskey links (e.g.) and provides a preview in a reply.
|
||||||
|
"""
|
||||||
|
import niobot
|
||||||
|
import httpx
|
||||||
|
import typing
|
||||||
|
import textwrap
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
if typing.TYPE_CHECKING:
|
||||||
|
from ..main import TortoiseIntegratedBot
|
||||||
|
|
||||||
|
|
||||||
|
class MisskeyPreviewModule(niobot.Module):
|
||||||
|
bot: "TortoiseIntegratedBot"
|
||||||
|
|
||||||
|
@niobot.event("message")
|
||||||
|
async def on_message(self, room: niobot.MatrixRoom, event: niobot.RoomMessage):
|
||||||
|
supported_prefixes = self.bot.cfg.get("misskey_preview", {})
|
||||||
|
supported_prefixes = supported_prefixes.get("urls", ["https://fedi.transgender.ing/notes"])
|
||||||
|
if not isinstance(event, niobot.RoomMessageText):
|
||||||
|
return
|
||||||
|
|
||||||
|
sent = []
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
for item in event.body.split():
|
||||||
|
if not event.body.startswith(tuple(supported_prefixes)):
|
||||||
|
return
|
||||||
|
parsed = urlparse(item)
|
||||||
|
post_id = parsed.path.split("/")[-1]
|
||||||
|
if post_id in sent:
|
||||||
|
continue
|
||||||
|
elif len(sent) >= 5:
|
||||||
|
break
|
||||||
|
resp = await client.post("https://%s/api/notes/show" % parsed.netloc, json={"noteId": post_id})
|
||||||
|
if resp.status_code != 200:
|
||||||
|
continue
|
||||||
|
data = resp.json()
|
||||||
|
if not data["text"]:
|
||||||
|
continue
|
||||||
|
text_body = []
|
||||||
|
for line in textwrap.shorten(data["text"], width=1000).splitlines(keepends=True):
|
||||||
|
text_body.append("> " + (line or "\u200b"))
|
||||||
|
body = "<a href=\"%s\">@%s:</a>\n\n%s" % (
|
||||||
|
"https://%s/notes/%s" % (parsed.netloc, data["user"]["username"]),
|
||||||
|
data["user"]["username"],
|
||||||
|
"".join(text_body),
|
||||||
|
)
|
||||||
|
await self.bot.send_message(room, body, reply_to=event)
|
||||||
|
sent.append(post_id)
|
|
@ -5,4 +5,7 @@ device_id = "nonsensebot" # optional, defaults to hostname. DO NOT CHANGE AFTER
|
||||||
access_token = "syt_xyzxyz" # access token (niocli get-access-token)
|
access_token = "syt_xyzxyz" # access token (niocli get-access-token)
|
||||||
|
|
||||||
[database]
|
[database]
|
||||||
url = "sqlite://./store/data.db"
|
url = "sqlite://./store/data.db"
|
||||||
|
|
||||||
|
[misskey_preview]
|
||||||
|
urls = ["https://fedi.transgender.ing/notes"]
|
2
tox.init
Normal file
2
tox.init
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
[flake8]
|
||||||
|
max-line-length=120
|
Loading…
Reference in a new issue