async def broadcast(message): chats = mongodb.chat_list.find({}) raw_text = message.get_args() text, buttons = button_parser(message.chat.id, raw_text) if len(buttons) == 0: buttons = None msg = await message.reply("Broadcasting to {} chats...".format( chats.count())) num_succ = 0 num_fail = 0 for chat in chats: try: await tbot.send_message(chat['chat_id'], text, buttons=buttons) num_succ = num_succ + 1 except Exception as err: num_fail = num_fail + 1 await msg.edit_text( "Error:\n<code>{}</code>.\nBroadcasting will continues.". format(err)) await asyncio.sleep(2) await msg.edit_text("Broadcasting to {} chats...".format( chats.count())) await msg.edit_text( "**Broadcast completed!** Message sended to <code>{}</code> chats successfully, \ <code>{}</code> didn't received message.".format(num_succ, num_fail))
async def check_message_for_smartbroadcast(event): chat_id = event.chat_id match = mongodb.sbroadcast_list.find_one({'chat_id': chat_id}) if match: try: raw_text = mongodb.sbroadcast_settings.find_one({})['text'] text, buttons = button_parser(event.chat_id, raw_text) if len(buttons) == 0: buttons = None await bot.send_message(chat_id, text, buttons=buttons) except Exception as err: logger.error(err) mongodb.sbroadcast_list.delete_one({'chat_id': chat_id}) old = mongodb.sbroadcast_settings.find_one({}) num = old['recived_chats'] + 1 mongodb.sbroadcast_settings.update( {'_id': old['_id']}, { 'text': old['text'], 'all_chats': old['all_chats'], 'recived_chats': num }, upsert=False)
async def broadcast(event): chats = mongodb.chat_list.find({}) raw_text = event.message.text.split(" ", 1)[1] text, buttons = button_parser(event.chat_id, raw_text) if len(buttons) == 0: buttons = None msg = await event.reply("Broadcasting to {} chats...".format(chats.count())) num_succ = 0 num_fail = 0 for chat in chats: try: await bot.send_message(chat['chat_id'], text, buttons=buttons) num_succ = num_succ + 1 except Exception as err: num_fail = num_fail + 1 await msg.edit("Error:\n`{}`.\nBroadcasting will continues.".format(err)) await asyncio.sleep(2) await msg.edit("Broadcasting to {} chats...".format(chats.count())) await msg.edit( "**Broadcast completed!** Message sended to `{}` chats successfully, \ `{}` didn't received message.".format(num_succ, num_fail))