Skip to content

Commit 8d09122

Browse files
committed
Add group handling
1 parent ba02b7e commit 8d09122

File tree

3 files changed

+155
-58
lines changed

3 files changed

+155
-58
lines changed

src/pycamp_bot/commands/base.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22
from pycamp_bot.commands.help_msg import get_help
33
from pycamp_bot.logger import logger
44

5+
import os
6+
57

68
async def msg_to_active_pycamp_chat(bot, text):
7-
chat_id = -1001404878013 # Prueba
8-
await bot.send_message(
9-
chat_id=chat_id,
10-
text=text
11-
)
9+
if 'TEST_CHAT_ID' in os.environ:
10+
chat_id = -1001404878013 # Prueba
11+
await bot.send_message(
12+
chat_id=os.environ['TEST_CHAT_ID'],
13+
text=text
14+
)
1215

1316

1417
async def start(update, context):

src/pycamp_bot/commands/projects.py

Lines changed: 145 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,29 @@
11
import logging
2+
import textwrap
3+
24
import peewee
3-
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
4-
from telegram.ext import CallbackQueryHandler, ConversationHandler, CommandHandler, MessageHandler, filters
5+
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, LinkPreviewOptions
6+
from telegram.ext import CallbackQueryHandler, CommandHandler, ConversationHandler, MessageHandler, filters
57
from pycamp_bot.models import Pycampista, Project, Slot, Vote
68
from pycamp_bot.commands.base import msg_to_active_pycamp_chat
79
from pycamp_bot.commands.manage_pycamp import active_needed, get_active_pycamp
810
from pycamp_bot.commands.auth import admin_needed, get_admins_username
911
from pycamp_bot.commands.schedule import DIAS
1012
from pycamp_bot.utils import escape_markdown
1113

12-
1314
current_projects = {}
1415

1516
NOMBRE = "nombre"
1617
DIFICULTAD = "dificultad"
1718
TOPIC = "topic"
1819
CHECK_REPOSITORIO = "check_repositorio"
1920
REPOSITORIO = "repositorio"
21+
CHECK_GRUPO = "check_grupo"
22+
GRUPO = "grupo"
2023

2124
REPO_EXISTS_PATTERN = 'repoexists'
2225
PROJECT_NAME_PATTERN = 'projectname'
26+
GROUP_EXISTS_PATTERN = 'groupexists'
2327

2428
logger = logging.getLogger(__name__)
2529

@@ -47,15 +51,11 @@ async def load_project(update, context):
4751
logger.info("Adding project")
4852
username = update.message.from_user.username
4953

50-
logger.info("Load autorized. Starting dialog")
51-
await context.bot.send_message(
52-
chat_id=update.message.chat_id,
53-
text="Usuario: " + username
54-
)
54+
logger.info("Load authorized. Starting dialog")
5555

5656
await context.bot.send_message(
5757
chat_id=update.message.chat_id,
58-
text="Ingresá el Nombre del Proyecto a proponer",
58+
text="¿Cuál es el nombre del proyecto?",
5959
)
6060
return NOMBRE
6161

@@ -75,18 +75,13 @@ async def naming_project(update, context):
7575

7676
await context.bot.send_message(
7777
chat_id=update.message.chat_id,
78-
text="Estamos cargando tu proyecto: {}!".format(username)
79-
)
80-
await context.bot.send_message(
81-
chat_id=update.message.chat_id,
82-
text="Tu proyecto se llama: {}".format(name)
83-
)
84-
await context.bot.send_message(
85-
chat_id=update.message.chat_id,
86-
text="""Cual es el nivel de dificultad?
87-
1 = newbie friendly
88-
2 = intermedio
89-
3 = python avanzado"""
78+
text=textwrap.dedent("""
79+
¿Cuál es el nivel de dificultad del proyecto?
80+
81+
1: newbie friendly
82+
2: intermedio
83+
3: python avanzado"""
84+
)
9085
)
9186
return DIFICULTAD
9287

@@ -102,17 +97,16 @@ async def project_level(update, context):
10297

10398
await context.bot.send_message(
10499
chat_id=update.message.chat_id,
105-
text="Ok! Tu proyecto es nivel: {}".format(text)
106-
)
107-
await context.bot.send_message(
108-
chat_id=update.message.chat_id,
109-
text="""Ahora necesitamos que nos digas la temática de tu proyecto.
110-
Algunos ejemplos pueden ser:
111-
- flask
112-
- django
113-
- telegram
114-
- inteligencia artificial
115-
- recreativo"""
100+
text=textwrap.dedent("""
101+
¿Cuál es la temática del proyecto?
102+
103+
Ejemplos:
104+
- flask
105+
- django
106+
- telegram
107+
- inteligencia artificial
108+
- recreativo"""
109+
)
116110
)
117111
return TOPIC
118112
else:
@@ -131,11 +125,6 @@ async def project_topic(update, context):
131125
new_project = current_projects[username]
132126
new_project.topic = text
133127

134-
await context.bot.send_message(
135-
chat_id=update.message.chat_id,
136-
text="Excelente {}! La temática de tu proyecto es: {}.".format(username, text)
137-
)
138-
139128
keyboard = [
140129
[
141130
InlineKeyboardButton("Sí", callback_data=f"{REPO_EXISTS_PATTERN}:si"),
@@ -146,7 +135,7 @@ async def project_topic(update, context):
146135

147136
await context.bot.send_message(
148137
chat_id=update.message.chat_id,
149-
text="¿Existe un repositorio para tu proyecto?",
138+
text="¿El proyecto tiene un repositorio?",
150139
reply_markup=reply_markup,
151140
)
152141

@@ -167,10 +156,25 @@ async def save_project(username, chat_id, context):
167156
else:
168157
await context.bot.send_message(
169158
chat_id=chat_id,
170-
text="Tu proyecto ha sido cargado"
159+
text="Proyecto cargado"
171160
)
172161

173162

163+
async def present_group_inline_keyboard(chat_id, context):
164+
keyboard = [
165+
[
166+
InlineKeyboardButton("Sí", callback_data=f"{GROUP_EXISTS_PATTERN}:si"),
167+
InlineKeyboardButton("No", callback_data=f"{GROUP_EXISTS_PATTERN}:no"),
168+
]
169+
]
170+
reply_markup = InlineKeyboardMarkup(keyboard)
171+
await context.bot.send_message(
172+
chat_id=chat_id,
173+
text="¿Tu proyecto tiene un grupo de Telegram?",
174+
reply_markup=reply_markup,
175+
)
176+
177+
174178
async def ask_if_repository_exists(update, context):
175179
'''Dialog to ask if a repository exists'''
176180
callback_query = update.callback_query
@@ -179,14 +183,39 @@ async def ask_if_repository_exists(update, context):
179183
if callback_query.data.split(':')[1] == "si":
180184
await context.bot.send_message(
181185
chat_id=chat.id,
182-
text="¿Cuál es la URL del repositorio?",
186+
text="¿Cuál es la URL del repositorio del proyecto?",
183187
)
184188
return REPOSITORIO
185189
else:
186190
await context.bot.send_message(
187191
chat_id=chat.id,
188192
text="Si creás un repositorio, podés agregarlo con /agregar_repositorio."
189193
)
194+
195+
await present_group_inline_keyboard(
196+
chat_id=chat.id,
197+
context=context,
198+
)
199+
200+
return CHECK_GRUPO
201+
202+
203+
async def ask_if_group_exists(update, context):
204+
'''Dialog to ask if a group exists'''
205+
callback_query = update.callback_query
206+
chat = callback_query.message.chat
207+
208+
if callback_query.data.split(':')[1] == "si":
209+
await context.bot.send_message(
210+
chat_id=chat.id,
211+
text="¿Cuál es la URL del grupo del proyecto?",
212+
)
213+
return GRUPO
214+
else:
215+
await context.bot.send_message(
216+
chat_id=chat.id,
217+
text="Si creás un grupo, podés agregarlo con /agregar_grupo."
218+
)
190219
await save_project(callback_query.from_user.username, chat.id, context)
191220
return ConversationHandler.END
192221

@@ -199,13 +228,23 @@ async def project_repository(update, context):
199228
new_project = current_projects[username]
200229
new_project.repository_url = text
201230

202-
await context.bot.send_message(
231+
await present_group_inline_keyboard(
203232
chat_id=update.message.chat_id,
204-
text=f"El repositorio de tu proyecto es: {text}."
233+
context=context,
205234
)
206235

207-
await save_project(username, update.message.chat_id, context)
236+
return CHECK_GRUPO
237+
238+
239+
async def project_group(update, context):
240+
'''Dialog to set project group'''
241+
username = update.message.from_user.username
242+
text = update.message.text
243+
244+
new_project = current_projects[username]
245+
new_project.group_url = text
208246

247+
await save_project(username, update.message.chat_id, context)
209248
return ConversationHandler.END
210249

211250

@@ -218,7 +257,7 @@ async def cancel(update, context):
218257

219258
@active_needed
220259
async def ask_project_name(update, context):
221-
'''Command to start the agregar_repositorio dialog'''
260+
'''Command to start the agregar_repositorio/agregar_grupo dialogs'''
222261
username = update.message.from_user.username
223262

224263
projects = Project.select().join(Pycampista).where(Pycampista.username == username)
@@ -237,14 +276,14 @@ async def ask_project_name(update, context):
237276

238277
await context.bot.send_message(
239278
chat_id=update.message.chat_id,
240-
text="¿A qué proyecto querés agregar un repositorio?",
279+
text="¿Qué proyecto querés modificar?",
241280
reply_markup=reply_markup,
242281
)
243282

244283
return 1
245284

246285

247-
async def ask_repository_name(update, context):
286+
async def ask_repository_url(update, context):
248287
'''Dialog to set project name'''
249288
callback_query = update.callback_query
250289
chat = callback_query.message.chat
@@ -260,6 +299,22 @@ async def ask_repository_name(update, context):
260299
return 2
261300

262301

302+
async def ask_group_url(update, context):
303+
'''Dialog to set project name'''
304+
callback_query = update.callback_query
305+
chat = callback_query.message.chat
306+
307+
username = callback_query.from_user.username
308+
309+
current_projects[username] = callback_query.data.split(':')[1]
310+
311+
await context.bot.send_message(
312+
chat_id=chat.id,
313+
text="¿Cuál es la URL del grupo?",
314+
)
315+
return 2
316+
317+
263318
async def add_repository(update, context):
264319
'''Dialog to set repository'''
265320
username = update.message.from_user.username
@@ -272,7 +327,24 @@ async def add_repository(update, context):
272327

273328
await context.bot.send_message(
274329
chat_id=update.message.chat_id,
275-
text=f'Repositorio "{text}" agregado al proyecto "{current_projects[username]}"',
330+
text=f'Repositorio agregado',
331+
)
332+
return ConversationHandler.END
333+
334+
335+
async def add_group(update, context):
336+
'''Dialog to set group'''
337+
username = update.message.from_user.username
338+
text = update.message.text
339+
340+
project = Project.select().where(Project.name == current_projects[username]).get()
341+
342+
project.group_url = text
343+
project.save()
344+
345+
await context.bot.send_message(
346+
chat_id=update.message.chat_id,
347+
text=f'Grupo agregado',
276348
)
277349
return ConversationHandler.END
278350

@@ -287,7 +359,7 @@ async def start_project_load(update, context):
287359
pycamp.project_load_authorized = True
288360
pycamp.save()
289361

290-
await update.message.reply_text("Autorizadx \nCarga de proyectos Abierta")
362+
await update.message.reply_text("Carga de proyectos Abierta")
291363
await msg_to_active_pycamp_chat(context.bot, "Carga de proyectos Abierta")
292364
else:
293365
await update.message.reply_text("La carga de proyectos ya estaba abierta")
@@ -318,6 +390,8 @@ async def end_project_load(update, context):
318390
TOPIC: [MessageHandler(filters.TEXT, project_topic)],
319391
CHECK_REPOSITORIO: [CallbackQueryHandler(ask_if_repository_exists, pattern=f'{REPO_EXISTS_PATTERN}:')],
320392
REPOSITORIO: [MessageHandler(filters.TEXT, project_repository)],
393+
CHECK_GRUPO: [CallbackQueryHandler(ask_if_group_exists, pattern=f'{GROUP_EXISTS_PATTERN}:')],
394+
GRUPO: [MessageHandler(filters.TEXT, project_group)],
321395
},
322396
fallbacks=[CommandHandler('cancel', cancel)])
323397

@@ -368,12 +442,13 @@ async def show_projects(update, context):
368442
projects = Project.select()
369443
text = []
370444
for project in projects:
371-
project_text = "{} \n Owner: {} \n Temática: {} \n Nivel: {} \n Repositorio: {}".format(
445+
project_text = "{}\n Owner: @{}\n Temática: {}\n Nivel: {}\n Repositorio: {}\n Grupo de Telegram: {}".format(
372446
project.name,
373447
project.owner.username,
374448
project.topic,
375449
project.difficult_level,
376450
project.repository_url or '(ninguno)',
451+
project.group_url or '(ninguno)',
377452
)
378453
participants_count = Vote.select().where(
379454
(Vote.project == project) & (Vote.interest)).count()
@@ -386,7 +461,7 @@ async def show_projects(update, context):
386461
else:
387462
text = "Todavía no hay ningún proyecto cargado"
388463

389-
await update.message.reply_text(text)
464+
await update.message.reply_text(text, link_preview_options=LinkPreviewOptions(is_disabled=True))
390465

391466

392467

@@ -470,16 +545,33 @@ async def show_my_projects(update, context):
470545

471546
def set_handlers(application):
472547
add_repository_handler = ConversationHandler(
473-
entry_points=[CommandHandler('agregar_repositorio', ask_project_name)],
548+
entry_points=[
549+
CommandHandler('agregar_repositorio', ask_project_name),
550+
],
474551
states={
475-
1: [CallbackQueryHandler(ask_repository_name, pattern=f'{PROJECT_NAME_PATTERN}:')],
552+
1: [CallbackQueryHandler(ask_repository_url, pattern=f'{PROJECT_NAME_PATTERN}:')],
476553
2: [MessageHandler(filters.TEXT, add_repository)],
477554
},
478-
fallbacks=[CommandHandler('cancel', cancel)],
555+
fallbacks=[
556+
CommandHandler('cancel', cancel),
557+
],
558+
)
559+
add_group_handler = ConversationHandler(
560+
entry_points=[
561+
CommandHandler('agregar_grupo', ask_project_name),
562+
],
563+
states={
564+
1: [CallbackQueryHandler(ask_group_url, pattern=f'{PROJECT_NAME_PATTERN}:')],
565+
2: [MessageHandler(filters.TEXT, add_group)],
566+
},
567+
fallbacks=[
568+
CommandHandler('cancel', cancel),
569+
],
479570
)
480571

481572
application.add_handler(load_project_handler)
482573
application.add_handler(add_repository_handler)
574+
application.add_handler(add_group_handler)
483575

484576
application.add_handler(
485577
CommandHandler('empezar_carga_proyectos', start_project_load))

0 commit comments

Comments
 (0)