[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 |
|
---|
[5179f7b] | 12 | from telegram import Update, error
|
---|
| 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 | from rich import inspect
|
---|
| 19 |
|
---|
| 20 | from strings import INVALID_COMMAND, USER_NOT_PROVIDED, USER_ADDED, \
|
---|
| 21 | USER_REMOVED
|
---|
| 22 | from utils import context_init
|
---|
| 23 | from persistence import Persistence
|
---|
| 24 |
|
---|
[ca5a97e] | 25 |
|
---|
| 26 | load_dotenv()
|
---|
| 27 |
|
---|
[50e04dd] | 28 | logging.basicConfig(
|
---|
| 29 | level=logging.INFO,
|
---|
| 30 | format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
|
---|
| 31 | )
|
---|
| 32 |
|
---|
[71efb80] | 33 |
|
---|
[5179f7b] | 34 | async def about(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
---|
| 35 | await update.message.reply_text('''
|
---|
| 36 | Version 0.0
|
---|
[71efb80] | 37 |
|
---|
[5179f7b] | 38 | python-cinema-club-bot is a bot for Telegram specialized for managing cinema club activities. This software is released as Public Domain using CC0 license.
|
---|
[ca5a97e] | 39 |
|
---|
[5179f7b] | 40 | Project information: http://57.129.46.169/trac/wiki/python-cinema-club-bot
|
---|
| 41 | Source code: http://57.129.46.169/cgit/python-cinema-club-bot/
|
---|
| 42 |
|
---|
| 43 | Contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
|
---|
| 44 |
|
---|
| 45 | To the extent possible under law, the author(s) have dedicated all copyright and related and neighboring rights to this software to the public domain worldwide. This software is distributed without any warranty.
|
---|
| 46 |
|
---|
| 47 | You should have received a copy of the CC0 Public Domain Dedication along with this software. If not, see:
|
---|
| 48 | <http://creativecommons.org/publicdomain/zero/1.0/>
|
---|
| 49 |
|
---|
| 50 | ''')
|
---|
| 51 |
|
---|
[71efb80] | 52 |
|
---|
[0d67456] | 53 | async def add(
|
---|
[5179f7b] | 54 | update: Update,
|
---|
| 55 | context: ContextTypes.DEFAULT_TYPE
|
---|
| 56 | ) -> None:
|
---|
[0d67456] | 57 | context_init(context)
|
---|
| 58 |
|
---|
| 59 | if context.args == []:
|
---|
| 60 | await update.message.reply_text(USER_NOT_PROVIDED)
|
---|
| 61 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
[5179f7b] | 62 |
|
---|
[0d67456] | 63 | for user in context.args:
|
---|
| 64 | context.chat_data["users"].append(user)
|
---|
| 65 | await update.message.reply_text(USER_ADDED.format(user=user))
|
---|
[5179f7b] | 66 |
|
---|
[0d67456] | 67 | await update.message.reply_text(context.chat_data)
|
---|
[5179f7b] | 68 |
|
---|
| 69 |
|
---|
[0d67456] | 70 | async def list(
|
---|
| 71 | update: Update,
|
---|
| 72 | context: ContextTypes.DEFAULT_TYPE
|
---|
| 73 | ) -> None:
|
---|
| 74 | context_init(context)
|
---|
| 75 |
|
---|
| 76 | await update.message.reply_text(context.chat_data)
|
---|
| 77 |
|
---|
| 78 |
|
---|
| 79 | async def remove(
|
---|
| 80 | update: Update,
|
---|
| 81 | context: ContextTypes.DEFAULT_TYPE
|
---|
| 82 | ) -> None:
|
---|
| 83 | context_init(context)
|
---|
| 84 |
|
---|
| 85 | if context.args == []:
|
---|
| 86 | await update.message.reply_text(USER_NOT_PROVIDED)
|
---|
| 87 | raise error.TelegramError(USER_NOT_PROVIDED)
|
---|
| 88 |
|
---|
| 89 | for user in context.args:
|
---|
| 90 | if user == "*":
|
---|
| 91 | context.chat_data["users"] = []
|
---|
| 92 | break
|
---|
| 93 |
|
---|
| 94 | context.chat_data["users"].remove(user)
|
---|
| 95 | await update.message.reply_text(USER_REMOVED.format(user=user))
|
---|
| 96 |
|
---|
| 97 | await update.message.reply_text(context.chat_data)
|
---|
[5179f7b] | 98 |
|
---|
| 99 |
|
---|
| 100 | async def unknown(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
---|
[0d67456] | 101 | await context.bot.send_message(
|
---|
| 102 | chat_id=update.effective_chat.id,
|
---|
| 103 | text=INVALID_COMMAND
|
---|
| 104 | )
|
---|
| 105 |
|
---|
| 106 |
|
---|
| 107 | if __name__ == "__main__":
|
---|
| 108 | app = ApplicationBuilder().token(
|
---|
| 109 | os.environ.get("TELEGRAM_TOKEN")
|
---|
| 110 | ).persistence(Persistence).build()
|
---|
[ca5a97e] | 111 |
|
---|
[0d67456] | 112 | app.add_handler(CommandHandler("about", about))
|
---|
| 113 | app.add_handler(CommandHandler("add", add))
|
---|
| 114 | app.add_handler(CommandHandler("list", list))
|
---|
| 115 | app.add_handler(CommandHandler("remove", remove))
|
---|
[ca5a97e] | 116 |
|
---|
[5179f7b] | 117 |
|
---|
[0d67456] | 118 | app.add_handler(MessageHandler(filters.COMMAND, unknown))
|
---|
[ca5a97e] | 119 |
|
---|
[0d67456] | 120 | app.run_polling()
|
---|