Skip to content

Commit e3a0a90

Browse files
committed
bot: add archive bot
1 parent 0a9b91f commit e3a0a90

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Note: this bot was written by Kimi K2.
3+
"""
4+
5+
import re
6+
import waybackpy
7+
8+
class ArchiveBotHandler:
9+
def usage(self) -> str:
10+
return """
11+
This bot listens for messages containing URLs, saves each URL to the
12+
Internet Archive, and replies with the fresh archive.org link(s).
13+
Mention the bot or send it a PM that contains a URL.
14+
"""
15+
16+
def handle_message(self, message: dict, bot_handler) -> None:
17+
content = message["content"]
18+
urls = re.findall(r"https?://[^\s]+", content)
19+
if not urls:
20+
return
21+
22+
replies = []
23+
for url in urls:
24+
try:
25+
archive_url = waybackpy.Url(url).save()
26+
replies.append(f"Archived: {archive_url}")
27+
except Exception as exc:
28+
replies.append(f"Failed to archive {url}{exc}")
29+
30+
if replies:
31+
bot_handler.send_reply(message, "\n".join(replies))
32+
33+
handler_class = ArchiveBotHandler
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
re
2+
waybackpy

0 commit comments

Comments
 (0)