-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
103 lines (82 loc) · 3.22 KB
/
Copy pathmain.py
File metadata and controls
103 lines (82 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import logging
from datetime import datetime as dt
import pytz
from telegram import (
Update,
InlineKeyboardButton,
InlineKeyboardMarkup,
)
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
ContextTypes,
CallbackQueryHandler,
filters,
)
from app.config import BOT_TOKEN, WEBHOOK_URL, PORT
# As mensagens do bot foram adicionadas em um dicionário para o código ficar mais limpo.
from app.messages import WELCOME, ABOUT, RULES, ADMINS
from app.config import ADMIN_GROUP_ID
from app.modules.guardian import adm_button, join, build_join_conversation
from app.modules.doomlist import doom
# logger ajuda a debugar o bot, caso queira desativar, basta comentar as linhas abaixo
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [
[InlineKeyboardButton("🚗 Entrar no Grupo", callback_data="join_group")],
[InlineKeyboardButton("📖 Sobre o Grupo", callback_data="about_group")],
[InlineKeyboardButton("📜 Regras do Grupo", callback_data="rules_group")],
[InlineKeyboardButton("👮 Administradores", callback_data="admins_group")],
]
await update.message.reply_text(
WELCOME,
reply_markup=InlineKeyboardMarkup(keyboard),
)
async def ping(update: Update, context: ContextTypes.DEFAULT_TYPE):
tz = pytz.timezone("America/Sao_Paulo")
now = dt.now(tz)
await update.message.reply_text(
f"Bot está online, Data: {now.strftime('%d/%m/%Y %H:%M')} (Horário de Brasília). Versão atual do bot é 1.1"
)
async def button_handler(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
match query.data:
case "join_group":
pass
case "about_group":
await query.message.reply_text(ABOUT)
case "rules_group":
await query.message.reply_text(RULES)
case "admins_group":
await query.message.reply_text(ADMINS)
def main():
# debuga possíveis ausências de variáveis de ambiente
if not BOT_TOKEN:
raise RuntimeError("BOT_TOKEN não está definido.")
app = ApplicationBuilder().token(BOT_TOKEN).build()
app.add_handler(CommandHandler("start", start, filters=filters.ChatType.PRIVATE))
app.add_handler(CommandHandler("ping", ping, filters=filters.ChatType.PRIVATE))
app.add_handler(build_join_conversation())
app.add_handler(CommandHandler("doom", doom, filters=filters.Chat(ADMIN_GROUP_ID)))
app.add_handler(CallbackQueryHandler(button_handler, pattern=r"^(about|rules|admins)_group$"))
app.add_handler(CallbackQueryHandler(adm_button, pattern=r"^\d+\?(Yay|Nay)$"))
if not WEBHOOK_URL:
logger.warning("Polling mode.")
app.run_polling()
return
webhook_path = f"/webhook/{BOT_TOKEN}"
webhook_url = f"{WEBHOOK_URL}{webhook_path}"
logger.info(f"Iniciando Webhook em {webhook_url}")
app.run_webhook(
listen="0.0.0.0",
port=PORT,
url_path=webhook_path,
webhook_url=webhook_url,
)
if __name__ == "__main__":
main()