-
Notifications
You must be signed in to change notification settings - Fork 51
/
bot.py
86 lines (73 loc) · 2.75 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
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
Bitly Bot
© All rights reserved by Mrvishal2k2
Kangers don't f*ckin kang this !!!
Should have to give credits 😏 else f***off
This is only for personal use Dont use this for ur bot channel business 😂
Thanks to Mahesh Malekar for his Gplinks Bot !!
"""
import logging
from pyrogram import Client, filters, idle
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from pyshorteners import Shortener
from config import API_ID, API_HASH, BOT_TOKEN, BITLY_KEY, OWNER
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
log = logging.getLogger(__name__)
logging.getLogger("pyrogram").setLevel(logging.WARNING)
SHORTLINKBOT = Client('ShortlinkBot',
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN,
workers=50,
sleep_threshold=10)
@SHORTLINKBOT.on_message(filters.command(['start', 'help']))
async def start(_, update):
"""Start Message"""
markup = InlineKeyboardMarkup([[InlineKeyboardButton("My Owner 👮", url=f"https://t.me/{OWNER}")]])
await update.reply(
f"**Hi {update.chat.first_name}!**\n\n"
"I'm shortlink bot. Just send me link and get adsless short link",
reply_markup=markup,
quote=True)
@SHORTLINKBOT.on_message(filters.regex(r'https?://[^\s]+'))
async def link_handler(_, update):
"""Link Handler"""
link = update.matches[0].group(0)
shortened_url, Err = get_shortlink(link)
if shortened_url is None:
message = f"Something went wrong \n{Err}"
await update.reply(message, quote=True)
return
message = f"Here is your shortlink\n {shortened_url}"
markup = InlineKeyboardMarkup(
[
[
InlineKeyboardButton("Open Link 🔗", url=shortened_url)
]
]
)
# i don't think this bot with get sending message error so no need of exceptions
await update.reply_text(text=message, reply_markup=markup, quote=True)
def get_shortlink(url):
"""Get Shortlink"""
shortened_url = None
Err = None
try:
if BITLY_KEY:
# Bittly Shorten
s = Shortener(api_key=BITLY_KEY)
shortened_url = s.bitly.short(url)
else:
# Da.gd : I prefer this
s = Shortener()
shortened_url = s.dagd.short(url)
except Exception as error: # skipcq: PYL-W0703 - sometimes it can give error.
Err = f"#ERROR: {error}"
log.info(Err)
return shortened_url, Err
if __name__ == "__main__" :
SHORTLINKBOT.start()
log.info(">>Bot-Started<<")
idle()
SHORTLINKBOT.stop()
log.info(">>Bot-Stopped<<")