diff --git a/zulip_bots/zulip_bots/bots/archive/archive.py b/zulip_bots/zulip_bots/bots/archive/archive.py new file mode 100644 index 000000000..e3dbc4c1c --- /dev/null +++ b/zulip_bots/zulip_bots/bots/archive/archive.py @@ -0,0 +1,36 @@ +""" +Note: this bot was written by Kimi K2. +""" + +import re + +import waybackpy + + +class ArchiveBotHandler: + def usage(self) -> str: + return """ + This bot listens for messages containing URLs, saves each URL to the + Internet Archive, and replies with the fresh archive.org link(s). + Mention the bot or send it a PM that contains a URL. + """ + + def handle_message(self, message: dict, bot_handler) -> None: + content = message["content"] + urls = re.findall(r"https?://[^\s]+", content) + if not urls: + return + + replies = [] + for url in urls: + try: + archive_url = waybackpy.Url(url).save() + replies.append(f"Archived: {archive_url}") + except Exception as exc: + replies.append(f"Failed to archive {url} – {exc}") + + if replies: + bot_handler.send_reply(message, "\n".join(replies)) + + +handler_class = ArchiveBotHandler diff --git a/zulip_bots/zulip_bots/bots/archive/requirements.txt b/zulip_bots/zulip_bots/bots/archive/requirements.txt new file mode 100644 index 000000000..48cec14e6 --- /dev/null +++ b/zulip_bots/zulip_bots/bots/archive/requirements.txt @@ -0,0 +1 @@ +waybackpy