├── .env.sample ├── .gitignore ├── requirements.txt ├── README.md └── bot.py /.env.sample: -------------------------------------------------------------------------------- 1 | BOT_TOKEN= -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | bot.session 2 | .env -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | python-decouple 2 | telethon 3 | requests 4 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Link Shortener Bot. 2 | 3 | Can be found on telegram as [@BHURLBot](https://t.me/BHURLBot) 4 | 5 | ## Deploying 6 | Only necessary var is `BOT_TOKEN`. 7 | 8 | 9 | ## Credits 10 | - [@BotzHub](https://t.me/BotzHub) 11 | - [Me](https://xditya.me) -------------------------------------------------------------------------------- /bot.py: -------------------------------------------------------------------------------- 1 | # < (c) @xditya > 2 | # This file is a part of LinkShortener < https://github.com/xditya/LinkShortener > 3 | 4 | import logging 5 | from telethon import TelegramClient, events, Button 6 | from decouple import config 7 | import requests 8 | from telethon.errors.rpcerrorlist import QueryIdInvalidError 9 | import re 10 | 11 | logging.basicConfig( 12 | format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.INFO 13 | ) 14 | 15 | bottoken = None 16 | # start the bot 17 | print("Starting...") 18 | apiid = 6 19 | apihash = "eb06d4abfb49dc3eeb1aeb98ae0f581e" 20 | try: 21 | bottoken = config("BOT_TOKEN") 22 | except: 23 | print("Environment vars are missing! Kindly recheck.") 24 | print("Bot is quiting...") 25 | exit() 26 | 27 | if bottoken != None: 28 | try: 29 | BotzHub = TelegramClient("bot", apiid, apihash).start(bot_token=bottoken) 30 | except Exception as e: 31 | print(f"ERROR!\n{str(e)}") 32 | print("Bot is quiting...") 33 | exit() 34 | else: 35 | print("Environment vars are missing! Kindly recheck.") 36 | print("Bot is quiting...") 37 | exit() 38 | 39 | base_url = "https://is.gd/create.php?format=simple&url=" 40 | dagd_url = "https://da.gd/s?url=" 41 | 42 | @BotzHub.on(events.NewMessage(incoming=True, pattern="^/start$")) 43 | async def msgg(event): 44 | await send_start(event, "msg") 45 | 46 | 47 | @BotzHub.on(events.NewMessage(incoming=True, pattern="^/start xx")) 48 | async def msgg(event): 49 | await send_start(event, "msg") 50 | 51 | 52 | @BotzHub.on(events.callbackquery.CallbackQuery(data="help")) 53 | async def send_help(event): 54 | await event.edit( 55 | "**URL Shortener.**\n\nSend me any URL and I'll shorten it for you!\nJoin @BotzHub if you liked this bot!", 56 | buttons=[ 57 | [Button.switch_inline("Go Inline", query="", same_peer=True)], 58 | [Button.inline("« Back", data="bck")], 59 | ], 60 | ) 61 | 62 | 63 | @BotzHub.on(events.callbackquery.CallbackQuery(data="bck")) 64 | async def bk(event): 65 | await send_start(event, "") 66 | 67 | 68 | @BotzHub.on(events.NewMessage(incoming=True, func=lambda e: e.is_private)) 69 | async def fn_(event): 70 | if event.text.startswith("/"): 71 | return # ignore commands. 72 | await event.reply( 73 | "Select the shortenert service.", 74 | buttons=[ 75 | Button.inline("is.gd", data=f"i_{event.text}"), 76 | Button.inline("da.gd", data=f"d_{event.text}") 77 | ]) 78 | 79 | @BotzHub.on(events.callbackquery.CallbackQuery(data=re.compile(b"i_(.*)"))) 80 | async def in_pl(event): 81 | await event.answer("Processing...") 82 | tmp = event.data_match.group(1).decode("UTF-8") 83 | return await event.edit(link_shortener(tmp)) 84 | 85 | 86 | @BotzHub.on(events.callbackquery.CallbackQuery(data=re.compile(b"d_(.*)"))) 87 | async def in_pl(event): 88 | await event.answer("Processing...") 89 | tmp = event.data_match.group(1).decode("UTF-8") 90 | return await event.edit(dagd_shrt(tmp)) 91 | 92 | 93 | @BotzHub.on(events.InlineQuery) 94 | async def in_q(event): 95 | if len(event.text) == 0: 96 | await event.answer( 97 | [], switch_pm="Enter a URL to shorten it.", switch_pm_param="xx" 98 | ) 99 | else: 100 | try: 101 | await event.answer( 102 | [ 103 | await event.builder.article( 104 | title="is.gd shortener.", 105 | description=link_shortener(event.text), 106 | text=link_shortener(event.text), 107 | ), 108 | await event.builder.article( 109 | title="da.gd shortener.", 110 | description=dagd_shrt(event.text), 111 | text=dagd_shrt(event.text), 112 | ), 113 | ], 114 | switch_pm="Shortener", 115 | switch_pm_param="xx", 116 | ) 117 | except QueryIdInvalidError: 118 | await event.answer([], switch_pm="Busy. Please try again.", switch_pm_param="xx") 119 | 120 | buttons = [ 121 | [Button.inline("Help", data="help")], 122 | [ 123 | Button.url("Channel", url="t.me/BotzHub"), 124 | Button.url("Source", url="https://github.com/xditya/LinkShortener"), 125 | ], 126 | ] 127 | 128 | 129 | async def send_start(event, mode): 130 | user_ = await BotzHub.get_entity(event.sender_id) 131 | if mode == "msg": 132 | await event.reply( 133 | f"Hi {user_.first_name}.\n\nI am a URL shortener bot!", buttons=buttons 134 | ) 135 | else: 136 | await event.edit( 137 | f"Hi {user_.first_name}.\n\nI am a URL shortener bot!", buttons=buttons 138 | ) 139 | 140 | 141 | def link_shortener(url): 142 | req_ = f"{base_url}{url}" 143 | try: 144 | requests.get(req_) 145 | except requests.exceptions.ConnectionError: 146 | return "Invalid URL!" 147 | return requests.get(req_).text 148 | 149 | 150 | def dagd_shrt(url): 151 | if not (url.startswith("http://") or url.startswith("https://")): 152 | r = f"http://{url}" 153 | else: 154 | r = url 155 | try: 156 | requests.get(r) 157 | except requests.exceptions.ConnectionError: 158 | return "Invalid URL!" 159 | tmp = requests.get(f"{dagd_url}{r}").text 160 | if tmp: 161 | return tmp 162 | else: 163 | return "404. Not Available." 164 | 165 | print("Bot has started.") 166 | print("Do visit @BotzHub..") 167 | BotzHub.run_until_disconnected() 168 | --------------------------------------------------------------------------------