mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-15 03:26:40 +02:00
Complete refactor... Some issue...
This commit is contained in:
86
bot.py
86
bot.py
@@ -1,38 +1,42 @@
|
||||
from telegram import Update
|
||||
from telegram.ext import Application, CommandHandler, MessageHandler, ContextTypes
|
||||
from config import get_bot_token, get_commands
|
||||
from access_control import can_open_gate, grant_access, get_grantor
|
||||
from gates import get_name, open_gate
|
||||
from users import get_role, set_credentials, get_credentials, get_grantor_credentials, get_admins, update_user, get_fullname, get_username
|
||||
from config import BotConfig
|
||||
from gates import Gates
|
||||
from users import Users, Credential, Role
|
||||
|
||||
BOT_NAME = "lagomareGateKeeperBot"
|
||||
BOT_USERNAME = "lagomareGateKeeperBot"
|
||||
bot_config = BotConfig(BOT_USERNAME)
|
||||
gates = Gates()
|
||||
users = Users()
|
||||
|
||||
async def post_init(application: Application) -> None:
|
||||
bot_commands = get_commands(BOT_NAME)
|
||||
bot_commands = bot_config.commands
|
||||
await application.bot.set_my_commands(bot_commands)
|
||||
await application.bot.set_my_name("Lagomare GateKeeper Bot")
|
||||
await application.bot.set_my_description("This bot is used to open Lagomare gates by members. You can also request timed access if you are a guest.")
|
||||
await application.bot.set_my_short_description("Open Lagomare gates or request guest access")
|
||||
await application.bot.set_my_name(bot_config.name)
|
||||
await application.bot.set_my_description(bot_config.description)
|
||||
await application.bot.set_my_short_description(bot_config.short_description)
|
||||
|
||||
async def updateuser(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
user_id = str(update.effective_user.id)
|
||||
username = update.effective_user.username
|
||||
fullname = update.effective_user.full_name
|
||||
update_user(user_id, username, fullname)
|
||||
users.update_user(user_id, username, fullname)
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
await update.message.reply_text("Welcome to GatekeeperBot! Use `/setcredentials` to configure your access.")
|
||||
await update.message.reply_text("Welcome to GatekeeperBot! Use `/setcredentials` to configure your access")
|
||||
|
||||
async def setcredentials(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
user_id = str(update.effective_user.id)
|
||||
args = context.args
|
||||
if len(args) != 2:
|
||||
return await update.message.reply_text("Usage: `/setcredentials <username> <password>`")
|
||||
role = get_role(user_id)
|
||||
role = users.get_role(user_id)
|
||||
if role not in ("admin", "member"):
|
||||
return await update.message.reply_text("Only members or admins can set credentials.")
|
||||
set_credentials(user_id, args[0], args[1])
|
||||
await update.message.reply_text("Credentials saved.")
|
||||
return await update.message.reply_text("Only members or admins can set credentials")
|
||||
if users.set_credentials(user_id, Credential(args[0], args[1])):
|
||||
await update.message.reply_text("Credentials saved")
|
||||
else:
|
||||
await update.message.reply_text("Error saving credentials")
|
||||
|
||||
async def opengate(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
user_id = str(update.effective_user.id)
|
||||
@@ -40,55 +44,57 @@ async def opengate(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
if not args:
|
||||
return await update.message.reply_text("Usage: `/opengate <gate>`")
|
||||
gate = args[0]
|
||||
role = get_role(user_id)
|
||||
if role in ("admin", "member"):
|
||||
creds = get_credentials(user_id)
|
||||
gate_name = gates.get_name(gate)
|
||||
role = users.get_role(user_id)
|
||||
if role in (Role.ADMIN, Role.MEMBER):
|
||||
creds = users.get_credentials(user_id)
|
||||
if not creds:
|
||||
return await update.message.reply_text("Please set your credentials with `/setcredentials` first.")
|
||||
elif role == "guest" and can_open_gate(user_id, gate):
|
||||
creds = get_grantor_credentials(user_id, gate)
|
||||
return await update.message.reply_text("Please set your credentials with `/setcredentials` first")
|
||||
elif role == Role.GUEST and users.can_open_gate(user_id, gate):
|
||||
grantor = users.get_grantor(user_id, gate)
|
||||
creds = users.get_credentials(grantor)
|
||||
if not grantor:
|
||||
return await update.message.reply_text("No valid grantor available.")
|
||||
if not creds:
|
||||
return await update.message.reply_text("No valid grantor credentials available.")
|
||||
grantor = get_grantor(user_id, gate)
|
||||
if grantor:
|
||||
try:
|
||||
await context.bot.send_message(chat_id=grantor, text=f"Guest {user_id} opened {get_name(gate)}")
|
||||
except Exception as e:
|
||||
print(f"Failed to notify {grantor} that guest {user_id} opened {get_name(gate)}: {e}")
|
||||
#TODO: update guest last_used_at
|
||||
try:
|
||||
await context.bot.send_message(chat_id=grantor, text=f"Guest {user_id} opened {gate_name}")
|
||||
except Exception as e:
|
||||
print(f"Failed to notify {grantor} that guest {user_id} opened {gate_name}: {e}")
|
||||
else:
|
||||
return await update.message.reply_text("Access denied.")
|
||||
|
||||
if open_gate(gate, creds):
|
||||
return await update.message.reply_text(f"Gate {get_name(gate)} opened!")
|
||||
await update.message.reply_text(f"ERROR: Cannot open gate {get_name(gate)}")
|
||||
if gates.open_gate(gate, creds):
|
||||
return await update.message.reply_text(f"Gate {gate_name} opened!")
|
||||
await update.message.reply_text(f"ERROR: Cannot open gate {gate_name}")
|
||||
|
||||
async def requestaccess(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
user_id = str(update.effective_user.id)
|
||||
role = get_role(user_id)
|
||||
role = users.get_role(user_id)
|
||||
if role not in ("guest"):
|
||||
return await update.message.reply_text("Only guests can request access.")
|
||||
if not context.args:
|
||||
return await update.message.reply_text("Usage: `/requestaccess <gate>`")
|
||||
gate = context.args[0]
|
||||
requester = get_fullname(user_id) or get_username(user_id)
|
||||
text = (f"Access request: {requester} ({user_id}) requests access to *{gate}*.\nUse `/approve {user_id} {gate} 60` to grant access.")
|
||||
return await update.message.reply_text("Usage: `/requestaccess`", parse_mode="Markdown")
|
||||
requester = users.get_fullname(user_id) or users.get_username(user_id)
|
||||
text = (f"Access request: {requester} ({user_id}) requests access.\nUse `/approve {user_id} <gate|all> YYYY-MM-DDTHH:MM:SSZ` to grant access.")
|
||||
await update.message.reply_text("Your request has been submitted.")
|
||||
admins = get_admins()
|
||||
admins = users.get_admins()
|
||||
for admin_id in admins:
|
||||
try:
|
||||
await context.bot.send_message(chat_id=admin_id, text=text, parse_mode="Markdown")
|
||||
except Exception as e:
|
||||
print(f"Failed to notify {admin_id} that guest {user_id} requested access for {gate}: {e}")
|
||||
print(f"Failed to notify {admin_id} that guest {user_id} requested access: {e}")
|
||||
|
||||
async def approve(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
approver_id = str(update.effective_user.id)
|
||||
if get_role(approver_id) != "admin":
|
||||
if users.get_role(approver_id) != "admin":
|
||||
return await update.message.reply_text("Only admins can approve access.")
|
||||
try:
|
||||
user_id = context.args[0]
|
||||
gate = context.args[1]
|
||||
expires_at = context.args[2]
|
||||
grant_access(user_id, gate, expires_at, grantor_id=approver_id)
|
||||
users.grant_access(user_id, gate, expires_at, grantor_id=approver_id)
|
||||
await update.message.reply_text(f"Access to {gate} granted to user {user_id}.")
|
||||
try:
|
||||
await context.bot.send_message(chat_id=user_id, text=f"Access granted to gate {gate} up to {expires_at}")
|
||||
@@ -98,7 +104,7 @@ async def approve(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||
await update.message.reply_text("Usage: `/approve <user_id> <gate|all> <expires_at:YYYY-MM-DDTHH:MM:SSZ>`")
|
||||
|
||||
def main():
|
||||
app = Application.builder().token(get_bot_token(BOT_NAME)).post_init(post_init).build()
|
||||
app = Application.builder().token(bot_config.token).post_init(post_init).build()
|
||||
app.add_handler(MessageHandler(None, updateuser), group=1)
|
||||
app.add_handler(CommandHandler("start", start))
|
||||
app.add_handler(CommandHandler("setcredentials", setcredentials))
|
||||
|
Reference in New Issue
Block a user