-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
39 lines (29 loc) · 1.41 KB
/
app.py
File metadata and controls
39 lines (29 loc) · 1.41 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
import logging
from telegram import Update
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, MessageHandler, filters
from dotenv import load_dotenv
load_dotenv()
TOKEN = os.getenv('TELEGRAM_TOKEN')
# Configuración de logs para ver qué pasa en consola
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO
)
# Acción para el comando /start
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("¡Hola! Soy tu bot de Python. Envíame algo y haré una acción.")
# Acción que se ejecuta al recibir UN MENSAJE de texto
async def manejar_mensaje(update: Update, context: ContextTypes.DEFAULT_TYPE):
texto_recibido = update.message.text
print(f"Mensaje recibido: {texto_recibido}")
# Aquí es donde ocurre la "Acción"
respuesta = f"He recibido tu mensaje: '{texto_recibido}'. Procesando acción..."
await update.message.reply_text(respuesta)
if __name__ == '__main__':
# Reemplaza 'TU_TOKEN_AQUI' con el token de BotFather
application = ApplicationBuilder().token(TOKEN).build()
# Manejadores (Handlers)
application.add_handler(CommandHandler('start', start))
application.add_handler(MessageHandler(filters.TEXT & (~filters.COMMAND), manejar_mensaje))
print("Bot en marcha... Presiona Ctrl+C para detenerlo.")
application.run_polling()