├── .env.sample ├── .gitignore ├── bot.py └── requirements.txt /.env.sample: -------------------------------------------------------------------------------- 1 | BOT_TOKEN= -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .env -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | # < (c) 2021 @xditya > 2 | 3 | import logging 4 | from os import remove 5 | 6 | import requests 7 | from decouple import config 8 | from htmlwebshot import WebShot 9 | from telethon import Button, TelegramClient, events 10 | from telethon.errors.rpcerrorlist import PhotoInvalidDimensionsError 11 | 12 | shot = WebShot() 13 | 14 | logging.basicConfig( 15 | format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.INFO 16 | ) 17 | 18 | bot = TelegramClient(None, api_id=6, api_hash="eb06d4abfb49dc3eeb1aeb98ae0f581e").start( 19 | bot_token=config("BOT_TOKEN") 20 | ) 21 | 22 | logging.info("Starting bot...") 23 | 24 | 25 | @bot.on(events.NewMessage(incoming=True, pattern="^/start")) 26 | async def start_(event): 27 | await event.reply( 28 | "Hi {}!\nI am a webshot bot. \n\n**Usage:** Send me a URL or a `.html` file and I'll generate a web-screenshot for you!".format( 29 | (await bot.get_entity(event.sender_id)).first_name 30 | ), 31 | buttons=[ 32 | [ 33 | Button.url("Repo", url="https://github.com/xditya/HTMLWebShotBot"), 34 | Button.url( 35 | "Package", url="https://pypi.org/project/htmlwebshot/0.1.0/" 36 | ), 37 | ], 38 | [Button.url("Channel", url="https://t.me/BotzHub")], 39 | ], 40 | ) 41 | 42 | 43 | @bot.on(events.NewMessage(incoming=True, func=lambda e: e.is_private)) 44 | async def web_ss_capture(event): 45 | if event.text and not event.text.startswith("/") and not event.document: 46 | url = event.text 47 | xurl = "" 48 | xx = await event.reply("Getting info...") 49 | try: 50 | requests.get(url) 51 | xurl = url 52 | except requests.ConnectionError: 53 | return await xx.edit("Invalid URL!") 54 | except requests.exceptions.MissingSchema: 55 | try: 56 | requests.get("https://" + url) 57 | xurl = "https://" + url 58 | except requests.ConnectionError: 59 | try: 60 | requests.get("http://" + url) 61 | xurl = "http://" + url 62 | except requests.ConnectionError: 63 | return await xx.edit("Invalid URL!") 64 | await xx.edit("Generating a webshot...") 65 | try: 66 | web_ss_path = shot.create_pic(url=xurl) 67 | await xx.edit("Uploading a webshot of `{}`".format(xurl)) 68 | await bot.send_file( 69 | event.chat_id, 70 | file=web_ss_path, 71 | caption="**WebShot generated.**\n\n~ @BotzHub", 72 | ) 73 | await xx.delete() 74 | remove(web_ss_path) 75 | except Exception as e: 76 | await xx.edit( 77 | f"**ERROR**: \n`{e}`\n**URL**: `{xurl}`\n\nKindly forward this message to @BotzHubChat." 78 | ) 79 | elif event.document and event.file.name.endswith(".html"): 80 | xx = await event.reply("Downloading file.... Please wait..") 81 | path = await bot.download_file(event.document) 82 | await xx.edit("Generating a screenshot...") 83 | shot.create_pic(html=path, output="webss_bh.jpg") 84 | try: 85 | await event.reply( 86 | "**ScreenShot generated.**\n\n~ @BotzHub", file="webss_bh.jpg" 87 | ) 88 | await xx.delete() 89 | except PhotoInvalidDimensionsError: 90 | await event.reply( 91 | "**ScreenShot generated.**\n\n~ @BotzHub", file="webss_bh.jpg", 92 | force_document=True 93 | ) 94 | await xx.delete() 95 | 96 | try: 97 | remove("webss_bh.jpg") 98 | except Exception as e: 99 | logging.warning(e) 100 | 101 | 102 | logging.info("\n\nBot has started.\n(c) @xditya") 103 | 104 | bot.run_until_disconnected() 105 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | python-decouple 2 | telethon 3 | htmlwebshot 4 | requests --------------------------------------------------------------------------------