Initial commit
This commit is contained in:
parent
4fd3d49af1
commit
8c21710dd2
1 changed files with 16 additions and 40 deletions
|
@ -31,50 +31,26 @@ class PreviewBot(slixmpp.ClientXMPP):
|
|||
|
||||
async def on_message(self, msg):
|
||||
if msg["type"] in ("chat", "normal") and msg["from"] != self.nick:
|
||||
url_matches = url_regex.findall(msg["body"])
|
||||
if url_matches:
|
||||
print("Fetching previews for:", url_matches)
|
||||
fetch_tasks = [urlpreview.get_preview(url) for url in url_matches]
|
||||
previews = await asyncio.gather(*fetch_tasks)
|
||||
preview_lines = []
|
||||
for url, preview_resp in zip(url_matches, previews):
|
||||
if preview_resp is None:
|
||||
preview_lines.append(f"Could not fetch preview for {url}")
|
||||
else:
|
||||
preview_lines.append(f"Title: {preview_resp}")
|
||||
preview_text = "\n".join(preview_lines)
|
||||
msg.reply(preview_text).send()
|
||||
return
|
||||
if "kuschelkatze" in msg["body"]:
|
||||
reply_msg = msg.reply()
|
||||
reply_msg["body"] = "http://bastiodon.lan/gabriel/ba8af1391c85f763.jpeg"
|
||||
reply_msg["oob"]["url"] = "http://bastiodon.lan/gabriel/ba8af1391c85f763.jpeg"
|
||||
reply_msg["oob"]["desc"] = "Katzenbild"
|
||||
reply_msg.send()
|
||||
self.handle_msg(msg)
|
||||
|
||||
async def on_muc_message(self, msg):
|
||||
if msg["type"] == "groupchat" and msg["from"] != self.nick:
|
||||
url_matches = url_regex.findall(msg["body"])
|
||||
if url_matches:
|
||||
print("Fetching previews for:", url_matches)
|
||||
fetch_tasks = [urlpreview.get_preview(url) for url in url_matches]
|
||||
previews = await asyncio.gather(*fetch_tasks)
|
||||
preview_lines = []
|
||||
for url, preview_resp in zip(url_matches, previews):
|
||||
if preview_resp is None:
|
||||
preview_lines.append(f"Could not fetch preview for {url}")
|
||||
else:
|
||||
preview_lines.append(f"Title: {preview_resp}")
|
||||
preview_text = "\n".join(preview_lines)
|
||||
msg.reply(preview_text).send()
|
||||
return
|
||||
if "kuschelkatze" in msg["body"]:
|
||||
reply_msg = msg.reply()
|
||||
reply_msg["body"] = "http://bastiodon.lan/gabriel/ba8af1391c85f763.jpeg"
|
||||
reply_msg["oob"]["url"] = "http://bastiodon.lan/gabriel/ba8af1391c85f763.jpeg"
|
||||
reply_msg["oob"]["desc"] = "Katzenbild"
|
||||
reply_msg.send()
|
||||
self.handle_msg(msg)
|
||||
|
||||
async def handle_msg(self, msg):
|
||||
url_matches = url_regex.findall(msg["body"])
|
||||
if url_matches:
|
||||
print("Fetching previews for:", url_matches)
|
||||
fetch_tasks = [urlpreview.get_preview(url) for url in url_matches]
|
||||
previews = await asyncio.gather(*fetch_tasks)
|
||||
preview_lines = []
|
||||
for url, preview_resp in zip(url_matches, previews):
|
||||
if preview_resp is None:
|
||||
preview_lines.append(f"Could not fetch preview for {url}")
|
||||
else:
|
||||
preview_lines.append(f"Title: {preview_resp}")
|
||||
preview_text = "\n".join(preview_lines)
|
||||
msg.reply(preview_text).send()
|
||||
|
||||
if __name__ == "__main__":
|
||||
config = configparser.ConfigParser()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue