-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathbot.py
57 lines (44 loc) · 2.17 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# ©AKBOTZ
import re
import aiohttp
from os import environ
from pyrogram import Client, filters
from pyrogram.types import *
API_ID = environ.get('API_ID')
API_HASH = environ.get('API_HASH')
BOT_TOKEN = environ.get('BOT_TOKEN')
API_KEY = environ.get('API_KEY')
API_URL = environ.get('API_URL')
akbotz = Client('link shortener bot',
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN,
workers=100)
print("Developer: @AKBotZ , Join & Share Channel")
print("Bot is Started Now")
@akbotz.on_message(filters.command('start') & filters.private)
async def start(bot, message):
await message.reply(
f"**Hi {message.chat.first_name}!**\n\n"
"I'm Link Shortener bot. Just send me link and get short link, You can also send multiple links seperated by a space or enter.\n\n**Developer:** @AKBotZ")
@akbotz.on_message(filters.private & filters.text & filters.incoming)
async def link_handler(bot, message):
link_pattern = re.compile('https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,}', re.DOTALL)
links = re.findall(link_pattern, message.text)
if len(links) <1:
await message.reply("No links Found in this text",quote=True)
return
for link in links:
try:
short_link = await get_shortlink(link)
await message.reply(f"𝐇𝐞𝐫𝐞 𝐢𝐬 𝐘𝐨𝐮𝐫 𝐒𝐡𝐨𝐫𝐭𝐞𝐧𝐞𝐝 𝐋𝐢𝐧𝐤\n\n𝐎𝐫𝐢𝐠𝐢𝐧𝐚𝐥 𝐋𝐢𝐧𝐤: {link}\n\n𝐒𝐡𝐨𝐫𝐭𝐞𝐧𝐞𝐝 𝐋𝐢𝐧𝐤: `{short_link}`",quote=True,disable_web_page_preview=True)
except Exception as e:
await message.reply(f'𝐄𝐫𝐫𝐨𝐫: `{e}`', quote=True)
async def get_shortlink(link):
url = API_URL
params = {'api': API_KEY, 'url': link}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, raise_for_status=True) as response:
data = await response.json()
return data["shortenedUrl"]
akbotz.run()