Skip to content

Instantly share code, notes, and snippets.

@nuhmanpk
Last active March 17, 2022 13:44
Show Gist options
  • Save nuhmanpk/21b7ac507980afe85e32b654f6fe3879 to your computer and use it in GitHub Desktop.
Save nuhmanpk/21b7ac507980afe85e32b654f6fe3879 to your computer and use it in GitHub Desktop.
Broadcasting in Bots Pyrogram
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()
#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
@rajeshsaini2115
Copy link

in userbase.py line 5 in env what is var?

@nuhmanpk
Copy link
Author

through app.json

@rajeshsaini2115
Copy link

rajeshsaini2115 commented Nov 30, 2021

what is var should be set in heroku like DATABASE_URL ?

@nuhmanpk
Copy link
Author

nuhmanpk commented Dec 4, 2021

Any name as you wish, import forget to import it to main module

@y4kupkaya
Copy link

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?

@nuhmanpk
Copy link
Author

nuhmanpk commented Dec 31, 2021

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

@vyshnavdas
Copy link

Says update not defined

@bughunter0

@nuhmanpk
Copy link
Author

nuhmanpk commented Jan 9, 2022

Use async def function (bot, update): @Vasubai

@mnmsby
Copy link

mnmsby commented Mar 16, 2022

how to add this code to a project....

@nuhmanpk
Copy link
Author

@mnmsby

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