-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
314 lines (255 loc) · 11.1 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
309
310
311
312
313
314
from telegram import Update, ReplyKeyboardMarkup, ReplyKeyboardRemove, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes, ConversationHandler, CallbackQueryHandler
from datetime import datetime, timedelta
import sqlite3
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# States for the conversation handler
TITLE, DESCRIPTION, LOCATION, PRICE, SHIPPING, PHOTOS, CONFIRM = range(7)
EDIT_CHOOSE, EDIT_FIELD, EDIT_VALUE = range(7, 10)
# Dictionary to store temporary offer data
temp_offers = {}
def setup_database():
"""Create the SQLite database and tables"""
conn = sqlite3.connect('marketplace.db')
c = conn.cursor()
# Create offers table
c.execute('''
CREATE TABLE IF NOT EXISTS offers (
id INTEGER PRIMARY KEY AUTOINCREMENT,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
location TEXT NOT NULL,
price REAL NOT NULL,
shipping BOOLEAN NOT NULL,
created_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL
)
''')
# Create photos table
c.execute('''
CREATE TABLE IF NOT EXISTS photos (
id INTEGER PRIMARY KEY AUTOINCREMENT,
offer_id INTEGER NOT NULL,
file_id TEXT NOT NULL,
FOREIGN KEY (offer_id) REFERENCES offers (id)
)
''')
conn.commit()
conn.close()
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Start command handler"""
await update.message.reply_text(
"¡Bienvenido al Bot de Ventas! 👋\n\n"
"Comandos disponibles:\n"
"/createoffer - Crear una nueva oferta\n"
"/getalloffers - Ver todas las ofertas activas\n"
"/editoffer - Editar una oferta existente\n"
"/renewoffer - Renovar una oferta por una semana más\n"
"/myoffers - Ver tus ofertas activas"
)
async def create_offer(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Start the offer creation process"""
await update.message.reply_text(
"¡Entendido! Vamos a crear una nueva oferta de venta.\n"
"Puedes cancelar en cualquier momento con /cancel\n\n"
"¿Cuál es el título para el producto que quieres vender? (máx. 64 caracteres)"
)
user_id = update.effective_user.id
temp_offers[user_id] = {
'photos': []
}
return TITLE
async def title(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle the product title"""
user_id = update.effective_user.id
text = update.message.text
if len(text) > 64:
await update.message.reply_text("El título es demasiado largo. Por favor, intenta con uno más corto (máx. 64 caracteres)")
return TITLE
temp_offers[user_id]['title'] = text
await update.message.reply_text(
"¿Cuál es la descripción del producto que quieres vender? (máx. 512 caracteres)"
)
return DESCRIPTION
async def description(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle the product description"""
user_id = update.effective_user.id
text = update.message.text
if len(text) > 512:
await update.message.reply_text("La descripción es demasiado larga. Por favor, intenta con una más corta (máx. 512 caracteres)")
return DESCRIPTION
temp_offers[user_id]['description'] = text
await update.message.reply_text(
"¿Dónde está el producto, para un posible trato en mano?"
)
return LOCATION
async def location(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle the product location"""
user_id = update.effective_user.id
temp_offers[user_id]['location'] = update.message.text
await update.message.reply_text(
"¿Cuál es el precio del producto que quieres vender? (máx. 2 decimales)"
)
return PRICE
async def price(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle the product price"""
user_id = update.effective_user.id
text = update.message.text
try:
price = float(text)
if price < 0:
raise ValueError
temp_offers[user_id]['price'] = "{:.2f}".format(price)
keyboard = [['SÍ', 'NO']]
reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)
await update.message.reply_text(
"¿Está el envío incluído?",
reply_markup=reply_markup
)
return SHIPPING
except ValueError:
await update.message.reply_text("Por favor, introduce un precio válido (ejemplo: 99.99)")
return PRICE
async def shipping(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle shipping information"""
user_id = update.effective_user.id
text = update.message.text.upper()
if text not in ['SÍ', 'NO']:
keyboard = [['SÍ', 'NO']]
reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)
await update.message.reply_text(
"Por favor, selecciona SÍ o NO",
reply_markup=reply_markup
)
return SHIPPING
temp_offers[user_id]['shipping'] = text == 'SÍ'
await update.message.reply_text(
"Ya casi estamos. Envía algunas fotos del producto para finalizar.\n"
"Cuando hayas terminado, escribe 'LISTO'",
reply_markup=ReplyKeyboardRemove()
)
return PHOTOS
async def handle_photo(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle incoming photos"""
user_id = update.effective_user.id
photo_file = await update.message.photo[-1].get_file()
if user_id not in temp_offers:
temp_offers[user_id] = {'photos': []}
# Store photo file_id
temp_offers[user_id]['photos'].append(photo_file.file_id)
await update.message.reply_text(f"Foto recibida ({len(temp_offers[user_id]['photos'])}). "
"Envía más fotos o escribe 'LISTO' para continuar.")
return PHOTOS
async def photos_done(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle completion of photo uploads"""
user_id = update.effective_user.id
if user_id not in temp_offers:
await update.message.reply_text("Ha ocurrido un error. Por favor, empieza de nuevo con /createoffer")
return ConversationHandler.END
if not temp_offers[user_id].get('photos'):
await update.message.reply_text("Por favor, envía al menos una foto del producto.")
return PHOTOS
offer = temp_offers[user_id]
preview = (
f"📦 {offer.get('title', 'Sin título')}\n\n"
f"📝 {offer.get('description', 'Sin descripción')}\n\n"
f"📍 Ubicación: {offer.get('location', 'No especificada')}\n"
f"💰 Precio: {offer.get('price', '0')}€\n"
f"🚚 Envío: {'Incluido' if offer.get('shipping', False) else 'No incluido'}\n\n"
"¿Quieres publicar esta oferta? (SÍ/NO)"
)
keyboard = [['SÍ', 'NO']]
reply_markup = ReplyKeyboardMarkup(keyboard, one_time_keyboard=True)
await update.message.reply_text(preview, reply_markup=reply_markup)
# Send preview photos
for photo_id in offer['photos']:
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=photo_id)
return CONFIRM
async def confirm_offer(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Handle final confirmation and publication of the offer"""
user_id = update.effective_user.id
text = update.message.text.upper()
if text == 'SÍ':
if user_id in temp_offers:
# Save to database
conn = sqlite3.connect('marketplace.db')
c = conn.cursor()
now = datetime.now()
expires_at = now + timedelta(days=7)
offer = temp_offers[user_id]
# Insert offer
c.execute('''
INSERT INTO offers (user_id, title, description, location, price, shipping, created_at, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
''', (user_id, offer['title'], offer['description'], offer['location'],
float(offer['price']), offer['shipping'], now, expires_at))
offer_id = c.lastrowid
# Insert photos
for photo_id in offer['photos']:
c.execute('INSERT INTO photos (offer_id, file_id) VALUES (?, ?)',
(offer_id, photo_id))
conn.commit()
conn.close()
await update.message.reply_text(
"¡Oferta publicada con éxito! 🎉\n"
"Puedes ver todas tus ofertas con /myoffers",
reply_markup=ReplyKeyboardRemove()
)
del temp_offers[user_id]
else:
await update.message.reply_text(
"Ha ocurrido un error. Por favor, intenta crear la oferta de nuevo.",
reply_markup=ReplyKeyboardRemove()
)
else:
await update.message.reply_text(
"Oferta cancelada. Puedes crear una nueva oferta cuando quieras con /createoffer",
reply_markup=ReplyKeyboardRemove()
)
if user_id in temp_offers:
del temp_offers[user_id]
return ConversationHandler.END
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Cancel the current operation"""
user_id = update.effective_user.id
if user_id in temp_offers:
del temp_offers[user_id]
await update.message.reply_text(
"Operación cancelada. Puedes empezar de nuevo cuando quieras.",
reply_markup=ReplyKeyboardRemove()
)
return ConversationHandler.END
def main():
"""Main function to run the bot"""
# Setup database
setup_database()
# Initialize bot
application = Application.builder().token(os.getenv('BOT_TOKEN')).build()
# Add conversation handler
conv_handler = ConversationHandler(
entry_points=[CommandHandler('createoffer', create_offer)],
states={
TITLE: [MessageHandler(filters.TEXT & ~filters.COMMAND, title)],
DESCRIPTION: [MessageHandler(filters.TEXT & ~filters.COMMAND, description)],
LOCATION: [MessageHandler(filters.TEXT & ~filters.COMMAND, location)],
PRICE: [MessageHandler(filters.TEXT & ~filters.COMMAND, price)],
SHIPPING: [MessageHandler(filters.TEXT & ~filters.COMMAND, shipping)],
PHOTOS: [
MessageHandler(filters.PHOTO, handle_photo),
MessageHandler(filters.Regex('^LISTO$'), photos_done)
],
CONFIRM: [MessageHandler(filters.TEXT & ~filters.COMMAND, confirm_offer)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
# Add handlers
application.add_handler(conv_handler)
application.add_handler(CommandHandler('start', start))
# Start the bot
application.run_polling()
if __name__ == '__main__':
main()