Example #1
0
 def get_question_id_message(self, message):
     if message.is_replier_message:
         question_replier = QuestionReplier.get_by_message_id(
             self._session, message.id)
         question = Question.get_question_id(self._session,
                                             question_replier.question_id)
     else:
         question = Question.get_question(self._session, message.id)
     return question.id
Example #2
0
 def update_message_text(self, message_text, last_message, message):
     """Add a line for continuos messages in database."""
     question = Question.get_question(self._session, last_message.id)
     updated_text = question.message_text + '\n' + message_text
     self._session.query(Question).filter_by(
         message_id=last_message.id).update({
             "message_id": message.id,
             "message_text": updated_text,
             'new_message_user': 1
         })
Example #3
0
    def _is_resolved(self, message):
        """Check whether the question has been marked as resolved or not"""
        if message.is_replier_message:
            question_replier = QuestionReplier.get_by_message_id(
                self._session, message.id)
            question_is_resolved = question_replier.question.is_resolved
        else:
            question = Question.get_question(self._session, message.id)
            question_is_resolved = question.is_resolved

        return question_is_resolved
Example #4
0
 def get_expert_info_to_reply(self, message):
     if message.is_replier_message:
         question_replier = QuestionReplier.get_by_message_id(
             self._session, message.id)
     else:
         question = Question.get_question(self._session, message.id)
         question_replier = next((x for x in question.question_repliers
                                  if x.reply_status_id == 3))
     user_id = question_replier.message.slack_user_id
     message_id = question_replier.message.slack_message_id
     channel_id = question_replier.replier.channel_id
     return channel_id, message_id, question_replier.id
Example #5
0
    def reply_question(self, event):
        """Handle messages that are sent to existing thread"""
        message = Message.get_message(self._session, event.thread_id,
                                      event.user)

        if self._is_resolved(message):
            self.send_closed_message(event, message)
            return

        if message.is_replier_message:
            question_replier = QuestionReplier.get_by_message_id(
                self._session, message_id=message.id)
            question = Question.get_question_id(
                self._session, question_id=question_replier.question_id)
            if not self.is_correct_replier(event.bot, message):
                return
            channel_id, message_id, replier_question_id = self.get_user_info_to_reply(
                message)
            self.delete_previous_scheduled_messages(event.bot, event.channel,
                                                    replier_question_id, 3)
            event.bot.delete_message_in_history(
                event.channel, event.thread_id, [
                    WANT_ANSWER_QUESTION, WANT_CONTINUE_RESPONDING,
                    M_ASKS_REPLIER_CLOSE_QUESTION
                ])
            attachments_type = ''
        else:
            question = Question.get_question(self._session,
                                             message_id=message.id)
            channel_id, message_id, _ = self.get_expert_info_to_reply(message)
            attachments_type = ATTACHMENTS_keep_answering_or_not
            event.bot.delete_message_in_history(event.channel, event.thread_id,
                                                [QUESTION_ANSWERED])
            message_txt = WANT_CONTINUE_RESPONDING

        # if this question has continous messages
        if question.new_message_user == 1:
            # add some text as alert in order to avoid this behaviour again
            event.bot.post_message(
                channel_id,
                USER_ALERT + question.message_text + '\n\n' + USER_ALERT_2,
                message_id, attachments_type)
            self._session.query(Question).filter_by(id=question.id).update(
                {'new_message_user': 0})

        event.bot.post_message(channel_id, event.message_text, message_id)
        if attachments_type != '':
            event.bot.post_message(channel_id, message_txt, message_id,
                                   attachments_type)
        self.handle_scheduled_message(event, message)
Example #6
0
 def get_user_info_to_reply(self, message):
     if message.is_replier_message:
         question_replier = QuestionReplier.get_by_message_id(
             self._session, message.id)
         user_id = question_replier.question.message.slack_user_id
         message_id = question_replier.question.message.slack_message_id
         question_replier_id = question_replier.id
     else:
         question = Question.get_question(self._session, message.id)
         user_id = question.message.slack_user_id
         message_id = question.message.slack_message_id
         question_replier_id = None
     replier = Replier.get_replier_by_slack_id(self._session, user_id)
     channel_id = replier.channel_id
     return channel_id, message_id, question_replier_id
Example #7
0
    def forward_message(self, event, replier_level_id=MIN_LEVEL_ID):
        """Send the question to everyone on MIN_LEVEL_ID and save the info of any threads to database"""
        user = Replier.get_replier_by_channel_id(self._session, event.channel)
        message = Message.get_last_message_of_user(self._session,
                                                   slack_user_id=user.slack_id,
                                                   is_replier=False)
        question = Question.get_question(self._session, message_id=message.id)

        question_repliers = self.forward_first_message(event.bot,
                                                       question.message_text,
                                                       replier_level_id,
                                                       user.slack_id)

        db_question_repliers = self.add_question_repliers_to_db(
            question, question_repliers)

        timeout = self.get_suitable_timeout()

        self.time_to_level_up(event.bot, db_question_repliers, timeout)