def test_close(self):
     # CLOSING CONVERSATIONS
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertTrue(conversation.open)
     conversation.close_conversation(admin_id=self.admin.id, body='Closing message')
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertFalse(conversation.open)
 def test_assignment(self):
     # ASSIGNING CONVERSATIONS
     conversation = Conversation.find(id=self.admin_conv.id)
     num_parts = len(conversation.conversation_parts)
     conversation.assign(assignee_id=self.admin.id, admin_id=self.admin.id)
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertEqual(num_parts + 1, len(conversation.conversation_parts))
     self.assertEqual("assignment", conversation.conversation_parts[-1].part_type)
Example #3
0
 def test_close(self):
     # CLOSING CONVERSATIONS
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertTrue(conversation.open)
     conversation.close_conversation(admin_id=self.admin.id,
                                     body='Closing message')
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertFalse(conversation.open)
Example #4
0
 def test_assignment(self):
     # ASSIGNING CONVERSATIONS
     conversation = Conversation.find(id=self.admin_conv.id)
     num_parts = len(conversation.conversation_parts)
     conversation.assign(assignee_id=self.admin.id, admin_id=self.admin.id)
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertEqual(num_parts + 1, len(conversation.conversation_parts))
     self.assertEqual("assignment",
                      conversation.conversation_parts[-1].part_type)
 def test_mark_read(self):
     # MARKING A CONVERSATION AS READ
     conversation = Conversation.find(id=self.admin_conv.id)
     conversation.read = False
     conversation.save()
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertFalse(conversation.read)
     conversation.read = True
     conversation.save()
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertTrue(conversation.read)
 def test_mark_read(self):
     # MARKING A CONVERSATION AS READ
     conversation = Conversation.find(id=self.admin_conv.id)
     conversation.read = False
     conversation.save()
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertFalse(conversation.read)
     conversation.read = True
     conversation.save()
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertTrue(conversation.read)
    def test_conversation_parts(self):
        # INTERACTING WITH THE PARTS OF A CONVERSATION
        convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
        conversation = Conversation.find(id=convo_id)

        # Getting the subject of a part (only applies to email-based
        # conversations)
        self.assertEqual(conversation.conversation_message.subject, "")
        for part in conversation.conversation_parts:
            # There is a part_type
            self.assertIsNotNone(part.part_type)
            # There is a body
            self.assertIsNotNone(part.body)
    def test_conversation_parts(self):
        # INTERACTING WITH THE PARTS OF A CONVERSATION
        convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
        conversation = Conversation.find(id=convo_id)

        # Getting the subject of a part (only applies to email-based
        # conversations)
        self.assertEqual(conversation.conversation_message.subject, "")
        for part in conversation.conversation_parts:
            # There is a part_type
            self.assertIsNotNone(part.part_type)
            # There is a body
            self.assertIsNotNone(part.body)
 def test_reply(self):
     # REPLYING TO CONVERSATIONS
     conversation = Conversation.find(id=self.admin_conv.id)
     num_parts = len(conversation.conversation_parts)
     # User (identified by email) replies with a comment
     conversation.reply(
         type='user', email=self.email,
         message_type='comment', body='foo')
     # Admin (identified by admin_id) replies with a comment
     conversation.reply(
         type='admin', admin_id=self.admin.id,
         message_type='comment', body='bar')
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertEqual(num_parts + 2, len(conversation.conversation_parts))
Example #10
0
    def _predict(self, conversation_id, assignee_id=None):
        """
        Predict the assignee
        """
        conversation = Conversation.find(id=conversation_id)
        messages = [conversation.conversation_message
                    ] + conversation.conversation_parts

        for message in messages:
            if SECRET_MENTION_ID in (message.body or ""):
                # we've already suggested an Admin, don't suggest again
                return

        collection = Collection(COLLECTION_NAME, domain='topics')
        text = extract_messages_text(messages)
        results = collection.predict(text)

        best_prediction = sorted(results.items(), key=itemgetter(1))[-1]
        if best_prediction[1] > ASSIGNMENT_THRESHOLD:
            if assignee_id is not None:
                self._suggest_assignee(conversation, best_prediction[0])
            else:
                self._assign_assignee(conversation, best_prediction[0])

        elif best_prediction[1] > SUGGESTION_THRESHOLD:
            self._suggest_assignee(conversation, best_prediction[0])
        else:
            LOGGER.debug(
                "Prediction did not produce high enough results for conversation {id} with {results}"
                .format(id=conversation_id, results=results))
Example #11
0
    def _add_data_to_collection(self, conversation_id, assignee_id):
        """
        Add data to the collection when a conversation gets assigned
        """

        conversation = Conversation.find(id=conversation_id)
        messages = [conversation.conversation_message
                    ] + conversation.conversation_parts

        for message in messages:
            if SECRET_MENTION_ID in (message.body or ""):
                # we've already suggested an Admin, don't suggest again
                return

        LOGGER.info("Adding data to collections for assignee {id}".format(
            id=assignee_id))

        messages = filter(lambda msg: not isinstance(msg.author, Admin),
                          messages)

        message_text = extract_messages_text(messages)

        collection = Collection(COLLECTION_NAME, domain='topics')
        collection.add_data([message_text, assignee_id])

        try:
            collection.train()
        except IndicoError as e:
            pass
 def test_find_all_unread(self):
     # Iterate over all unread conversations with a user based on the
     # users email
     for convo in Conversation.find_all(email=self.email,
                                        type='user',
                                        unread=True):
         self.assertIsNotNone(convo.id)
 def test_find_all_read(self):
     # Iterate over through all conversations (read + unread) with a
     # user based on the users email
     for convo in Conversation.find_all(email=self.email,
                                        type='user',
                                        unread=False):
         self.assertIsNotNone(convo.id)
    def setup_class(cls):
        # get admin
        cls.admin = Admin.all()[1]

        # get user
        timestamp = get_timestamp()
        cls.user = get_or_create_user(timestamp)
        cls.email = cls.user.email

        # send user message
        message_data = {
            'from': {
                'type': "user",
                'id': cls.user.id
            },
            'body': "Hey"
        }
        cls.user_message = Message.create(**message_data)

        conversations = Conversation.find_all()
        user_init_conv = conversations[0]
        # send admin reply
        cls.admin_conv = user_init_conv.reply(type='admin',
                                              admin_id=cls.admin.id,
                                              message_type='comment',
                                              body='There')
 def test_reply(self):
     # REPLYING TO CONVERSATIONS
     conversation = Conversation.find(id=self.admin_conv.id)
     num_parts = len(conversation.conversation_parts)
     # User (identified by email) replies with a comment
     conversation.reply(type='user',
                        email=self.email,
                        message_type='comment',
                        body='foo')
     # Admin (identified by admin_id) replies with a comment
     conversation.reply(type='admin',
                        admin_id=self.admin.id,
                        message_type='comment',
                        body='bar')
     conversation = Conversation.find(id=self.admin_conv.id)
     self.assertEqual(num_parts + 2, len(conversation.conversation_parts))
    def setup_class(cls):
        # get admin
        cls.admin = Admin.all()[1]

        # get user
        timestamp = get_timestamp()
        cls.user = get_or_create_user(timestamp)
        cls.email = cls.user.email

        # send user message
        message_data = {
            'from': {
                'type': "user",
                'id': cls.user.id
            },
            'body': "Hey"
        }
        cls.user_message = Message.create(**message_data)

        conversations = Conversation.find_all()
        user_init_conv = conversations[0]
        # send admin reply
        cls.admin_conv = user_init_conv.reply(
            type='admin', admin_id=cls.admin.id,
            message_type='comment', body='There')
Example #17
0
def fetch_new_conversation(context):
    cursor = table.get_item(Key={
        'project': 'intercom',
        'id': 'conversation'
    },
                            AttributesToGet=['value'])
    if "Item" in cursor:
        max_id = cursor.get('Item').get('value')
    else:
        max_id = 0

    events = []
    for convo in Conversation.find_all(after=max_id):
        event = {
            'assignee_type': None,
            'assignee_id': convo.assignee.id,
            'subject': convo.conversation_message.subject,
            'author_id': convo.conversation_message.author.id,
            'author_type': None,
            '_time': convo.created_at.isoformat("T"),
            '_user': convo.user.user_id or convo.user.email or convo.user.id
        }
        max_id = max(int(convo.id), max_id)
        events.append({
            'collection': context.get('collection_prefix') + '_conversation',
            'properties': event
        })

    response = requests.post(context.get('rakam_api_url') + "/event/batch",
                             data=json.dumps({
                                 'events': events,
                                 'api': {
                                     'api_key': context.get('rakam_write_key')
                                 }
                             }),
                             headers={'Content-type': 'application/json'})

    if len(events) == 0:
        return

    if response.status_code != 200:
        print('[event] Invalid status code from Rakam {} with response {}'.
              format(response.status_code, response.text))
    else:
        print(
            "[event] collected {} events between {} and {}, new cursor is {}".
            format(len(events), events[0].get('properties').get('_time'),
                   events[len(events) - 1].get('properties').get('_time'),
                   max_id))

    table.update_item(Key={
        'project': 'intercom',
        'id': 'conversation'
    },
                      UpdateExpression='SET #keyval = :val1',
                      ExpressionAttributeNames={'#keyval': 'value'},
                      ExpressionAttributeValues={':val1': max_id})
Example #18
0
def tokenize_conversation_body(cid):
    conversation = Conversation.find(id=cid)
    conversation_body = strip_tags(conversation.conversation_message.body)
    print conversation_body
    words = word_tokenize(conversation_body)
    return words
Example #19
0
def stuff_with_all_convos():
    for admin in Admin.all():
        # need to add unassigned since there will be a lot of those
        for convo in Conversation.find_all(type='admin', id=admin.id):
            print convo.id
 def test_find_all_closed_before_admin(self):
     for convo in Conversation.find_all(type='admin',
                                        id=self.admin.id,
                                        open=False,
                                        before=1374844930):
         self.assertIsNotNone(convo.id)
 def test_find_all_read(self):
     # Iterate over through all conversations (read + unread) with a
     # user based on the users email
     for convo in Conversation.find_all(
             email=self.email, type='user', unread=False):
         self.assertIsNotNone(convo.id)
 def test_find_all_user(self):
     # FINDING CONVERSATIONS FOR A USER
     # Iterate over all conversations (read + unread, correct) with a
     # user based on the users email
     for convo in Conversation.find_all(email=self.email, type='user'):
         self.assertIsNotNone(convo.id)
 def test_find_all_closed_before_admin(self):
     for convo in Conversation.find_all(
             type='admin', id=self.admin.id, open=False,
             before=1374844930):
         self.assertIsNotNone(convo.id)
Example #24
0
def intercom_conversations():
    conversations = Conversation.find_all(open=True)
    print(conversations)
 def test_find_all_admin(self):
     # FINDING CONVERSATIONS FOR AN ADMIN
     # Iterate over all conversations (open and closed) assigned to an admin
     for convo in Conversation.find_all(type='admin', id=self.admin.id):
         self.assertIsNotNone(convo.id)
         self.admin_conv.id = convo.id
 def test_find_all_admin(self):
     # FINDING CONVERSATIONS FOR AN ADMIN
     # Iterate over all conversations (open and closed) assigned to an admin
     for convo in Conversation.find_all(type='admin', id=self.admin.id):
         self.assertIsNotNone(convo.id)
         self.admin_conv.id = convo.id
 def test_find_all_unread(self):
     # Iterate over all unread conversations with a user based on the
     # users email
     for convo in Conversation.find_all(
             email=self.email, type='user', unread=True):
         self.assertIsNotNone(convo.id)
 def test_find_all_closed_admin(self):
     # Iterate over closed conversations assigned to an admin
     for convo in Conversation.find_all(type='admin',
                                        id=self.admin.id,
                                        open=False):
         self.assertIsNotNone(convo.id)
 def test_find_single_conversation(self):
     # FINDING A SINGLE CONVERSATION
     convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
     conversation = Conversation.find(id=convo_id)
     self.assertEqual(conversation.id, convo_id)
 def test_find_all_user(self):
     # FINDING CONVERSATIONS FOR A USER
     # Iterate over all conversations (read + unread, correct) with a
     # user based on the users email
     for convo in Conversation.find_all(email=self.email, type='user'):
         self.assertIsNotNone(convo.id)
 def test_find_single_conversation(self):
     # FINDING A SINGLE CONVERSATION
     convo_id = Conversation.find_all(type='admin', id=self.admin.id)[0].id
     conversation = Conversation.find(id=convo_id)
     self.assertEqual(conversation.id, convo_id)
 def test_find_all_closed_admin(self):
     # Iterate over closed conversations assigned to an admin
     for convo in Conversation.find_all(
             type='admin', id=self.admin.id, open=False):
         self.assertIsNotNone(convo.id)
Example #33
0
def reply_to_user(conversation_id, reply_msg):
    conversation = Conversation.find(id=conversation_id)
    conversation.reply(
        type='admin', id=str(ADMIN_BOT_ID),
        message_type='comment', body=reply_msg)