|
20 | 20 | import bot.utils as utils
|
21 | 21 |
|
22 | 22 | timezone = pytz.timezone("Asia/Singapore")
|
| 23 | +MASTER_TELE_ID = os.environ.get("MASTER_TELE_ID") |
23 | 24 |
|
24 | 25 |
|
25 | 26 | def get_category_text(sheet_id, entry_type):
|
@@ -895,6 +896,65 @@ def add_backlog_entry(update, context) -> int:
|
895 | 896 | return CS.ENTRY
|
896 | 897 |
|
897 | 898 |
|
| 899 | +def send_new_feature_message(context, new_feature_message): |
| 900 | + users = db.get_all_user_id() |
| 901 | + no_of_users = 0 |
| 902 | + errors = [] |
| 903 | + |
| 904 | + for user_id in users: |
| 905 | + try: |
| 906 | + context.bot.send_message(chat_id=user_id, text=new_feature_message) |
| 907 | + no_of_users += 1 |
| 908 | + except Exception as e: |
| 909 | + try: |
| 910 | + chat = context.bot.get_chat(chat_id=user_id) |
| 911 | + username = chat.username if chat.username else "?" |
| 912 | + except Exception: |
| 913 | + username = "?" |
| 914 | + |
| 915 | + errors.append(f"Username @{username} (ID: {user_id}): {e}") |
| 916 | + |
| 917 | + error_message = "\n".join(errors) |
| 918 | + return no_of_users, error_message |
| 919 | + |
| 920 | + |
| 921 | +def notify_all(update, context): |
| 922 | + if str(update.effective_user.id) == MASTER_TELE_ID: |
| 923 | + new_feature_message = update.message.text.partition(" ")[2] |
| 924 | + if not new_feature_message: |
| 925 | + update.message.reply_text("Please provide a message to send.") |
| 926 | + return |
| 927 | + |
| 928 | + keyboard = [ |
| 929 | + [ |
| 930 | + InlineKeyboardButton("Confirm Send", callback_data="confirm_send"), |
| 931 | + InlineKeyboardButton("Cancel", callback_data="cancel_send"), |
| 932 | + ] |
| 933 | + ] |
| 934 | + reply_markup = InlineKeyboardMarkup(keyboard) |
| 935 | + update.message.reply_text( |
| 936 | + f"Preview:\n{new_feature_message}", reply_markup=reply_markup |
| 937 | + ) |
| 938 | + else: |
| 939 | + update.message.reply_text("You are not authorized to use this command.") |
| 940 | + |
| 941 | + |
| 942 | +def notify_preview(update, context): |
| 943 | + query = update.callback_query |
| 944 | + query.answer() |
| 945 | + if query.data == "confirm_send": |
| 946 | + new_feature_message = query.message.text.partition("\n")[2] |
| 947 | + no_of_users, error_message = send_new_feature_message( |
| 948 | + context, new_feature_message |
| 949 | + ) |
| 950 | + response = f"Message sent to {no_of_users} users." |
| 951 | + if error_message: |
| 952 | + response += f"\nErrors:\n{error_message}" |
| 953 | + query.edit_message_text(text=response) |
| 954 | + elif query.data == "cancel_send": |
| 955 | + query.edit_message_text(text="Message sending cancelled.") |
| 956 | + |
| 957 | + |
898 | 958 | def setup_handlers(dispatcher):
|
899 | 959 | # Configuration-related states and handlers
|
900 | 960 | config_states = {
|
@@ -965,8 +1025,13 @@ def setup_handlers(dispatcher):
|
965 | 1025 | },
|
966 | 1026 | fallbacks=[CommandHandler("cancel", cancel)],
|
967 | 1027 | )
|
968 |
| - |
969 | 1028 | dispatcher.add_handler(conv_handler)
|
970 | 1029 |
|
971 | 1030 | help_handler = CommandHandler("help", help)
|
972 | 1031 | dispatcher.add_handler(help_handler)
|
| 1032 | + |
| 1033 | + # Notify all users (admin) |
| 1034 | + notify_all_handler = CommandHandler("notifyall", notify_all) |
| 1035 | + |
| 1036 | + dispatcher.add_handler(CallbackQueryHandler(notify_preview)) |
| 1037 | + dispatcher.add_handler(notify_all_handler) |
0 commit comments