Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit401e781

Browse files
Automatically Generate Pastebins (#151)
Co-authored-by: Bibo-Joshi <22366557+Bibo-Joshi@users.noreply.github.com>
1 parent0cd7d4c commit401e781

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
importasyncio
22
importcontextlib
3+
importhtml
4+
importjson
35
importlogging
46
importrandom
57
importre
@@ -8,6 +10,7 @@
810
fromcopyimportdeepcopy
911
fromtypingimportDict,List,Match,Tuple,cast
1012

13+
fromhttpximportcodes
1114
fromtelegramimport (
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()}"ifupdate.effective_userelseNone
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+
ifpastebin_client:=context.bot_data.get("pastebin_client"):
590+
# if there are code formatted snippets we only move those
591+
ifparsed_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=awaitpastebin_client.post(const.PASTEBIN_URL,content=content)
598+
# if the request was successful we put the link in the message
599+
ifr.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
awaitmessage.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
importos
44
fromtypingimportcast
55

6+
importhttpx
67
fromtelegramimport (
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"inconfig["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)

[8]ページ先頭

©2009-2025 Movatter.jp