Skip to content

Commit

Permalink
Merge pull request #183 from SHABIN-K/main
Browse files Browse the repository at this point in the history
Added mongo db support
  • Loading branch information
SHABIN-K committed Nov 5, 2022
2 parents 8dec82d + 066cf50 commit febebe2
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 86 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href='https://github.com/codexbotz/File-Sharing-Bot/blob/main/README.md#start_message'>fillings</a>
* `FORCE_SUB_MESSAGE`Optional:Force sub message of bot, use HTML and Fillings
Expand Down
18 changes: 9 additions & 9 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": ""
Expand Down Expand Up @@ -52,14 +60,6 @@
"required": false
}
},
"addons": [
{
"plan": "heroku-postgresql",
"options": {
"version": "12"
}
}
],
"buildpacks": [
{
"url": "heroku/python"
Expand Down
19 changes: 19 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand Down Expand Up @@ -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):
Expand Down
8 changes: 8 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -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", "")

Expand All @@ -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"))
Expand Down
39 changes: 39 additions & 0 deletions database/database.py
Original file line number Diff line number Diff line change
@@ -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
55 changes: 0 additions & 55 deletions database/sql.py

This file was deleted.

43 changes: 26 additions & 17 deletions plugins/start.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#(©)CodeXBotz




import os
import asyncio
from pyrogram import Client, filters, __version__
Expand All @@ -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 = """"<b>Processing ...</b>"""
from database.database import add_user, del_user, full_userbase, present_user

REPLY_ERROR = """<code>Use this command as a replay to any telegram message with out any spaces.</code>"""

#=====================================================================================##


@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:
Expand Down Expand Up @@ -110,6 +107,17 @@ async def start_command(client: Client, message: Message):
)
return


#=====================================================================================##

WAIT_MSG = """"<b>Processing ...</b>"""

REPLY_ERROR = """<code>Use this command as a replay to any telegram message with out any spaces.</code>"""

#=====================================================================================##



@Bot.on_message(filters.command('start') & filters.private)
async def not_joined(client: Client, message: Message):
buttons = [
Expand Down Expand Up @@ -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
Expand All @@ -162,8 +170,7 @@ async def send_text(client: Bot, message: Message):
unsuccessful = 0

pls_wait = await message.reply("<i>Broadcasting Message.. This will Take Some Time</i>")
for row in query:
chat_id = int(row[0])
for chat_id in query:
try:
await broadcast_msg.copy(chat_id)
successful += 1
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@ pyrogram
TgCrypto
Pyromod
# --- For-Database ------------ #
sqlalchemy~=1.3.23
psycopg2-binary
feedparser

pymongo
dnspython

0 comments on commit febebe2

Please sign in to comment.