|
|
|
|
@@ -1,9 +1,9 @@
|
|
|
|
|
"""Telegram command handlers for JIGAIDO - Thin wrappers around core services."""
|
|
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from functools import wraps
|
|
|
|
|
from typing import Optional
|
|
|
|
|
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
|
|
|
|
|
|
|
|
|
import dateparser
|
|
|
|
|
from telegram import Update
|
|
|
|
|
@@ -20,6 +20,34 @@ TRACKING_SERVICE = TrackingService(TRACKING_STORAGE, ROOM_STORAGE)
|
|
|
|
|
TELEGRAM_BOT_USERNAME = "your_bot_username"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_due_date(due_date_ts: int | None, timezone_str: str) -> str:
|
|
|
|
|
"""Format due date as human-readable with timezone.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
No due date: (none shown)
|
|
|
|
|
Date only: 4 April 2026
|
|
|
|
|
Date + time: 4 April 2026 14:30
|
|
|
|
|
With timezone: 4 April 2026 14:30 (Asia/Jakarta)
|
|
|
|
|
"""
|
|
|
|
|
if not due_date_ts:
|
|
|
|
|
return ""
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
tz = ZoneInfo(timezone_str)
|
|
|
|
|
except (KeyError, ZoneInfoNotFoundError):
|
|
|
|
|
tz = ZoneInfo("UTC")
|
|
|
|
|
|
|
|
|
|
dt = datetime.fromtimestamp(due_date_ts, tz=tz)
|
|
|
|
|
|
|
|
|
|
date_str = dt.strftime("%-d %B %Y")
|
|
|
|
|
|
|
|
|
|
if dt.hour != 0 or dt.minute != 0:
|
|
|
|
|
date_str += f" {dt.strftime('%H:%M')}"
|
|
|
|
|
date_str += f" ({timezone_str})"
|
|
|
|
|
|
|
|
|
|
return date_str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def extract_args(text: str) -> list[str]:
|
|
|
|
|
if not text:
|
|
|
|
|
return []
|
|
|
|
|
@@ -49,7 +77,7 @@ def parse_args(args: list[str]) -> tuple[Optional[str], Optional[str], Optional[
|
|
|
|
|
return text, link, due_date_ts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def format_bounty(b, show_id: bool = True) -> str:
|
|
|
|
|
def format_bounty(b, show_id: bool = True, room_id: int | None = None) -> str:
|
|
|
|
|
parts = []
|
|
|
|
|
if show_id:
|
|
|
|
|
parts.append(f"[#{b.id}]")
|
|
|
|
|
@@ -58,7 +86,11 @@ def format_bounty(b, show_id: bool = True) -> str:
|
|
|
|
|
if b.link:
|
|
|
|
|
parts.append(f"🔗 {b.link}")
|
|
|
|
|
if b.due_date_ts:
|
|
|
|
|
due_str = time.strftime("%Y-%m-%d", time.localtime(b.due_date_ts))
|
|
|
|
|
timezone_str = "UTC"
|
|
|
|
|
if room_id is not None:
|
|
|
|
|
timezone_str = BOUNTY_SERVICE.get_timezone(room_id)
|
|
|
|
|
|
|
|
|
|
due_str = format_due_date(b.due_date_ts, timezone_str)
|
|
|
|
|
days_left = (b.due_date_ts - int(time.time())) // 86400
|
|
|
|
|
if days_left < 0:
|
|
|
|
|
parts.append(f"⏰ {due_str} (OVERDUE)")
|
|
|
|
|
@@ -102,7 +134,7 @@ async def cmd_bounty(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
await update.message.reply_text("No bounties yet.")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
lines = [format_bounty(b, show_id=True) for b in bounties]
|
|
|
|
|
lines = [format_bounty(b, show_id=True, room_id=room_id) for b in bounties]
|
|
|
|
|
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -112,6 +144,7 @@ async def cmd_my(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
if is_group(update):
|
|
|
|
|
group_id = get_group_id(update)
|
|
|
|
|
bounties = TRACKING_SERVICE.get_tracked_bounties(group_id, user_id)
|
|
|
|
|
room_id = group_id
|
|
|
|
|
else:
|
|
|
|
|
room_id = get_room_id(update)
|
|
|
|
|
bounties = BOUNTY_SERVICE.list_bounties(room_id)
|
|
|
|
|
@@ -125,7 +158,7 @@ async def cmd_my(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
await update.message.reply_text(msg)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
lines = [format_bounty(b, show_id=True) for b in bounties]
|
|
|
|
|
lines = [format_bounty(b, show_id=True, room_id=room_id) for b in bounties]
|
|
|
|
|
await update.message.reply_text("\n".join(lines), disable_web_page_preview=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@@ -156,7 +189,8 @@ async def cmd_add(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
|
|
|
|
|
due_str = ""
|
|
|
|
|
if due_date_ts:
|
|
|
|
|
due_str = f" | Due: {time.strftime('%Y-%m-%d', time.localtime(due_date_ts))}"
|
|
|
|
|
timezone_str = BOUNTY_SERVICE.get_timezone(room_id)
|
|
|
|
|
due_str = f" | Due: {format_due_date(due_date_ts, timezone_str)}"
|
|
|
|
|
|
|
|
|
|
await update.message.reply_text(
|
|
|
|
|
f"✅ Bounty added (#{bounty.id}){due_str}",
|
|
|
|
|
@@ -324,37 +358,7 @@ async def cmd_help(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
"/delete <id> — delete bounty\n"
|
|
|
|
|
"/track <id> — track a bounty (groups only)\n"
|
|
|
|
|
"/untrack <id> — stop tracking (groups only)\n"
|
|
|
|
|
"/timezone [tz] — get/set room timezone (admin only)\n"
|
|
|
|
|
"/start — re-initialize\n"
|
|
|
|
|
"/help — this message",
|
|
|
|
|
disable_web_page_preview=True,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async def cmd_timezone(update: Update, ctx: ContextTypes.DEFAULT_TYPE) -> None:
|
|
|
|
|
args = extract_args(update.message.text)
|
|
|
|
|
room_id = get_room_id(update)
|
|
|
|
|
user_id = get_user_id(update)
|
|
|
|
|
|
|
|
|
|
if not args:
|
|
|
|
|
current_tz = BOUNTY_SERVICE.get_timezone(room_id)
|
|
|
|
|
await update.message.reply_text(f"Current timezone: {current_tz}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
timezone_str = args[0]
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
ZoneInfo(timezone_str)
|
|
|
|
|
except (KeyError, ZoneInfoNotFoundError):
|
|
|
|
|
await update.message.reply_text(
|
|
|
|
|
"⛔ Invalid timezone. Use IANA format (e.g., Asia/Jakarta)"
|
|
|
|
|
)
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
BOUNTY_SERVICE.set_timezone(room_id, timezone_str, user_id)
|
|
|
|
|
except PermissionError as e:
|
|
|
|
|
await update.message.reply_text(f"⛔ {e}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
await update.message.reply_text(f"✅ Timezone set to {timezone_str}.")
|
|
|
|
|
|