[ca5a97e] | 1 | # This file is part of python-cinema-club-bot
|
---|
| 2 | # contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
|
---|
| 3 |
|
---|
| 4 | # To the extent possible under law, the author(s) have dedicated all copyright
|
---|
| 5 | # and related and neighboring rights to this software to the public domain
|
---|
| 6 | # worldwide. This software is distributed without any warranty.
|
---|
| 7 |
|
---|
| 8 | # You should have received a copy of the CC0 Public Domain Dedication along
|
---|
| 9 | # with this software. If not, see:
|
---|
[71efb80] | 10 | # <http://creativecommons.org/publicdomain/zero/1.0/>
|
---|
[ca5a97e] | 11 |
|
---|
[cc34991] | 12 | from telegram import Update
|
---|
[5179f7b] | 13 | from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, \
|
---|
| 14 | MessageHandler, filters
|
---|
[ca5a97e] | 15 | from dotenv import load_dotenv
|
---|
| 16 | import os
|
---|
[50e04dd] | 17 | import logging
|
---|
[0d67456] | 18 |
|
---|
[cc34991] | 19 | from strings import INVALID_COMMAND
|
---|
[0d67456] | 20 | from persistence import Persistence
|
---|
[cc34991] | 21 | import commands
|
---|
[0d67456] | 22 |
|
---|
[ca5a97e] | 23 |
|
---|
| 24 | load_dotenv()
|
---|
| 25 |
|
---|
[50e04dd] | 26 | logging.basicConfig(
|
---|
| 27 | level=logging.INFO,
|
---|
| 28 | format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
---|
| 29 | )
|
---|
| 30 |
|
---|
[71efb80] | 31 |
|
---|
[5179f7b] | 32 | async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
---|
[0d67456] | 33 | await context.bot.send_message(
|
---|
| 34 | chat_id=update.effective_chat.id,
|
---|
| 35 | text=INVALID_COMMAND
|
---|
| 36 | )
|
---|
| 37 |
|
---|
[0d48ea2] | 38 | async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
|
---|
| 39 | logging.error("Exception:", exc_info=context.error)
|
---|
| 40 |
|
---|
| 41 | await context.bot.send_message(
|
---|
| 42 | chat_id=update.effective_chat.id,
|
---|
| 43 | text=context.error.message
|
---|
| 44 | )
|
---|
[0d67456] | 45 |
|
---|
[cc34991] | 46 | if __name__ == "__main__":
|
---|
[0d67456] | 47 | app = ApplicationBuilder().token(
|
---|
| 48 | os.environ.get("TELEGRAM_TOKEN")
|
---|
| 49 | ).persistence(Persistence).build()
|
---|
[ca5a97e] | 50 |
|
---|
[0d48ea2] | 51 | # Meta commands
|
---|
[cc34991] | 52 | app.add_handler(CommandHandler("about", commands.about))
|
---|
[0d48ea2] | 53 |
|
---|
| 54 | # Movie commands
|
---|
| 55 | app.add_handler(CommandHandler("set", commands.set_users))
|
---|
[cc34991] | 56 | app.add_handler(CommandHandler("add", commands.add_users))
|
---|
| 57 | app.add_handler(CommandHandler("list", commands.list_users))
|
---|
| 58 | app.add_handler(CommandHandler("remove", commands.remove_users))
|
---|
| 59 | app.add_handler(CommandHandler("chooser", commands.chooser_user))
|
---|
[ca5a97e] | 60 |
|
---|
[0d67456] | 61 | app.add_handler(MessageHandler(filters.COMMAND, unknown))
|
---|
[0d48ea2] | 62 | app.add_error_handler(error_handler)
|
---|
| 63 |
|
---|
[ca5a97e] | 64 |
|
---|
[0d67456] | 65 | app.run_polling()
|
---|
[0d48ea2] | 66 |
|
---|