-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
308 lines (247 loc) · 10.9 KB
/
bot.py
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import logging
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
import requests
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
import os
from datetime import datetime, time
import base64
# import time
from dotenv import load_dotenv
from bs4 import BeautifulSoup
import schedule
import pytz
from pytz import timezone
load_dotenv()
# Configura el token de tu bot proporcionado por BotFather
TOKEN = os.getenv("TOKEN")
api_key = os.getenv("API_KEY")
# Configura el nivel de registro (puedes ajustarlo según tus necesidades)
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
# Crea un objeto logger
logger = logging.getLogger(__name__)
# Crea un objeto Updater para interactuar con el bot
updater = Updater(token=TOKEN, use_context=True)
dispatcher = updater.dispatcher
# ------------------------------
# Funciones Externas
# ------------------------------
# Importa funciones relacionadas con el clima desde el archivo weather_functions.py
from scripts.weather_functions import get_weather_data, create_weather_image
# Importa funciones relacionadas con criptomonedas desde el archivo crypto_functions.py
from scripts.crypto_functions import get_crypto_data, create_crypto_image, get_crypto
# Importa funciones relacionadas con el Metro desde el archivo metro_functions.py
from scripts.metro_functions import scrape_metro_status
# Importa funciones relacionadas con los precios de las acciones desde el archivo stonks.py
from scripts.stonks import allPrice
# Importa funciones relacionadas con las noticias desde el archivo news.py
from scripts.news import noticias_economicas_español
# ------------------------------
# Funciones de Comandos y Manejadores de Comandos
# ------------------------------
# Función para el comando /start
def start(update, context):
chat_id = update.effective_chat.id
context.bot.send_message(chat_id=chat_id, text="¡Hola! Soy tu bot de Telegram. ¡Envíame un mensaje!")
print(f"Chat ID: {chat_id}") # Imprime el Chat ID
logging.info(f"User {update.effective_user['username']} started the conversation")
# Función para el comando /crypto
def crypto(update, context):
# Obtener el nombre o nombres de las criptomonedas
crypto = context.args[0]
# Obtener los datos de las criptomonedas
crypto_data = get_crypto(crypto)
# Enviar los datos de las criptomonedas al usuario
context.bot.send_message(chat_id=update.effective_chat.id, text=crypto_data)
# Log the message
logger.info(f"User {update.effective_user['username']} asked for crypto data")
# Función para el comando /weather
def weather(update, context):
# Obtener el nombre de la ciudad
city = context.args[0]
# Obtener los datos del tiempo
weather_data = get_weather_data(city)
# Crear una imagen con los datos del tiempo
image = create_weather_image(city)
# Enviar la imagen al usuario
context.bot.send_photo(chat_id=update.effective_chat.id, photo=open(image, 'rb'))
# Eliminar la imagen creada
os.remove(image)
# Log the message
logger.info(f"User {update.effective_user['username']} asked for weather data")
# Función para el comando /metro
def metro(update, context):
line_statuses = scrape_metro_status()
# Create a string with the status of each line
message = ''
for line, status in line_statuses.items():
message += f'{line}: {status}\n'
# Send the message to the user
context.bot.send_message(chat_id=update.effective_chat.id, text=message)
# Log the message
logger.info(f"User {update.effective_user['username']} asked for Metro status")
# Función para el comando /hora
def hora(update, context):
# Obtener la hora actual
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
# Enviar la hora actual al usuario
context.bot.send_message(chat_id=update.effective_chat.id, text=f"Son las {current_time}")
# Log the message
logger.info(f"User {update.effective_user['username']} asked for the time")
# Función para el comando /stonks
def stonksPrice(update, context):
# Enviar mensaje de espera al usuario
context.bot.send_message(chat_id=update.effective_chat.id, text="Enviando precios de las acciones... (esto puede tardar unos segundos)")
logger.info("Stonks Prices Sent!!")
logger.info(allPrice())
# Obtener los precios de las acciones
output = allPrice()
# Enviar los precios de las acciones al usuario
context.bot.send_message(chat_id=update.effective_chat.id, text=output)
# Log the message
logger.info(f"User {update.effective_user['username']} asked for stonks prices")
# Función para el comando /noticias
def noticias(update, context):
print("Noticias enviadas!!")
print(noticias_economicas_español())
# Obtener las noticias
output = noticias_economicas_español()
# Enviar las noticias al usuario
context.bot.send_message(chat_id=update.effective_chat.id, text=output)
# Log the message
logger.info(f"User {update.effective_user['username']} asked for news")
# Función para el comando /bolsa
def bolsaOpen(update, context):
# Mensajes
output = """
La bolsa de Madrid abre a las 9:00 y cierra a las 17:30 (hora de Madrid)
La bolsa de Nueva York abre a las 15:30 y cierra a las 22:00 (hora de Madrid)
La bolsa de Tokio abre a las 23:00 y cierra a las 7:00 (hora de Madrid)
"""
# Enviar los horarios de apertura de las bolsas al usuario
context.bot.send_message(chat_id=update.effective_chat.id, text=output)
# Función para el comando /miestacion
def miestacion(update, context):
weather_status = miestacion()
context.bot.send_message(chat_id=update.effective_chat.id, text=weather_status)
logger.info(f"User {update.effective_user['username']} asked for mi estacion data")
# Función para el comando /generate
def generate_image(update, context):
logger.info(f"User {update.effective_user['username']} asked for image generation")
# Send the message to the user
context.bot.send_message(chat_id=update.effective_chat.id, text="Generando imagen...")
# Obtenemos el texto completo después del comando
prompt = ' '.join(context.args)
print("User prompt: ", prompt)
response = requests.get(f"http://localhost:8000/generate?prompt={prompt}")
# Si la respuesta es correcta y el codigo de estado es 200
if response.status_code == 200:
# hacemos una peticion a la API para obtener la imagen
response = requests.get("http://localhost:8000/image")
response_data = response.json()
if response_data['image']:
# Decodificamos la imagen
image = base64.b64decode(response_data['image'])
# Creamos una imagen
image = Image.open(BytesIO(image))
# Guardamos la imagen
image.save("image.png")
# Enviamos la imagen al usuario
context.bot.send_photo(chat_id=update.effective_chat.id, photo=open("image.png", 'rb'))
# Eliminamos la imagen
os.remove("image.png")
else:
# Si no hay imagen
context.bot.send_message(chat_id=update.effective_chat.id, text="No se ha podido enviar la imagen")
else:
# Si no hay respuesta
context.bot.send_message(chat_id=update.effective_chat.id, text="No se ha podido generar la imagen")
# Función para el comando /help
def help(update, context):
#Comandos disponibles
output = """
Comandos disponibles:
- /start: Inicia el bot
- /price <nombre crypto>: Muestra el precio de una criptomoneda
- /weather <nombre ciudad> : Muestra el tiempo de una ciudad
- /metro: Muestra el estado de las líneas de Metro de Madrid
- /hora: Muestra la hora actual
- /stonks: Muestra los precios de las acciones
- /noticias: Muestra las noticias económicas
- /bolsa: Muestra los horarios de apertura de las bolsas
- /miestacion: Muestra los datos de mi estación de temperatura
- /auto: Inicia el envio de mensajes automaticos
- /stop: Para el envio de mensajes automaticos
- /generate <texto>: Genera una imagen con el texto que le pasemos
"""
# Enviar los comandos disponibles al usuario
context.bot.send_message(chat_id=update.effective_chat.id, text=output)
# Log the message
logger.info(f"User {update.effective_user['username']} asked for help")
# ------------------------------
# Manejadores de Comandos
# ------------------------------
# Agregar un manejador para el comando /start
start_handler = CommandHandler('start', start)
dispatcher.add_handler(start_handler)
logger.info("Bot started")
# Agregar un manejador para el comando /crypto
crypto_handler = CommandHandler('price', crypto)
dispatcher.add_handler(crypto_handler)
logger.info("Crypto handler added")
# Agregar un manejador para el comando /weather
weather_handler = CommandHandler('weather', weather)
dispatcher.add_handler(weather_handler)
logger.info("Weather handler added")
# Agregar un manejador para el comando /help
help_handler = CommandHandler('help', help)
dispatcher.add_handler(help_handler)
logger.info("Help handler added")
# Agregar un manejador para el comando /metro
metro_handler = CommandHandler('metro', metro)
dispatcher.add_handler(metro_handler)
logger.info("Metro handler added")
# Agregar un manejador para el comando /hora
hora_handler = CommandHandler('hora', hora)
dispatcher.add_handler(hora_handler)
logger.info("Hora handler added")
# Agregar un manejador para el comando /stonks
stonks_handler = CommandHandler('stonks', stonksPrice)
dispatcher.add_handler(stonks_handler)
logger.info("Stonks handler added")
# Agregar un manejador para el comando /noticias
noticias_handler = CommandHandler('noticias', noticias)
dispatcher.add_handler(noticias_handler)
logger.info("Noticias handler added")
# Agregar un manejador para el comando /bolsa
bolsa_handler = CommandHandler('bolsa', bolsaOpen)
dispatcher.add_handler(bolsa_handler)
logger.info("Bolsa handler added")
# Agregar un manejador para el comando /miestacion
miestacion_handler = CommandHandler('miestacion', miestacion)
dispatcher.add_handler(miestacion_handler)
logger.info("Miestacion handler added")
# Agregar un manejador para el comando /generate
generate_handler = CommandHandler('generate', generate_image)
dispatcher.add_handler(generate_handler)
logger.info("Image Generate handler added")
# ------------------------------
# Iniciar el bot
# ------------------------------
while True:
try:
# Comprueba si hay tareas programadas y ejecútalas
schedule.run_pending()
# Mantener el bot en ejecución
updater.start_polling()
logger.info("Bot Started")
# Print schedule status
print(schedule.get_jobs())
# Mantener el bot en ejecución
updater.idle()
logger.info("Bot stopped")
except Exception as e:
# Maneja cualquier excepción que pueda ocurrir durante la ejecución
print(f"Error: {e}")
logger.error(f"Error: {e}")