Skip to content

Commit

Permalink
Added Batch support (Ns-Bots#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ns-AnoNymouS authored May 24, 2021
1 parent 4701ab5 commit 38ead6d
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 16 deletions.
1 change: 1 addition & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
logging.getLogger().setLevel(logging.ERROR)
logging.getLogger("pyrogram").setLevel(logging.WARNING)

from pyromod import listen
from pyrogram import Client
API_ID = int(os.environ.get("API_ID", 0))
API_HASH = os.environ.get("API_HASH", None)
Expand Down
20 changes: 19 additions & 1 deletion plugins/callback.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
logging.getLogger().setLevel(logging.ERROR)
logging.getLogger("pyrogram").setLevel(logging.WARNING)

from .commands import start
from .commands import start, BATCH
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
DB_CHANNEL_ID = os.environ.get("DB_CHANNEL_ID")
OWNER_ID = os.environ.get("OWNER_ID")


Expand Down Expand Up @@ -97,3 +98,20 @@ async def about_cb(c, m):
async def home_cb(c, m):
await m.answer()
await start(c, m, cb=True)


@Client.on_callback_query(filters.regex('^done$'))
async def done_cb(c, m):
BATCH.remove(m.from_user.id)
c.cancel_listener(m.from_user.id)
await m.message.delete()


@Client.on_callback_query(filters.regex('^delete'))
async def delete_cb(c, m):
await m.answer()
cmd, msg_id = m.data.split("+")
chat_id = m.from_user.id if not DB_CHANNEL_ID else int(DB_CHANNEL_ID)
message = await c.get_messages(chat_id, int(msg_id))
await message.delete()
await m.message.edit("Deleted files successfully πŸ‘¨β€βœˆοΈ")
86 changes: 75 additions & 11 deletions plugins/commands.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
import os
import asyncio
import logging
import logging.config

# Get logging configurations
logging.getLogger().setLevel(logging.ERROR)
logging.getLogger("pyrogram").setLevel(logging.WARNING)

import base64
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from pyrogram.errors import ListenerCanceled
DB_CHANNEL_ID = os.environ.get("DB_CHANNEL_ID")
OWNER_ID = os.environ.get("OWNER_ID")
BATCH = []


@Client.on_message(filters.command('start') & filters.incoming & filters.private)
async def start(c, m, cb=False):
if not cb:
send_msg = await m.reply_text("**Processing...**", quote=True)

owner = await c.get_users(int(OWNER_ID))
owner_username = owner.username if owner.username else 'Ns_bot_updates'

Expand Down Expand Up @@ -48,12 +54,36 @@ async def start(c, m, cb=False):
)

if len(m.command) > 1: # sending the stored file
try:
m.command[1] = await decode(m.command[1])
except:
pass

if 'batch_' in m.command[1]:
await send_msg.delete()
cmd, chat_id, message = m.command[1].split('_')
string = await c.get_messages(int(chat_id), int(message)) if not DB_CHANNEL_ID else await c.get_messages(int(DB_CHANNEL_ID), int(message))

if string.empty:
owner = await c.get_users(int(OWNER_ID))
return await m.reply_text(f"πŸ₯΄ Sorry bro your file was deleted by file owner or bot owner\n\nFor more help contact my owner πŸ‘‰ {owner.mention(style='md')}")
message_ids = (await decode(string.text)).split('-')
for msg_id in message_ids:
msg = await c.get_messages(int(chat_id), int(msg_id)) if not DB_CHANNEL_ID else await c.get_messages(int(DB_CHANNEL_ID), int(msg_id))

if msg.empty:
owner = await c.get_users(int(OWNER_ID))
return await m.reply_text(f"πŸ₯΄ Sorry bro your file was deleted by file owner or bot owner\n\nFor more help contact my owner πŸ‘‰ {owner.mention(style='md')}")

await msg.copy(m.from_user.id)
await asyncio.sleep(1)
return

chat_id, msg_id = m.command[1].split('_')
msg = await c.get_messages(int(chat_id), int(msg_id)) if not DB_CHANNEL_ID else await c.get_messages(int(DB_CHANNEL_ID), int(msg_id))

if msg.empty:
owner = await c.get_users(int(OWNER_ID))
return await m.reply_text(f"πŸ₯΄ Sorry bro your file was missing\n\nPlease contact my owner πŸ‘‰ {owner.mention(style='md')}")
return await send_msg.edit(f"πŸ₯΄ Sorry bro your file was deleted by file owner or bot owner\n\nFor more help contact my owner πŸ‘‰ {owner.mention(style='md')}")

caption = f"{msg.caption.markdown}\n\n\n" if msg.caption else ""

Expand All @@ -75,19 +105,21 @@ async def start(c, m, cb=False):
caption += f"__πŸ‘€ User Id:__ `{user.id}`\n\n"
caption += f"__πŸ’¬ DC ID:__ {user.dc_id}\n\n" if user.dc_id else ""

await send_msg.delete()
await msg.copy(m.from_user.id, caption=caption)


else: # sending start message
await m.reply_text(
await send_msg.edit(
text=text,
quote=True,
reply_markup=InlineKeyboardMarkup(buttons)
)


@Client.on_message(filters.command('me') & filters.incoming & filters.private)
async def me(c, m):
""" This will be sent when /me command was used"""

me = await c.get_users(m.from_user.id)
text = "--**YOUR DETAILS:**--\n\n\n"
text += f"__🦚 First Name:__ `{me.first_name}`\n\n"
Expand All @@ -105,23 +137,55 @@ async def me(c, m):

@Client.on_message(filters.command('batch') & filters.private & filters.incoming)
async def batch(c, m):
""" This is for batch command"""

BATCH.append(m.from_user.id)
files = []
i = 1

while m.from_user.id in BATCH:
reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton('Done βœ…', callback_data='done')]])
media = await c.ask(chat_id=m.from_user.id, 'Ok πŸ˜‰. Now send me some more files Or press done to get shareable link.', reply_markup=reply_markup)
files.append(media)

if i == 1:
media = await c.ask(chat_id=m.from_user.id, text='Send me some files or videos or photos or text or audio.')
files.append(media)
else:
try:
reply_markup = InlineKeyboardMarkup([[InlineKeyboardButton('Done βœ…', callback_data='done')]])
media = await c.ask(chat_id=m.from_user.id, text='Ok πŸ˜‰. Now send me some more files Or press done to get shareable link.', reply_markup=reply_markup)
files.append(media)
except ListenerCanceled:
pass
except Exception as e:
print(e)
await m.reply_text(text="Something went wrong. Try again later.")
i += 1

message = await m.reply_text("Generating shareable link πŸ”—")
string = ""
for file in files:
if DB_CHANNEL_ID:
copy_message = await file.copy(int(DB_CHANNEL_ID))
else:
copy_message = await file.copy(m.from_user.id)
string += f"{copy_message.message_id}&"
string += f"{copy_message.message_id}-"
await asyncio.sleep(1)

string_base64 = await encode_string(string[:-1])
send = await c.send_message(m.from_user.id, string_base64) if not DB_CHANNEL_ID else await c.send_message(int(DB_CHANNEL_ID), string_base64)
base64_string = await encode_string(f"batch_{m.chat.id}_{send.message_id}")
bot = await c.get_me()
url = f"https://t.me/{bot.username}?start=batch_{m.chat.id}_{string}" if not DB_CHANNEL_ID else f"https://t.me/{bot.username}?start=batch_{m.chat.id}_{string}"
url = f"https://t.me/{bot.username}?start={base64_string}"

await message.edit(text=url)


async def decode(base64_string):
base64_bytes = base64_string.encode("ascii")
string_bytes = base64.b64decode(base64_bytes)
string = string_bytes.decode("ascii")
return string

await m.reply_text(text=url)
async def encode_string(string):
string_bytes = string.encode("ascii")
base64_bytes = base64.b64encode(string_bytes)
base64_string = base64_bytes.decode("ascii")
return base64_string
13 changes: 9 additions & 4 deletions plugins/storefile.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import urllib
from .commands import encode_string
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardButton, InlineKeyboardMarkup
DB_CHANNEL_ID = os.environ.get("DB_CHANNEL_ID")
Expand All @@ -9,7 +10,7 @@
#################################### FOR PRIVATE ################################################
@Client.on_message((filters.document|filters.video|filters.audio|filters.photo) & filters.incoming & ~filters.edited & ~filters.channel)
async def storefile(c, m):

send_message = await m.reply_text("**Processing...**", quote=True)
if m.document:
media = m.document
if m.video:
Expand Down Expand Up @@ -46,18 +47,21 @@ async def storefile(c, m):

# creating urls
bot = await c.get_me()
url = f"https://t.me/{bot.username}?start={m.chat.id}_{m.message_id}" if not DB_CHANNEL_ID else f"https://t.me/{bot.username}?start={m.chat.id}_{msg.message_id}"
base64_string = await encode_string(f"{m.chat.id}_{msg.message_id}")
url = f"https://t.me/{bot.username}?start={base64_string}"
txt = urllib.parse.quote(text.replace('--', ''))
share_url = f"tg://share?url={txt}File%20Link%20πŸ‘‰%20{url}"

# making buttons
buttons = [[
InlineKeyboardButton(text="Open Url πŸ”—", url=url),
InlineKeyboardButton(text="Share Link πŸ‘€", url=share_url)
],[
InlineKeyboardButton(text="Delete πŸ—‘", callback_data=f"delete+{msg.message_id}")
]]

# sending message
await m.reply_text(
await send_message.edit(
text,
reply_markup=InlineKeyboardMarkup(buttons)
)
Expand Down Expand Up @@ -103,7 +107,8 @@ async def storefile_channel(c, m):

# creating urls
bot = await c.get_me()
url = f"https://t.me/{bot.username}?start={m.chat.id}_{m.message_id}" if not DB_CHANNEL_ID else f"https://t.me/{bot.username}?start={m.chat.id}_{msg.message_id}"
base64_string = await encode_string(f"{m.chat.id}_{m.message_id}")
url = f"https://t.me/{bot.username}?start={base64_string}"
txt = urllib.parse.quote(text.replace('--', ''))
share_url = f"tg://share?url={txt}File%20Link%20πŸ‘‰%20{url}"

Expand Down

0 comments on commit 38ead6d

Please sign in to comment.