def get_last_state(self, slack_user_id, team_id, bot=None): team_bot = None if bot: state = UserInteraction.objects.filter( \ employee__user__username=slack_user_id, bot=bot).last() team_bot = bot.teams.filter(slack_team_id=team_id).first() else: state = UserInteraction.objects.filter( \ employee__user__username=slack_user_id, bot__isnull=True).last() if not state: if bot: employee = get_employee(slack_user_id, team_id, team_bot) else: employee = get_employee(slack_user_id, team_id) if not employee: return None if bot: state = UserInteraction.objects.create(employee=employee, bot=bot) else: state = UserInteraction.objects.create(employee=employee) return state
def save_ic_message(team, msg, dm_status, team_bot=None): employee = get_employee(msg['user'], team.slack_team_id, team_bot) if employee: IncomingMessage.objects.create(team=team, employee=employee, dm=dm_status, channel=msg['channel'], msg_text=msg['text'], team_bot=team_bot)
def extract_mentions(text, team_id, myself=None, team_bot=None): from bot.models import get_employee mentions = re.findall('<@\w+>', text.upper()) is_everyone = re.findall('<!everyone>', text) employees = [] if is_everyone and team_id and myself: slack_bot_user_id = myself.get_team().slack_bot_user_id employees = Employee.objects.filter(company__name=team_id).exclude(user__username=slack_bot_user_id).exclude(id=myself.id).all() return employees mentions = list(set(mentions)) for mention in mentions: slack_user_id = mention[2:-1] employee = get_employee(slack_user_id=slack_user_id, team_id=team_id, team_bot=team_bot) if employee: employees.append(employee) return employees
def record_message_for_interactive_message_responses(self, json_data, bot=None): slack_team_id = json_data['team']['id'] team = Team.objects.filter(slack_team_id=slack_team_id).first() employee = get_employee(json_data['user']['id'], slack_team_id) team_bot = None if bot: team_bot = TeamBot.objects.filter(bot=bot, team=team).first() original_message = json_data['original_message'] if not 'attachments' in original_message: return None attachment = original_message["attachments"][0] msg_text = original_message['text'] if 'title' in attachment: msg_text += " | Attachment Title: " + attachment['title'] if 'text' in attachment: msg_text += " | Attachment Text: " + attachment['text'] msg_text += " | Button Value: " + json_data['actions'][0]['value'] channel = json_data['channel']['id'] IncomingMessage.objects.create(team=team, channel=channel, employee=employee, msg_text=msg_text, team_bot=team_bot)
def edubot_collaborate(sc, user_id, team_id, channel_id, message): bot = Bot.objects.filter(name='edubot').first() team_bot = bot.teams.filter(slack_team_id=team_id).first() if user_id == 'USLACKBOT': return None message = message.lower().lstrip().strip() employee = get_employee(user_id, team_id) ui = UserInteraction.get_last_state(user_id, team_id, bot) if message in ['help', 'hi', 'sup', 'hello', 'hey', 'yo']: ui.reset() if employee == team_bot.owner: return respond(sc, user_id, team_id, channel_id, get_edubot_help(), True, OutgoingMessage.TEXT, None, team_bot.id) else: return respond(sc, user_id, team_id, channel_id, get_edubot_student_help(), True, OutgoingMessage.TEXT, None, team_bot.id) elif message in get_edubot_known_commands(): ui.reset() if (((message in get_edubot_student_commands()) and (employee == team_bot.owner)) or ((message in get_edubot_professor_commands()) and (not (employee == team_bot.owner)))): res_message = 'Sorry..You are not authorized to perform this action.' return respond(sc, user_id, team_id, channel_id, res_message, True, OutgoingMessage.TEXT, None, team_bot.id) if ui.state == UserInteraction.EDUBOT_POLL_AWAITED: text, attachment = ui.receive_edubot_poll_question(message, team_bot) ui.reset() return respond(sc, user_id, team_id, channel_id, text, True, OutgoingMessage.TEXT, attachment, team_bot.id) elif ui.state == UserInteraction.EDUBOT_STUDENT_QUESTION_AWAITED: res_message = ui.create_and_post_on_channel(sc, team_bot, message, employee) ui.reset() return respond(sc, user_id, team_id, channel_id, res_message, True, OutgoingMessage.TEXT, None, team_bot.id) if message == 'exception': res_message = 'Something went wrong. Waking up my creators. Please bear with me till they fix this.' respond(sc, user_id, team_id, channel_id, res_message, True, OutgoingMessage.TEXT, None, team_bot.id) elif message == 'poll': respond(sc, user_id, team_id, channel_id, ui.send_poll_help()) elif message == 'feedback': respond(sc, user_id, team_id, channel_id, ui.send_feedback_help()) elif message == 'summary': text, attachments = summary_snapshot(user_id, team_id) respond(sc, user_id, team_id, channel_id, text, True, OutgoingMessage.TEXT, attachments) elif message == 'ask': respond(sc, user_id, team_id, channel_id, ui.send_ask_help()) else: ui.reset() res_message = ':wave: I am afraid I did not understand. Please type `help` to know more about me.' respond(sc, user_id, team_id, channel_id, res_message, True, OutgoingMessage.TEXT, None, team_bot.id)