parse_preview.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. from asyncio import sleep
  2. from pyrogram import filters
  3. from pyrogram.types import Message
  4. from wbb import SUDOERS, USERBOT_PREFIX, app2, eor
  5. from wbb.core.sections import section
  6. @app2.on_message(filters.command("parse_preview", prefixes=USERBOT_PREFIX) & SUDOERS)
  7. async def parse(_, message: Message):
  8. r = message.reply_to_message
  9. has_wpp = False
  10. if not r:
  11. return await eor(message, text="Reply to a message with a webpage")
  12. m_ = await eor(message, text="Parsing...")
  13. if not r.web_page:
  14. text = r.text or r.caption
  15. if text:
  16. m = await app2.send_message("me", text)
  17. await sleep(1)
  18. await m.delete()
  19. if m.web_page:
  20. r = m
  21. has_wpp = True
  22. else:
  23. has_wpp = True
  24. if not has_wpp:
  25. return await m_.edit(
  26. "Replied message has no webpage preview.",
  27. )
  28. wpp = r.web_page
  29. body = {
  30. "Title": [wpp.title or "Null"],
  31. "Description": [(wpp.description[:50] + "...") if wpp.description else "Null"],
  32. "URL": [wpp.display_url or "Null"],
  33. "Author": [wpp.author or "Null"],
  34. "Site Name": [wpp.site_name or "Null"],
  35. "Type": wpp.type or "Null",
  36. }
  37. text = section("Preview", body)
  38. t = wpp.type
  39. if t == "photo":
  40. media = wpp.photo
  41. func = app2.send_photo
  42. elif t == "audio":
  43. media = wpp.audio
  44. func = app2.send_audio
  45. elif t == "video":
  46. media = wpp.video
  47. func = app2.send_video
  48. elif t == "document":
  49. media = wpp.document
  50. func = app2.send_document
  51. else:
  52. media = None
  53. func = None
  54. if media and func:
  55. await m_.delete()
  56. return await func(
  57. m_.chat.id,
  58. media.file_id,
  59. caption=text,
  60. )
  61. await m_.edit(text, disable_web_page_preview=True)