|
1 | 1 | import asyncio
|
2 | 2 | import contextlib
|
| 3 | +import html |
| 4 | +import json |
3 | 5 | import logging
|
4 | 6 | import random
|
5 | 7 | import re
|
|
8 | 10 | from copy import deepcopy
|
9 | 11 | from typing import Dict, List, Match, Tuple, cast
|
10 | 12 |
|
| 13 | +from httpx import codes |
11 | 14 | from telegram import (
|
12 | 15 | CallbackQuery,
|
13 | 16 | Chat,
|
@@ -576,7 +579,39 @@ async def long_code_handling(update: Update, context: ContextTypes.DEFAULT_TYPE)
|
576 | 579 |
|
577 | 580 | # the leading ". " is important here since html_markup() splits on whitespaces!
|
578 | 581 | 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 | + ) |
580 | 615 |
|
581 | 616 | await message.reply_text(
|
582 | 617 | text,
|
|
0 commit comments