kanha-upadhyay commited on
Commit
e8283b9
Β·
1 Parent(s): e1760df

Add file processing logic to prevent duplicate file handling in BotController

Browse files
Files changed (1) hide show
  1. src/controllers/_bot_controller.py +28 -11
src/controllers/_bot_controller.py CHANGED
@@ -8,6 +8,8 @@ from src.models import ContentDeliveryFrequency
8
  from src.services import FileService, SlackBotService, SlackTeamService, UserService
9
  from src.utils import logger
10
 
 
 
11
 
12
  class BotController:
13
  def __init__(self):
@@ -23,11 +25,25 @@ class BotController:
23
  return f"""Hi {firstname} πŸ‘‹\n\nWelcome to the Mandala Slackbot! Every week, you'll receive a learning tailored from the Resilient Leadership program to support you on your leadership journey. If you'd like to make this content even more personalized, share your Resilient Leadership guidebook with us either as an attachment or a link to the slide deck.\n\nOtherwise, we'll plan on sharing our curated weekly insights to keep you growing. πŸ™‚\n\nYou can refresh the content at any time by simply typing */refresh* and sending that guidebook back our way. Looking forward to getting started! Your first learning will arrive next week.\n\nWarmly,\nTeam Mandala"""
24
 
25
  async def process_event_background(self, event: dict):
 
 
 
26
  try:
27
-
28
  actual_event = event["event"]
29
- slack_user_id = actual_event.get("user") or actual_event.get("user_id")
 
 
 
 
 
 
 
 
 
 
 
30
 
 
31
  async with UserService() as user_service:
32
  user = await user_service.get_user_by_slack_id(slack_user_id)
33
  if (
@@ -44,14 +60,17 @@ class BotController:
44
  raise Exception("User not found")
45
 
46
  if "files" in actual_event or "file" in actual_event:
47
- await user_service.send_message(
48
- user.id,
49
- "You're very welcome! πŸŽ‰ We'll be sharing these insights with you gradually over the next few weeks. 😊",
50
- )
 
51
  await self.handle_files(event, actual_event, user, user_service)
52
-
53
  except Exception as e:
54
  logger.error(f"Background task error: {e}")
 
 
 
55
 
56
  async def handle_new_user(self, event, slack_user_id, user_service):
57
  async with SlackTeamService() as slack_team_service:
@@ -112,15 +131,13 @@ class BotController:
112
 
113
  uploaded_file = await slack_bot_service.save_file(team, file)
114
  if uploaded_file:
115
- await file_service.process_file(
116
  file=uploaded_file,
117
  user=user,
118
  slack_file_id=file_id,
119
  slack_token=team.token,
120
  )
121
- else:
122
- logger.error(f"Failed to save file {file_id}")
123
-
124
  except Exception as e:
125
  logger.error(f"Error processing file {file_id}: {e}")
126
  await user_service.send_message(
 
8
  from src.services import FileService, SlackBotService, SlackTeamService, UserService
9
  from src.utils import logger
10
 
11
+ file_set = set()
12
+
13
 
14
  class BotController:
15
  def __init__(self):
 
25
  return f"""Hi {firstname} πŸ‘‹\n\nWelcome to the Mandala Slackbot! Every week, you'll receive a learning tailored from the Resilient Leadership program to support you on your leadership journey. If you'd like to make this content even more personalized, share your Resilient Leadership guidebook with us either as an attachment or a link to the slide deck.\n\nOtherwise, we'll plan on sharing our curated weekly insights to keep you growing. πŸ™‚\n\nYou can refresh the content at any time by simply typing */refresh* and sending that guidebook back our way. Looking forward to getting started! Your first learning will arrive next week.\n\nWarmly,\nTeam Mandala"""
26
 
27
  async def process_event_background(self, event: dict):
28
+ file_ids = []
29
+ global file_set
30
+ send_message = True
31
  try:
 
32
  actual_event = event["event"]
33
+ if "files" in actual_event:
34
+ file_ids = [file.get("id") for file in actual_event.get("files", [])]
35
+ if all(file_id in file_set for file_id in file_ids):
36
+ return
37
+ if any(file_id in file_set for file_id in file_ids):
38
+ send_message = False
39
+ file_set.update(file_ids)
40
+ if "file" in actual_event:
41
+ file_ids = [actual_event.get("file").get("id")]
42
+ if any(file_id in file_set for file_id in file_ids):
43
+ return
44
+ file_set.update(file_ids)
45
 
46
+ slack_user_id = actual_event.get("user") or actual_event.get("user_id")
47
  async with UserService() as user_service:
48
  user = await user_service.get_user_by_slack_id(slack_user_id)
49
  if (
 
60
  raise Exception("User not found")
61
 
62
  if "files" in actual_event or "file" in actual_event:
63
+ if send_message:
64
+ await user_service.send_message(
65
+ user.id,
66
+ "You're very welcome! πŸŽ‰ We'll be sharing these insights with you gradually over the next few weeks. 😊",
67
+ )
68
  await self.handle_files(event, actual_event, user, user_service)
 
69
  except Exception as e:
70
  logger.error(f"Background task error: {e}")
71
+ logger.exception(e)
72
+ finally:
73
+ file_set.difference_update(file_ids)
74
 
75
  async def handle_new_user(self, event, slack_user_id, user_service):
76
  async with SlackTeamService() as slack_team_service:
 
131
 
132
  uploaded_file = await slack_bot_service.save_file(team, file)
133
  if uploaded_file:
134
+ return await file_service.process_file(
135
  file=uploaded_file,
136
  user=user,
137
  slack_file_id=file_id,
138
  slack_token=team.token,
139
  )
140
+ raise Exception(f"Failed to save file")
 
 
141
  except Exception as e:
142
  logger.error(f"Error processing file {file_id}: {e}")
143
  await user_service.send_message(