def send_notification_new_message(self, team, tenant): if self.admin_task.assigned_to != self.comment_by: if self.admin_task.assigned_to.slack_user_id != "" and team is not None: blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": self.comment_by.full_name() + " added a message to your task:\n*" + self.admin_task.title + "*\n_" + self.comment + "_" } }, {{ "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "emoji": True, "text": "I have completed this" }, "style": "primary", "value": "admin_task:complete:" + self.pk }] }}] s = Slack() s.send_message(channel=self.slack_user, blocks=blocks) else: send_email_new_comment(self)
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 first_day_reminder(): org = Organization.object.get() if not AccessToken.objects.filter( integration=0).exists() or not org.send_new_hire_start_reminder: return translation.activate(org.language) user = get_user_model().objects.filter(role=1).first() us_state = user.get_local_time() new_hires_starting_today = get_user_model().objects.filter( start_day=datetime.now().date(), role=0) if us_state.hour == 8 and org.send_new_hire_start_reminder and new_hires_starting_today.exists( ): text = '' if new_hires_starting_today.count() == 1: text = _("Just a quick reminder: It's ") + user.full_name + _( "'s first day today!") else: for i in new_hires_starting_today: text += i.get_full_name() + ', ' # remove last comma text = text[:-2] text = _("We got some new hires coming in! ") + text + _( " are starting today!") s = Slack() blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": text } }] s.send_message(blocks=blocks, channel="#general") return True
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 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 save(self, *args, **kwargs): # checking if this is new before saving, sending information after we have the ID. is_new = self.pk is None super(AdminTask, self).save(*args, **kwargs) if is_new and self.option == 1: # through email send_email_notification_to_external_person(self) elif is_new and self.option == 2: blocks = [{ "type": "section", "text": { "type": "mrkdwn", "text": self.assigned_to.full_name() + " needs your help with this task:\n*" + self.title + "*\n_" + self.comment.last().content + "_" } }, {{ "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "emoji": True, "text": "I have completed this" }, "style": "primary", "value": "admin_task:complete:" + self.pk }] }}] s = Slack() s.send_message(channel=self.slack_user, blocks=blocks)
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 introduce_new_people(): org = Organization.object.get() if not AccessToken.objects.filter( integration=0).exists() or not org.ask_colleague_welcome_message: return s = Slack() translation.activate(org.language) new_hires = get_user_model().objects.filter( is_introduced_to_colleagues=False, role=0, start_day__gt=datetime.now().date()) if new_hires.exists(): blocks = [] if new_hires.count() > 1: text = _( "We got some new hires coming in soon! Make sure to leave a welcome message for them!" ) else: text = _( "We have a new hire coming in soon! Make sure to leave a message for " ) + new_hires.first().first_name + "!" blocks.append({ "type": "section", "text": { "type": "mrkdwn", "text": text } }) for new_hire in new_hires: message = "*" + new_hire.full_name() + "*" if new_hire.message != "": message += "\n_" + new_hire.message + "_" block = { "type": "section", "text": { "type": "mrkdwn", "text": message } } if new_hire.profile_image: block["accessory"] = { "type": "image", "image_url": new_hire.profile_image.get_url(), "alt_text": "profile image" } blocks.append(block) footer_extra = "" if new_hire.position is not None and new_hire.position != "": footer_extra = _(" and is our new ") + new_hire.position context = new_hire.first_name + _(" starts on ") + localize( new_hire.start_day) + footer_extra + "." blocks.append({ "type": "context", "elements": [{ "type": "plain_text", "text": context }] }) blocks.append({ "type": "actions", "elements": [{ "type": "button", "text": { "type": "plain_text", "text": _("Welcome this new hire!") }, "value": "dialog:welcome:" + str(new_hire.id) }] }) new_hire.is_introduced_to_colleagues = True new_hire.save() s.send_message(channel="#general", blocks=blocks)
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