-
Notifications
You must be signed in to change notification settings - Fork 37
/
bot.py
73 lines (58 loc) · 2.15 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import aiohttp
from pyrogram import Client, filters
API_ID = os.environ.get('API_ID')
API_HASH = os.environ.get('API_HASH')
BOT_TOKEN = os.environ.get('BOT_TOKEN')
API_KEY = os.environ.get('API_KEY')
WEBSITE = os.environ.get('WEBSITE')
try:
bot = Client('shortener bot',
api_id=int(API_ID),
api_hash=API_HASH,
plugins = dict(root="plugins"),
bot_token=BOT_TOKEN,
workers=50,
sleep_threshold=10)
except Exception:
print("Add var values properly. Read readme.md once")
@bot.on_message(filters.command('start'))
async def start(bot, message):
start_msg = f"""
Hi {message.chat.first_name}!
I'm {WEBSITE} bot. Just send me link and get short link!
Send me a link to short a link with random alias.
For custom alias, <code>[link] | [custom_alias]</code>, Send in this format\n
Ex: https://t.me/example | Example
"""
await message.reply_text(start_msg, disable_web_page_preview=True, quote=True)
@bot.on_message(filters.regex(r'https?://[^\s]+') & filters.private)
async def link_handler(bot, message):
if "|" in message.text:
link_parts = message.text.split("|")
link = link_parts[0]
aliases = link_parts[1:len(message.text) + 1]
alias1 = ""
for alias in aliases:
alias1 += alias
x = alias1.replace(" ", "")
else:
link = message.matches[0].group(0)
x= ""
short_link = await get_shortlink(link, x)
await message.reply(short_link, quote=True)
async def get_shortlink(link, x):
url = f'https://{WEBSITE}/api'
params = {'api': API_KEY,
'url': link,
'alias': x
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, raise_for_status=True, ssl=False) as response:
data = await response.json()
print(data["status"])
if data["status"] == "success":
return f"<code>{data['shortenedUrl']}</code>\n\nHere is your Link:\n{data['shortenedUrl']}"
else:
return f"Error: {data['message']}"
bot.run()