-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathannouncements.py
More file actions
247 lines (218 loc) · 10.1 KB
/
announcements.py
File metadata and controls
247 lines (218 loc) · 10.1 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
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
from telegram import Update, Bot
from telegram.ext import ConversationHandler, CommandHandler, MessageHandler, filters, CallbackContext
from pycamp_bot.models import Project, Pycampista, Vote
from pycamp_bot.commands.auth import get_admins_username
from pycamp_bot.logger import logger
from pycamp_bot.commands.manage_pycamp import active_needed
from pycamp_bot.utils import escape_markdown
PROYECTO, LUGAR, MENSAJE = ["proyecto", "lugar", "mensaje"]
ERROR_MESSAGES = {
"format_error": "Error de formato, recuerde que el formato debe ser el siguiente:\n/anunciar tu_proyecto\nPor favor comienza de nuevo.",
"not_admin": "No tenes proyectos para anunciar y no eres admin.",
"not_found": "No se encontro el proyecto *{project_name}*. Intenta de nuevo por favor.",
"no_admin": "No puedes anunciar proyectos si no eres el owner.\nPor favor contacta un admin"
}
class AnnouncementState:
def __init__(self):
self.username = None
self.p_name = ''
self.current_project = False
self.projects = []
self.owner = ''
self.lugar = ''
self.mensaje = ''
state = AnnouncementState()
async def user_is_admin(pycampist: str) -> bool:
return pycampist in get_admins_username()
async def should_be_able_to_announce(pycampista: str, proyect: Project) -> bool:
return pycampista == proyect.owner.username or await user_is_admin(pycampista)
@active_needed
async def announce(update: Update, context: CallbackContext) -> str:
logger.info("Announcement project process started")
parameters: list[str] = update.message.text.split()
project_name = ("").join(parameters[1:])
state.username = update.message.from_user.username
state.projects = Project.select().join(Pycampista).where(Pycampista.username == state.username)
if len(state.projects) == 0:
if not await user_is_admin(state.username):
await context.bot.send_message(
chat_id=update.message.chat_id,
text=ERROR_MESSAGES["no_admin"],
)
logger.warning(f"Pycampista {state.username} no contiene proyectos creados.")
return ConversationHandler.END
else:
state.projects = Project.select()
return await get_project(update, context)
if len(parameters) == 1:
project_list: str = ""
if len(state.projects) == 0:
await context.bot.send_message(
chat_id=update.message.chat_id,
text="Ingresá el Nombre del Proyecto a anunciar."
)
else:
project_list = "\n".join(p.name for p in state.projects)
await context.bot.send_message(
chat_id=update.message.chat_id,
text=f"""Ingresá el Nombre del Proyecto a anunciar.\n\nTienes los siguientes proyectos:\n{project_list}""",
)
# if len(parameters) > 2:
# print('-Handle correct commands-')
# project_name = (" ").join(parameters[1:])
# state.p_name = project_name
# _projects = Project.select().join(Pycampista).where(Project.name == state.p_name)
# if len(_projects) == 0:
# await context.bot.send_message(
# chat_id=update.message.chat_id,
# text=f"No existe el proyecto: *{escape_markdown(state.p_name)}*.",
# parse_mode='MarkdownV2'
# )
# return ConversationHandler.END
# elif not await should_be_able_to_announce(state.username, _projects[0]):
# await context.bot.send_message(
# chat_id=update.message.chat_id,
# text=ERROR_MESSAGES["no_admin"],
# )
# logger.warn(f"Solicitud de anuncio no autorizada.")
# return ConversationHandler.END
# else:
# await context.bot.send_message(
# chat_id=update.message.chat_id,
# text=f"Anunciando el proyecto: *{escape_markdown(_projects[0].name)}* !!!",
# parse_mode='MarkdownV2'
# )
# state.owner = _projects[0].owner.username
# state.current_project = _projects[0]
# return await get_project(update, context)
return PROYECTO
async def get_project(update: Update, context: CallbackContext) -> str:
'''Dialog to set project to announce'''
logger.info("Getting project")
parameters_list: list[str] = update.message.text.split()
if "/anunciar" in parameters_list:
if len(parameters_list) > 2:
project_name = " ".join(parameters_list[1:])
state.current_project = Project.select().join(Pycampista).where(Project.name == project_name.lower())
if not await should_be_able_to_announce(update.message.from_user.username, state.current_project[0]):
await handle_error(context, update.message.chat_id, "format_error", state.current_project)
logger.warning(f"Project {parameters_list[1]} not found!")
return PROYECTO
else:
await context.bot.send_message(
chat_id=update.message.chat_id,
text="Ingrese lugar donde comienza el proyecto."
)
state.p_name = parameters_list[1].lower()
state.current_project = state.current_project[0]
if len(parameters_list) == 1:
await context.bot.send_message(
chat_id=update.message.chat_id,
text="Ingrese el nombre del proyecto."
)
return PROYECTO
else:
project_name = (" ").join(parameters_list)
c_proyect = Project.select().where(Project.name == project_name).first()
if c_proyect:
if await should_be_able_to_announce(update.message.from_user.username, c_proyect):
state.current_project = c_proyect
state.p_name = c_proyect.name
state.owner = c_proyect.owner.username
await context.bot.send_message(
chat_id=update.message.chat_id,
text="Ingrese lugar donde comienza el proyecto."
)
else:
logger.warning("Solicitud de anuncio no autorizada.")
await handle_error(
context,
update.message.chat_id,
"no_admin"
)
return ConversationHandler.END
else:
logger.warning("Solicitud de anuncio no autorizada.")
await handle_error(
context,
update.message.chat_id,
"not_found",
project_name=project_name
)
logger.warning("Error de formato en la solicitud. No se encontró el proyecto.")
return PROYECTO
return LUGAR
async def meeting_place(update: Update, context: CallbackContext) -> str:
'''Dialog to set the place of the meeting'''
logger.info("Setting place")
state.lugar = update.message.text.capitalize()
await context.bot.send_message(
chat_id=update.message.chat_id,
text="Escribe un mensaje a los pycampistas suscriptos ..."
)
return MENSAJE
async def message_project(update: Update, context: CallbackContext) -> str:
'''Dialog to set project topic'''
state.mensaje = update.message.text.capitalize()
pycampistas: list[Vote] = Vote.select().join(
Pycampista).where(
(Vote.project == Project.select().where(
Project.name == state.current_project.name)) & (Vote.interest))
chat_id_list: list[int] = [user.pycampista.chat_id for user in pycampistas]
for chat_id in chat_id_list:
try:
await context.bot.send_message(
chat_id=chat_id,
text=f'''Está por empezar el proyecto *"{escape_markdown(state.p_name)}"* a cargo de *@{escape_markdown(state.owner)}*\.\n*¿Dónde?* 👉🏼 {escape_markdown(state.lugar)}''',
parse_mode='MarkdownV2'
)
if update.message.from_user.username == state.owner:
await context.bot.send_message(
chat_id=chat_id,
text=f'*Project Owner says:* **{escape_markdown(state.mensaje)}**',
parse_mode='MarkdownV2'
)
else:
await context.bot.send_message(
chat_id=chat_id,
text=f'Admin *@{escape_markdown(update.message.from_user.username)}* says: **{escape_markdown(state.mensaje)}**',
parse_mode='MarkdownV2'
)
except Exception as e:
logger.error(f"Error al enviar el mensaje: {e}")
await context.bot.send_message(
chat_id=update.message.chat_id,
text="Anunciado!"
)
logger.info('Project announced!')
return ConversationHandler.END
async def cancel(update: Update, context: CallbackContext) -> str:
'''Cancel the project announcement'''
await context.bot.send_message(
chat_id=update.message.chat_id,
text="Has cancelado el anuncio del proyecto")
logger.warning("Announcement canceled")
return ConversationHandler.END
async def handle_error(context: CallbackContext, chat_id: int, error_key: str, **kwargs: list) -> None:
error_message = ERROR_MESSAGES[error_key].format(**kwargs)
print('error_message: ', error_message)
'''Handle error messages'''
try:
await context.bot.send_message(
chat_id=chat_id,
text=error_message,
)
except Exception as e:
logger.error(f"Error al enviar el mensaje: {e}")
start_project_handler = ConversationHandler(
entry_points=[CommandHandler('anunciar', announce)],
states={
PROYECTO: [MessageHandler(filters.TEXT & ~filters.COMMAND, get_project)],
LUGAR: [MessageHandler(filters.TEXT & ~filters.COMMAND, meeting_place)],
MENSAJE: [MessageHandler(filters.TEXT & ~filters.COMMAND, message_project)]
},
fallbacks=[CommandHandler('cancel', cancel)]
)
def set_handlers(application):
'''Add handlers to the application'''
application.add_handler(start_project_handler)