def broadcast(bot: Bot, update: Update): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: chats = sql.get_all_chats() or [] users = get_all_users() failed = 0 failed_user = 0 for chat in chats: try: bot.sendMessage(int(chat.chat_id), to_send[1]) sleep(0.1) except TelegramError: failed += 1 LOGGER.warning("Couldn't send broadcast to %s, group name %s", str(chat.chat_id), str(chat.chat_name)) for user in users: try: bot.sendMessage(int(user.user_id), to_send[1]) sleep(0.1) except TelegramError: failed_user += 1 LOGGER.warning("Couldn't send broadcast to %s", str(user.user_id)) update.effective_message.reply_text( f"Broadcast complete. {failed} groups failed to receive the message, probably due to being kicked. {failed_user} failed to receive message, probably due to being blocked" )
def get(): '''Get a list of all users known to the bot. Note that this does only include people who messaged the bot at least once.''' key = request.args.get('api_key') if not key: abort(HTTPStatus.UNAUTHORIZED, "Unauthorized") return user_id = sql.verify_key(key) if user_id is None: abort(HTTPStatus.UNAUTHORIZED, "Invalid Key") isowner = user_id is OWNER_ID iscowoner = user_id is CO_OWNER_ID isudo = user_id in SUDO_USERS isadmin = isowner or iscowoner or isudo if not isadmin: abort(HTTPStatus.FORBIDDEN, "User is not bot admin.") else: users = get_all_users() result = [] list_groups_str = request.args.get('list_groups') if list_groups_str: if list_groups_str in ("True", "true"): list_groups = True if list_groups_str in ("False", "false"): list_groups = False if list_groups_str.lower() not in ("false", "true"): abort(HTTPStatus.BAD_REQUEST, "'list_groups' is invalid.") else: list_groups = False for user in users: data = {} try: data.update({"id": user.user_id}) chat = dispatcher.bot.get_chat(user.user_id) if chat.username: data.update({"username": chat.username}) data.update({"link": chat.link}) data.update({"first_name": chat.first_name}) if chat.last_name: data.update({"last_name": chat.last_name}) if list_groups: groups = [] for group in get_chats_by_member(chat.id): groups.append([{"id": group.chat, "name": get_chatname(group.chat)}]) data.update({"groups": groups}) result.append(data) except TelegramError as excp: if excp.message == 'Chat not found': pass return {"users": result}, HTTPStatus.OK
def userbroadcast(bot: Bot, update: Update): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: users = sql.get_all_users() or [] failed = 0 success = 0 for user in users: try: bot.sendMessage(int(user.user_id), to_send[1]) success += 1 LOGGER.warning("Sent broadcast to %s, username %s, Count: %s", str(user.user_id), str(user.username), str(success)) sleep(0.5) except TelegramError: failed += 1 # LOGGER.warning("Couldn't send broadcast to %s, username %s", str(user.user_id), str(user.username)) update.effective_message.reply_text("Broadcast complete.\n{} users failed\n{} users received".format(failed, success))
def broadcast(bot: Bot, update: Update): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: users = get_all_users() failed_user = 0 for user in users: try: bot.sendMessage(int(user.user_id), to_send[1]) sleep(0.1) except TelegramError: failed_user += 1 LOGGER.warning("Couldn't send broadcast to %s", str(user.user_id)) update.effective_message.reply_text( f"ok {failed_user} failed to receive message, probably due to being blocked" )
def broadcast(update: Update, context: CallbackContext): to_send = update.effective_message.text.split(None, 1) if len(to_send) >= 2: to_group = False to_user = False if to_send[0] == "/broadcastgroups": to_group = True if to_send[0] == "/broadcastusers": to_user = True else: to_group = to_user = True chats = sql.get_all_chats() or [] users = get_all_users() failed = 0 failed_user = 0 if to_group: for chat in chats: try: context.bot.sendMessage( int(chat.chat_id), to_send[1], parse_mode="MARKDOWN", disable_web_page_preview=True, ) sleep(0.1) except TelegramError: failed += 1 if to_user: for user in users: try: context.bot.sendMessage( int(user.user_id), to_send[1], parse_mode="MARKDOWN", disable_web_page_preview=True, ) sleep(0.1) except TelegramError: failed_user += 1 update.effective_message.reply_text( f"Broadcast complete.\nGroups failed: {failed}.\nUsers failed: {failed_user}." )