diff --git a/README.md b/README.md index 43334ed54..b3696df5f 100644 --- a/README.md +++ b/README.md @@ -83,10 +83,12 @@ python3 main.py ### Variables * `API_HASH` Your API Hash from my.telegram.org -* `API_ID` Your API ID from my.telegram.org +* `APP_ID` Your API ID from my.telegram.org * `TG_BOT_TOKEN` Your bot token from @BotFather * `OWNER_ID` Must enter Your Telegram Id * `CHANNEL_ID` Your Channel ID eg:- -100xxxxxxxx +* `DATABASE_URL` Your mongo db url +* `DATABASE_NAME` Your mongo db session name * `ADMINS` Optional: A space separated list of user_ids of Admins, they can only create links * `START_MESSAGE` Optional: start message of bot, use HTML and fillings * `FORCE_SUB_MESSAGE`Optional:Force sub message of bot, use HTML and Fillings diff --git a/app.json b/app.json index ded376d9b..7e8cd5e04 100644 --- a/app.json +++ b/app.json @@ -7,7 +7,7 @@ "sharing" ], "repository": "https://github.com/CodeXBotz/File-Sharing-Bot", - "logo": "https://telegra.ph/file/14d3013fda21281c54b61.jpg", + "logo": "https://ibb.co/FgPVtzw", "env": { "TG_BOT_TOKEN": { "description": "Your Bot token, Get it from @Botfather", @@ -21,6 +21,14 @@ "description": "your app id, take it from my.telegram.org", "value": "" }, + "DATABASE_URL": { + "description": "Paste your mongo db url", + "value": "url" + }, + "DATABASE_NAME":{ + "description": "Enter your DATABASE_NAME ", + "value": "filesharexbot" + }, "API_HASH":{ "description": "your api hash, take it from my.telegram.org", "value": "" @@ -52,14 +60,6 @@ "required": false } }, - "addons": [ - { - "plan": "heroku-postgresql", - "options": { - "version": "12" - } - } - ], "buildpacks": [ { "url": "heroku/python" diff --git a/bot.py b/bot.py index ca9b32610..d19402db2 100644 --- a/bot.py +++ b/bot.py @@ -8,6 +8,17 @@ from config import API_HASH, APP_ID, LOGGER, TG_BOT_TOKEN, TG_BOT_WORKERS, FORCE_SUB_CHANNEL, CHANNEL_ID + +name =""" +░█████╗░░█████╗░██████╗░███████╗██╗░░██╗██████╗░░█████╗░████████╗███████╗ +██╔══██╗██╔══██╗██╔══██╗██╔════╝╚██╗██╔╝██╔══██╗██╔══██╗╚══██╔══╝╚════██║ +██║░░╚═╝██║░░██║██║░░██║█████╗░░░╚███╔╝░██████╦╝██║░░██║░░░██║░░░░░███╔═╝ +██║░░██╗██║░░██║██║░░██║██╔══╝░░░██╔██╗░██╔══██╗██║░░██║░░░██║░░░██╔══╝░░ +╚█████╔╝╚█████╔╝██████╔╝███████╗██╔╝╚██╗██████╦╝╚█████╔╝░░░██║░░░███████╗ +░╚════╝░░╚════╝░╚═════╝░╚══════╝╚═╝░░╚═╝╚═════╝░░╚════╝░░░░╚═╝░░░╚══════╝ +""" + + class Bot(Client): def __init__(self): super().__init__( @@ -53,6 +64,14 @@ async def start(self): self.set_parse_mode(ParseMode.HTML) self.LOGGER(__name__).info(f"Bot Running..!\n\nCreated by \nhttps://t.me/CodeXBotz") + self.LOGGER(__name__).info(f""" \n\n +░█████╗░░█████╗░██████╗░███████╗██╗░░██╗██████╗░░█████╗░████████╗███████╗ +██╔══██╗██╔══██╗██╔══██╗██╔════╝╚██╗██╔╝██╔══██╗██╔══██╗╚══██╔══╝╚════██║ +██║░░╚═╝██║░░██║██║░░██║█████╗░░░╚███╔╝░██████╦╝██║░░██║░░░██║░░░░░███╔═╝ +██║░░██╗██║░░██║██║░░██║██╔══╝░░░██╔██╗░██╔══██╗██║░░██║░░░██║░░░██╔══╝░░ +╚█████╔╝╚█████╔╝██████╔╝███████╗██╔╝╚██╗██████╦╝╚█████╔╝░░░██║░░░███████╗ +░╚════╝░░╚════╝░╚═════╝░╚══════╝╚═╝░░╚═╝╚═════╝░░╚════╝░░░░╚═╝░░░╚══════╝ + """) self.username = usr_bot_me.username async def stop(self, *args): diff --git a/config.py b/config.py index f06166635..e01596df7 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,14 @@ +#(©)CodeXBotz + + + + import os import logging from logging.handlers import RotatingFileHandler + + #Bot token @Botfather TG_BOT_TOKEN = os.environ.get("TG_BOT_TOKEN", "") @@ -19,6 +26,7 @@ #Database DB_URI = os.environ.get("DATABASE_URL", "") +DB_NAME = os.environ.get("DATABASE_NAME", "filesharexbot") #force sub channel id, if you want enable force sub FORCE_SUB_CHANNEL = int(os.environ.get("FORCE_SUB_CHANNEL", "0")) diff --git a/database/database.py b/database/database.py new file mode 100644 index 000000000..7eb1ad905 --- /dev/null +++ b/database/database.py @@ -0,0 +1,39 @@ +#(©)CodeXBotz + + + + +import pymongo, os +from config import DB_URI, DB_NAME + + +dbclient = pymongo.MongoClient(DB_URI) +database = dbclient[DB_NAME] + + +user_data = database['users'] + + + +async def present_user(user_id : int): + found = user_data.find_one({'_id': user_id}) + if found: + return True + else: + return False + +async def add_user(user_id: int): + user_data.insert_one({'_id': user_id}) + return + +async def full_userbase(): + user_docs = user_data.find() + user_ids = [] + for doc in user_docs: + user_ids.append(doc['_id']) + + return user_ids + +async def del_user(user_id: int): + user_data.delete_one({'_id': user_id}) + return \ No newline at end of file diff --git a/database/sql.py b/database/sql.py deleted file mode 100644 index a5dadecc5..000000000 --- a/database/sql.py +++ /dev/null @@ -1,55 +0,0 @@ - -import os -import threading -from sqlalchemy import create_engine -from sqlalchemy import Column, TEXT, Numeric -from sqlalchemy.ext.declarative import declarative_base -from config import DB_URI -from sqlalchemy.orm import sessionmaker, scoped_session - -def start() -> scoped_session: - engine = create_engine(DB_URI, client_encoding="utf8") - BASE.metadata.bind = engine - BASE.metadata.create_all(engine) - return scoped_session(sessionmaker(bind=engine, autoflush=False)) - - -BASE = declarative_base() -SESSION = start() - -INSERTION_LOCK = threading.RLock() - -class Broadcast(BASE): - __tablename__ = "broadcast" - id = Column(Numeric, primary_key=True) - user_name = Column(TEXT) - - def __init__(self, id, user_name): - self.id = id - self.user_name = user_name - -Broadcast.__table__.create(checkfirst=True) - - -# Add user details - -async def add_user(id, user_name): - with INSERTION_LOCK: - msg = SESSION.query(Broadcast).get(id) - if not msg: - usr = Broadcast(id, user_name) - SESSION.add(usr) - SESSION.commit() - else: - pass - -async def full_userbase(): - users = SESSION.query(Broadcast).all() - SESSION.close() - return users - -async def query_msg(): - try: - query = SESSION.query(Broadcast.id).order_by(Broadcast.id) - return query - finally: - SESSION.close() diff --git a/plugins/start.py b/plugins/start.py index 7330c5539..c0081ec6e 100644 --- a/plugins/start.py +++ b/plugins/start.py @@ -1,4 +1,8 @@ #(©)CodeXBotz + + + + import os import asyncio from pyrogram import Client, filters, __version__ @@ -7,28 +11,21 @@ from pyrogram.errors import FloodWait, UserIsBlocked, InputUserDeactivated from bot import Bot -from config import ADMINS, FORCE_MSG, START_MSG, OWNER_ID, CUSTOM_CAPTION, DISABLE_CHANNEL_BUTTON, PROTECT_CONTENT +from config import ADMINS, FORCE_MSG, START_MSG, CUSTOM_CAPTION, DISABLE_CHANNEL_BUTTON, PROTECT_CONTENT from helper_func import subscribed, encode, decode, get_messages -from database.sql import add_user, query_msg, full_userbase - - -#=====================================================================================## - -WAIT_MSG = """"Processing ...""" +from database.database import add_user, del_user, full_userbase, present_user -REPLY_ERROR = """Use this command as a replay to any telegram message with out any spaces.""" -#=====================================================================================## @Bot.on_message(filters.command('start') & filters.private & subscribed) async def start_command(client: Client, message: Message): id = message.from_user.id - user_name = '@' + message.from_user.username if message.from_user.username else None - try: - await add_user(id, user_name) - except: - pass + if not await present_user(id): + try: + await add_user(id) + except: + pass text = message.text if len(text)>7: try: @@ -110,6 +107,17 @@ async def start_command(client: Client, message: Message): ) return + +#=====================================================================================## + +WAIT_MSG = """"Processing ...""" + +REPLY_ERROR = """Use this command as a replay to any telegram message with out any spaces.""" + +#=====================================================================================## + + + @Bot.on_message(filters.command('start') & filters.private) async def not_joined(client: Client, message: Message): buttons = [ @@ -153,7 +161,7 @@ async def get_users(client: Bot, message: Message): @Bot.on_message(filters.private & filters.command('broadcast') & filters.user(ADMINS)) async def send_text(client: Bot, message: Message): if message.reply_to_message: - query = await query_msg() + query = await full_userbase() broadcast_msg = message.reply_to_message total = 0 successful = 0 @@ -162,8 +170,7 @@ async def send_text(client: Bot, message: Message): unsuccessful = 0 pls_wait = await message.reply("Broadcasting Message.. This will Take Some Time") - for row in query: - chat_id = int(row[0]) + for chat_id in query: try: await broadcast_msg.copy(chat_id) successful += 1 @@ -172,8 +179,10 @@ async def send_text(client: Bot, message: Message): await broadcast_msg.copy(chat_id) successful += 1 except UserIsBlocked: + await del_user(chat_id) blocked += 1 except InputUserDeactivated: + await del_user(chat_id) deleted += 1 except: unsuccessful += 1 diff --git a/requirements.txt b/requirements.txt index ea1a7787f..ea4999579 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,7 +3,5 @@ pyrogram TgCrypto Pyromod # --- For-Database ------------ # -sqlalchemy~=1.3.23 -psycopg2-binary -feedparser - +pymongo +dnspython