Last active
March 17, 2022 13:44
-
-
Save nuhmanpk/21b7ac507980afe85e32b654f6fe3879 to your computer and use it in GitHub Desktop.
Broadcasting in Bots Pyrogram
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from userbase import present_in_userbase, add_to_userbase, get_users # userbase.py is Attached below | |
import time | |
@bughunter0.on_message(filters.private & filters.command('broadcast') & filters.user(OWNER) & filters.reply) | |
async def broadcast(client: bughunter0, message: Message): | |
broadcast_msg = message.reply_to_message | |
txt = await message.reply(text = 'Staring....') | |
user_ids = await get_users() | |
success = 0 | |
deleted = 0 | |
blocked = 0 | |
await txt.edit(text = 'Broadcasting message, Please wait', reply_markup = None) | |
for user_id in user_ids: | |
try: | |
broadcast_msg = await broadcast_msg.copy( | |
chat_id =user_id , | |
reply_to_message_id = broadcast_msg.message_id | |
) | |
success += 1 | |
time.sleep(3) | |
except FloodWait as e: | |
await asyncio.sleep(e.x) | |
success += 1 | |
except UserIsBlocked: | |
blocked += 1 | |
except InputUserDeactivated: | |
deleted += 1 | |
text = f"""<b>Broadcast Completed</b> | |
Total users: {str(len(user_ids))} | |
Deleted accounts: {str(deleted)} """ | |
await message.reply(text=text) | |
await message.delete() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#setting up db using pymongo | |
import pymongo | |
from pymongo import MongoClient | |
dbclient = pymongo.MongoClient("mongodb+srv:// Paste it or By Env") | |
database = dbclient["Name"] | |
user_collection = database['users'] | |
async def present_in_userbase(user_id : int): | |
found = user_collection.find_one({'_id': user_id}) | |
if found: | |
return True | |
else: | |
return False | |
async def add_to_userbase(user_id: int): | |
user_collection.insert_one({'_id': user_id}) | |
return | |
async def get_users(): | |
user_docs = user_collection.find() | |
user_ids = [] | |
for doc in user_docs: | |
user_ids.append(doc['_id']) | |
return user_ids | |
async def del_from_userbase(user_id: int): | |
user_collection.delete_one({'_id': user_id}) | |
return |
through app.json
what is var should be set in heroku like DATABASE_URL ?
Any name as you wish, import forget to import it to main module
Where is the part that adds it to the database when the bot is started in a group and privately? And how is it set?
if not await present_in_userbase(update.from_user.id):
await add_to_userbase(update.from_user.id)
@aylak-github use this to add the user to DB
Says update not defined
Use async def function (bot, update):
@Vasubai
how to add this code to a project....
First save the userbase.py file as it is ,
Then paste the code from broadcast.py to your main module
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
in userbase.py line 5 in env what is var?