Skip to content

Commit 46ed503

Browse files
committed
2 parents a04884a + 582df06 commit 46ed503

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ DATABASE_URL=firebase_url
4747
GOOGLE_API_EMAIL=google_api_email
4848
FIREBASE_JSON=service_account_key
4949
GOOGLE_JSON=service_account_key
50+
MASTER_TELE_ID=your_telegram_id
5051
```
5152

5253
### Step 3
@@ -78,6 +79,8 @@ python3.9 test.py
7879

7980
/help - Show Help
8081

82+
/notifyall - Send message to all users (requires your telegram id/only for admin)
83+
8184
## Contributing
8285
If you want to contribute or have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue or work on issues that are currently open.
8386
Don't forget to give the project a ⭐! Thanks again!

bot/telegram_bot.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import bot.utils as utils
2121

2222
timezone = pytz.timezone("Asia/Singapore")
23+
MASTER_TELE_ID = os.environ.get("MASTER_TELE_ID")
2324

2425

2526
def get_category_text(sheet_id, entry_type):
@@ -895,6 +896,65 @@ def add_backlog_entry(update, context) -> int:
895896
return CS.ENTRY
896897

897898

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+
898958
def setup_handlers(dispatcher):
899959
# Configuration-related states and handlers
900960
config_states = {
@@ -965,8 +1025,13 @@ def setup_handlers(dispatcher):
9651025
},
9661026
fallbacks=[CommandHandler("cancel", cancel)],
9671027
)
968-
9691028
dispatcher.add_handler(conv_handler)
9701029

9711030
help_handler = CommandHandler("help", help)
9721031
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)

release_notes.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Release Notes
2+
## Version 2.1.9 - Date 23 Dec 2023
3+
### For Developer 🧑‍💻
4+
- Push features message to all users using `/notifyall` command (requires your telegram id)
5+
26
## Version 2.1.8 - Date 17 Dec 2023
37
### Bug Fix 🛠️
48
- Fix add entry bug

0 commit comments

Comments
 (0)