Skip to content

Commit 401e781

Browse files
Automatically Generate Pastebins (#151)
Co-authored-by: Bibo-Joshi <[email protected]>
1 parent 0cd7d4c commit 401e781

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

components/callbacks.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import asyncio
22
import contextlib
3+
import html
4+
import json
35
import logging
46
import random
57
import re
@@ -8,6 +10,7 @@
810
from copy import deepcopy
911
from typing import Dict, List, Match, Tuple, cast
1012

13+
from httpx import codes
1114
from telegram import (
1215
CallbackQuery,
1316
Chat,
@@ -576,7 +579,39 @@ async def long_code_handling(update: Update, context: ContextTypes.DEFAULT_TYPE)
576579

577580
# the leading ". " is important here since html_markup() splits on whitespaces!
578581
mention = f". {update.effective_user.mention_html()}" if update.effective_user else None
579-
text = f"{hint.html_markup(mention)}\n\n⚠️ Your message will be deleted in 1 minute."
582+
583+
text = (
584+
f"Hi {hint.html_markup(mention)}, we like to keep our groups readable and thus"
585+
f" require long code to be in a pastebin. \n\n⚠️ Your message will be deleted in 1 minute."
586+
)
587+
588+
# check if pastebin was setup
589+
if pastebin_client := context.bot_data.get("pastebin_client"):
590+
# if there are code formatted snippets we only move those
591+
if parsed_entities:
592+
content = "\n\n".join(parsed_entities.values())
593+
beginning = "⚠️ The code snippet(s) in your message have"
594+
else:
595+
content = text
596+
beginning = "⚠️ Your message has"
597+
r = await pastebin_client.post(const.PASTEBIN_URL, content=content)
598+
# if the request was successful we put the link in the message
599+
if r.status_code == codes.OK:
600+
text = (
601+
f"Hi {hint.html_markup(mention)}, we like to keep our groups readable and thus "
602+
f"require long code to be in a pastebin. \n\n{beginning} been moved to "
603+
f"{const.PASTEBIN_URL}{r.text}.py. Your original message will be deleted in a "
604+
f"minute, please reply to this message with your query."
605+
)
606+
else:
607+
logging.info(
608+
"The pastebin request failed with the status code %s and the text %s. The "
609+
"triggering update: %s",
610+
r.status_code,
611+
r.text,
612+
html.escape(json.dumps(update.to_dict(), indent=2, ensure_ascii=False)),
613+
exc_info=True,
614+
)
580615

581616
await message.reply_text(
582617
text,

components/const.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
DOCS_URL = "https://docs.python-telegram-bot.org/"
3232
OFFICIAL_URL = "https://core.telegram.org/bots/api"
3333
PROJECT_URL = urljoin(GITHUB_URL, DEFAULT_REPO + "/")
34+
PASTEBIN_URL = "https://pastebin.poolitzer.eu"
3435
WIKI_URL = urljoin(PROJECT_URL, "wiki/")
3536
WIKI_CODE_SNIPPETS_URL = urljoin(WIKI_URL, "Code-snippets")
3637
WIKI_FAQ_URL = urljoin(WIKI_URL, "Frequently-Asked-Questions")

rules_bot.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import os
44
from typing import cast
55

6+
import httpx
67
from telegram import (
78
BotCommandScopeAllGroupChats,
89
BotCommandScopeAllPrivateChats,
@@ -125,6 +126,11 @@ def main() -> None:
125126

126127
application.bot_data["search"] = Search(github_auth=config["KEYS"]["github_auth"])
127128

129+
if "pastebin_auth" in config["KEYS"]:
130+
application.bot_data["pastebin_client"] = httpx.AsyncClient(
131+
auth=httpx.BasicAuth(username="Rools", password=config["KEYS"]["pastebin_auth"])
132+
)
133+
128134
# Note: Order matters!
129135

130136
# Don't handle messages that were sent in the error channel

0 commit comments

Comments
 (0)