Refactor to apps/ structure
JIGAIDO is now a platform with apps/ as the container. All telegram-bot files moved to apps/telegram-bot/: - bot.py, commands.py, cron.py, db.py, schema.sql - requirements.txt, .env.example, README.md - Root now holds SPEC.md, README.md, CONTRIBUTING.md only. Structure: jigaido/ ├── apps/ │ └── telegram-bot/ └── SPEC.md, README.md, CONTRIBUTING.md
This commit is contained in:
78
apps/telegram-bot/bot.py
Normal file
78
apps/telegram-bot/bot.py
Normal file
@@ -0,0 +1,78 @@
|
||||
"""JIGAIDO Telegram bot entrypoint."""
|
||||
|
||||
import logging
|
||||
import os
|
||||
import sys
|
||||
|
||||
from telegram import Update
|
||||
from telegram.ext import (
|
||||
Application,
|
||||
CommandHandler,
|
||||
MessageHandler,
|
||||
filters,
|
||||
)
|
||||
|
||||
import db
|
||||
import commands
|
||||
|
||||
logging.basicConfig(
|
||||
format="%(asctime)s %(levelname)s %(name)s: %(message)s",
|
||||
level=logging.INFO,
|
||||
)
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
# Token from environment or config
|
||||
BOT_TOKEN = os.environ.get("JIGAIDO_BOT_TOKEN", "")
|
||||
|
||||
|
||||
def build_app() -> Application:
|
||||
app = Application.builder().token(BOT_TOKEN).build()
|
||||
|
||||
# Core commands
|
||||
app.add_handler(CommandHandler("start", commands.cmd_start))
|
||||
app.add_handler(CommandHandler("help", commands.cmd_help))
|
||||
app.add_handler(CommandHandler("bounty", commands.cmd_bounty))
|
||||
app.add_handler(CommandHandler("my", commands.cmd_my))
|
||||
app.add_handler(CommandHandler("add", commands.cmd_add))
|
||||
app.add_handler(CommandHandler("update", commands.cmd_update))
|
||||
app.add_handler(CommandHandler("delete", commands.cmd_delete))
|
||||
app.add_handler(CommandHandler("track", commands.cmd_track))
|
||||
app.add_handler(CommandHandler("untrack", commands.cmd_untrack))
|
||||
app.add_handler(CommandHandler("admin_add", commands.cmd_admin_add))
|
||||
app.add_handler(CommandHandler("admin_remove", commands.cmd_admin_remove))
|
||||
|
||||
# Fallback: unknown commands
|
||||
app.add_handler(MessageHandler(filters.COMMAND, commands.cmd_help))
|
||||
|
||||
return app
|
||||
|
||||
|
||||
async def post_init(app: Application) -> None:
|
||||
# Set bot commands in menu
|
||||
await app.bot.set_my_commands([
|
||||
("bounty", "List bounties"),
|
||||
("my", "Your tracked bounties"),
|
||||
("add", "Add a bounty"),
|
||||
("track", "Track a bounty"),
|
||||
("untrack", "Stop tracking"),
|
||||
("help", "Show help"),
|
||||
])
|
||||
|
||||
|
||||
def main() -> None:
|
||||
if not BOT_TOKEN:
|
||||
log.error("JIGAIDO_BOT_TOKEN environment variable not set.")
|
||||
sys.exit(1)
|
||||
|
||||
db.init_db()
|
||||
log.info("Database initialized.")
|
||||
|
||||
app = build_app()
|
||||
app.post_init = post_init
|
||||
|
||||
log.info("JIGAIDO starting...")
|
||||
app.run_polling(drop_pending_updates=True)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user