Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broadcast.py #21

Closed
wants to merge 23 commits into from
Closed

broadcast.py #21

wants to merge 23 commits into from

Conversation

Thiyaku17
Copy link

(c) @AbirHasan2005

import time
import string
import random
import asyncio
import datetime
import aiofiles.os
import traceback
from configs import Config
from helpers.database.access_db import db
from pyrogram.types import Message
from pyrogram.errors import FloodWait, InputUserDeactivated, UserIsBlocked, PeerIdInvalid

broadcast_ids = {}

async def send_msg(user_id, message):
try:
if Config.BROADCAST_AS_COPY is False:
await message.forward(chat_id=user_id)
elif Config.BROADCAST_AS_COPY is True:
await message.copy(chat_id=user_id)
return 200, None
except FloodWait as e:
await asyncio.sleep(e.x)
return send_msg(user_id, message)
except InputUserDeactivated:
return 400, f"{user_id} : deactivated\n"
except UserIsBlocked:
return 400, f"{user_id} : blocked the bot\n"
except PeerIdInvalid:
return 400, f"{user_id} : user id invalid\n"
except Exception as e:
return 500, f"{user_id} : {traceback.format_exc()}\n"

async def broadcast_handler(m: Message):
all_users = await db.get_all_users()
broadcast_msg = m.reply_to_message
while True:
broadcast_id = ''.join([random.choice(string.ascii_letters) for i in range(3)])
if not broadcast_ids.get(broadcast_id):
break
out = await m.reply_text(
text=f"Broadcast Started! You will be notified with log file when all the users are notified."
)
start_time = time.time()
total_users = await db.total_users_count()
done = 0
failed = 0
success = 0
broadcast_ids[broadcast_id] = dict(
total=total_users,
current=done,
failed=failed,
success=success
)
async with aiofiles.open('broadcast.txt', 'w') as broadcast_log_file:
async for user in all_users:
sts, msg = await send_msg(
user_id=int(user['id']),
message=broadcast_msg
)
if msg is not None:
await broadcast_log_file.write(msg)
if sts == 200:
success += 1
else:
failed += 1
if sts == 400:
await db.delete_user(user['id'])
done += 1
if broadcast_ids.get(broadcast_id) is None:
break
else:
broadcast_ids[broadcast_id].update(
dict(
current=done,
failed=failed,
success=success
)
)
if broadcast_ids.get(broadcast_id):
broadcast_ids.pop(broadcast_id)
completed_in = datetime.timedelta(seconds=int(time.time() - start_time))
await asyncio.sleep(3)
await out.delete()
if failed == 0:
await m.reply_text(
text=f"broadcast completed in {completed_in}\n\nTotal users {total_users}.\nTotal done {done}, {success} success and {failed} failed.",
quote=True
)
else:
await m.reply_document(
document='broadcast.txt',
caption=f"broadcast completed in {completed_in}\n\nTotal users {total_users}.\nTotal done {done}, {success} success and {failed} failed.",
quote=True
)
await aiofiles.os.remove('broadcast.txt')

@Thiyaku17
Copy link
Author

Plzz accept this change

@Charles-Hernandez
Copy link

Clone this Repo, and Commit your Edits in Cloned repo only

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants