def process_condition(self, user): from sequences.serializers import PendingAdminTaskSerializer from admin_tasks.models import AdminTaskComment items_added = { 'to_do': [], 'resources': [], 'badges': [], 'introductions': [] } for i in self.to_do.all(): if not user.to_do.filter(pk=i.pk).exists(): items_added['to_do'].append(i) user.to_do.add(i) for i in self.resources.all(): if not user.resources.filter(pk=i.pk).exists(): items_added['resources'].append(i) user.resources.add(i) for i in self.badges.all(): if not user.badges.filter(pk=i.pk).exists(): items_added['badges'].append(i) user.badges.add(i) for i in self.introductions.all(): if not user.introductions.filter(pk=i.pk).exists(): items_added['introductions'].append(i) user.introductions.add(i) for i in self.admin_tasks.all(): if not AdminTask.objects.filter(new_hire=user, assigned_to=i.assigned_to, name=i.name).exists(): serializer = PendingAdminTaskSerializer(i).data serializer.pop('assigned_to') serializer.pop('id') comment = serializer.pop('comment') task = AdminTask.objects.create(**serializer, assigned_to=i.assigned_to, new_hire=user) if comment is not None: AdminTaskComment.objects.create(content=comment, comment_by=task.assigned_to, admin_task=task) for i in self.external_messages.all(): if i.get_user(user) == None: continue if i.send_via == 0: # email send_sequence_message(i.get_user(user), i.email_message()) elif i.send_via == 1: # slack s = Slack() s.set_user(i.get_user(user)) blocks = [] for j in i.content_json.all(): blocks.append(j.to_slack_block(user)) s.send_message(blocks=blocks) else: # text if i.get_user(user).phone is not None and i.get_user(user).phone != "": client = Client(settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN) client.messages.create( to=i.get_user(user).phone, from_=settings.TWILIO_FROM_NUMBER, body=i.content) return items_added
def update_new_hire(): if not AccessToken.objects.filter(integration=0).exists(): return s = Slack() for user in get_user_model().objects.filter(slack_user_id__isnull=False, role=0): local_datetime = user.get_local_time() if local_datetime.hour == 8 and local_datetime.weekday( ) < 5 and local_datetime.date() >= user.start_date: s.set_user(user) # overdue items tasks = ToDoUser.objects.filter( user=user, completed=False, to_do__due_on_day__lt=user.workday()).exclude( to_do__due_on_day=0) if tasks.exists(): blocks = s.format_to_do_block(pre_message=_( "Some to do items are overdue. Please complete those as " "soon as possible!"), items=tasks) s.send_message(blocks=blocks) # to do items for today tasks = ToDoUser.objects.filter(user=user, completed=False, to_do__due_on_day=user.workday()) if tasks.exists(): blocks = s.format_to_do_block(pre_message=_( "Good morning! These are the tasks you need to complete " "today:"), items=tasks) s.send_message(blocks=blocks) return
def timed_triggers(): for user in get_user_model().objects.filter(role=0): # make sure it's 8 AM for the new hire if user.get_local_time().hour == 8: translation.activate(user.language) amount_days = user.workday() amount_days_before = user.days_before_starting() # check if it's before or after they start conditions = [] if amount_days == 0: conditions = user.conditions.filter(condition_type=2, days=amount_days_before) elif user.get_local_time().weekday() < 5: conditions = user.conditions.filter(condition_type=0, days=amount_days) # process conditions and send it through Slack/email for i in conditions: items = i.process_condition(user) if user.slack_user_id is not None: s = Slack() s.set_user(user) s.send_sequence_triggers(items, None) else: send_sequence_update_message(user, items) return
def post(self, request, id): ext_message = ExternalMessage.objects.get(id=id) if ext_message.send_via == 0: # email send_sequence_message(request.user, ext_message.email_message()) elif ext_message.send_via == 1: # slack s = Slack() s.set_user(request.user) blocks = [] for j in ext_message.content_json.all(): blocks.append(j.to_slack_block(request.user)) s.send_message(blocks=blocks) return Response()
def post(self, request, id): # reminding someone t_u = ResourceUser.objects.get(id=id) t_u.reminded = datetime.now() t_u.save() if t_u.user.slack_user_id: s = SlackBot() s.set_user(t_u.user) blocks = s.format_resource_block(pre_message=_('Don\'t forget this to do item!'), items=[t_u]) s.send_message(blocks=blocks) else: send_reminder_email(t_u) return Response()
def put(self, request, id): # reopen task t_u = ResourceUser.objects.get(id=id) t_u.completed_course = False t_u.answers.clear() t_u.save() if t_u.user.slack_user_id: s = SlackBot() s.set_user(t_u.user) blocks = s.format_resource_block( pre_message=_('This task has just been reopened! ' + request.data['message']), items=[t_u]) s.send_message(blocks=blocks) else: email_reopen_task(t_u, request.data['message'], t_u.user) return Response()
def post(self, request, id): ext_message = ExternalMessage.objects.select_related( 'send_to').prefetch_related('content_json').get(id=id) if ext_message.send_via == 0: # email send_sequence_message(request.user, ext_message.email_message(), ext_message.subject) elif ext_message.send_via == 1: # slack # User is not connected to slack. Needs -> employees -> 'give access' if request.user.slack_channel_id == None: return Response({'slack': 'not exist'}, status=status.HTTP_400_BAD_REQUEST) s = Slack() s.set_user(request.user) blocks = [] for j in ext_message.content_json.all(): blocks.append(j.to_slack_block(request.user)) s.send_message(blocks=blocks) return Response()
def give_slack_access(self, request, pk): user = self.get_object() s = SlackBot() response = s.find_by_email(user.email) if response: user.slack_user_id = response['user']['id'] user.save() translation.activate(user.language) blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": _("Click on the button to see all the categories that are available to you!" ) } }, { "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "text": _("resources") }, "style": "primary", "value": "show:resources" }] }] s.set_user(user) res = s.send_message(blocks=blocks) user.slack_channel_id = res['channel'] user.save() return Response() return Response( { "error": _('We couldn\'t find anyone in Slack with the same email address.' ) }, status=status.HTTP_400_BAD_REQUEST)
def link_slack_users(): if not AccessToken.objects.filter(integration=0).exists(): return s = Slack() for user in get_user_model().objects.filter(slack_user_id__isnull=True, role=0): response = s.find_by_email(email=user.email.lower()) if response: translation.activate(user.language) user.slack_user_id = response['user']['id'] user.save() s.set_user(user) blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": WelcomeMessage.objects.get(language=user.language, message_type=3).message }, }] # check if extra buttons need to be send with it as well if s.org.slack_buttons: blocks.extend([{ "type": "section", "text": { "type": "mrkdwn", "text": _("Click a button to see more information :)") } }, { "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "text": "To do items" }, "value": "to_do" }, { "type": "button", "text": { "type": "plain_text", "text": "Resources" }, "value": "resources" }] }]) # adding introduction items introductions = user.introductions.all() if introductions.exists(): for i in introductions: text = '*' + i.name + ':* ' + i.intro_person.full_name( ) + '\n' if i.intro_person.position is not None and i.intro_person.position != '': text += i.intro_person.position + '\n' if i.intro_person.message is not None and i.intro_person.message != "": text += '_' + s.personalize( i.intro_person.message) + '_\n' if i.intro_person.email is not None and i.intro_person.email != "": text += i.intro_person.email + ' ' if i.intro_person.phone is not None and i.intro_person.phone != "": text += i.intro_person.phone block = { "type": "section", "text": { "type": "mrkdwn", "text": text } } if i.intro_person.profile_image is not None: block["accessory"] = { "type": "image", "image_url": i.intro_person.profile_image.get_url(), "alt_text": "profile image" } blocks.append(block) res = s.send_message(blocks=blocks, channel=response['user']['id']) user.slack_channel_id = res['channel'] user.save() # send user to do items for that day (and perhaps over due ones) tasks = ToDoUser.objects.filter( user=user, completed=False, to_do__due_on_day__lte=user.workday()).exclude( to_do__due_on_day=0) if tasks.exists(): blocks = s.format_to_do_block( pre_message=_("These are the tasks you need to complete:"), items=tasks) s.send_message(blocks=blocks) return True
def link_slack_users(): if not AccessToken.objects.filter(integration=0).exists(): return s = Slack() for user in get_user_model().objects.filter(slack_user_id__isnull=True, role=0): response = s.find_by_email(email=user.email.lower()) if response: translation.activate(user.language) user.slack_user_id = response['user']['id'] user.save() s.set_user(user) blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": s.personalize( WelcomeMessage.objects.get(language=user.language, message_type=3).message) }, }] # check if extra buttons need to be send with it as well if s.org.slack_buttons: blocks.extend([{ "type": "section", "text": { "type": "mrkdwn", "text": _("Click a button to see more information :)") } }, { "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "text": "To do items" }, "value": "to_do" }, { "type": "button", "text": { "type": "plain_text", "text": "Resources" }, "value": "resources" }] }]) # adding introduction items introductions = user.introductions.all() for i in introductions: blocks.append(s.format_intro_block(i)) res = s.send_message(blocks=blocks, channel=response['user']['id']) user.slack_channel_id = res['channel'] user.save() # send user to do items for that day (and perhaps over due ones) tasks = ToDoUser.objects.filter( user=user, completed=False, to_do__due_on_day__lte=user.workday()).exclude( to_do__due_on_day=0) if tasks.exists(): blocks = s.format_to_do_block( pre_message=_("These are the tasks you need to complete:"), items=tasks) s.send_message(blocks=blocks) return True