source: python-cinema-club-bot/main.py@ 0d67456

Last change on this file since 0d67456 was 0d67456, checked in by Mikhail Kirillov <w96k@…>, on Oct 9, 2024 at 1:45:08 AM

Add /remove and /list commands (unfinished)

  • Property mode set to 100644
File size: 3.6 KB
RevLine 
[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]12from telegram import Update, error
13from telegram.ext import ApplicationBuilder, CommandHandler, ContextTypes, \
14 MessageHandler, filters
[ca5a97e]15from dotenv import load_dotenv
16import os
[50e04dd]17import logging
[0d67456]18from rich import inspect
19
20from strings import INVALID_COMMAND, USER_NOT_PROVIDED, USER_ADDED, \
21 USER_REMOVED
22from utils import context_init
23from persistence import Persistence
24
[ca5a97e]25
26load_dotenv()
27
[50e04dd]28logging.basicConfig(
29 level=logging.INFO,
30 format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
31)
32
[71efb80]33
[5179f7b]34async def about(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
35 await update.message.reply_text('''
36Version 0.0
[71efb80]37
[5179f7b]38python-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]40Project information: http://57.129.46.169/trac/wiki/python-cinema-club-bot
41Source code: http://57.129.46.169/cgit/python-cinema-club-bot/
42
43Contributed in 2024 by Mikhail Kirillov (~w96k) <w96k@runbox.com>
44
45To 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
47You 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]53async 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]70async 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
79async 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
100async 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
107if __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()
Note: See TracBrowser for help on using the repository browser.