-
Notifications
You must be signed in to change notification settings - Fork 0
/
conversation.py
180 lines (151 loc) · 6.37 KB
/
conversation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# Interacts with the database to follow conversations
from pymongo import MongoClient
from datetime import datetime
from operator import itemgetter
import os
import user
client = MongoClient(os.getenv('ARBITRUR_MONGO_URL'))
conversations = client.bot.conversations
def create(message, user_type = 'user', message_type = 'user_utterance', interaction_name= 'initial_user_message'):
user_id = message.get('user_id')
text = message.get('text')
created_at = message.get('created_at')
# Canonical Conversation is updated only at text recieved and sent.
# Messages are updated at bot interaction
conversation = {
'user_id': user_id,
'messages': [
{
'sender': user_type,
'message': message,
'type': message_type,
'created_at': created_at,
'interaction_name': interaction_name,
}
],
'canonical_conversation': [],
'context': {}
}
return conversations.insert_one(conversation)
def create_delegated(message):
user_id = message['user_id']
create(message, user_type= 'bot', message_type= 'bot_utterance', interaction_name = 'sent_message')
set_finished(user_id)
return True
def update(interaction, interaction_name, message, user_type = 'bot', message_type = 'bot_response'):
user_id = message.get('user_id')
text = message.get('text')
# Dont use created_at from message for conversation, so that makes for harder last message search
new_message = {
'sender': user_type,
'message': message,
'type': message_type,
'created_at':datetime.now(),
'interaction_name': interaction_name,
}
conversations.update({'user_id': user_id}, {'$push': {'messages': new_message}})
return True
def update_canonical_conversation(sender_id, reciever_id, text, sender_type):
new_canonical_message = {
'sender_type': sender_type,
'sender_id': sender_id,
'reciever_id': reciever_id,
'text': text,
'created_at': datetime.now(),
}
if sender_type == 'bot':
user_id = reciever_id
else:
user_id = sender_id
conversations.update({'user_id': user_id}, {'$push': {'canonical_conversation': new_canonical_message}})
return True
def find(user_id):
try:
conversation = [conversation for conversation in list(conversations.find({'user_id': user_id}))][0]
except:
conversation = None
return conversation
def delete(user_id):
try:
list(conversations.remove({'user_id': user_id}))
conversation = True
except:
conversation = None
return conversation
def context(user_id):
try:
context_data = [conversation['context'] for conversation in list(conversations.find({'user_id': user_id}))][0]
context_data.update(user.get(user_id))
except:
context_data=user.get(user_id)
return context_data
def update_context(user_id, field, text):
user_data = user.get(user_id)
if field in user_data:
user.update(user_id, {field: text} )
else:
conversations.update({'user_id': user_id}, {'$set': {'context.'+field: text}})
return True
def find_last_message(user_id):
try:
user_conversation = list(conversations.find({'user_id': user_id}))[0]
last_message_date = max(map(itemgetter('created_at'), user_conversation['messages']))
last_message = [message for message in user_conversation['messages'] if message['created_at'] == last_message_date][0]
except:
last_message = None
return last_message
def is_finished(user_id):
user_conversation = list(conversations.find({'user_id': user_id}))[0]
if 'finished' in user_conversation:
if user_conversation['finished'] == 'true':
return True
return False
def set_finished(user_id):
conversations.update({'user_id': user_id}, {'$set': {'finished': 'true'}})
def get_user_messages(user_id):
user_conversation = list(conversations.find({'user_id': user_id}))[0]
messages = [message['text'] for message in user_conversation['canonical_conversation'] if message['sender_type'] =='user']
return list(set(messages))
def get_canonical_messages(user_id):
user_conversation = list(conversations.find({'user_id': user_id}))[0]
output = []
for message in user_conversation['canonical_conversation']:
if 'BEGIN:VCARD' in message['text']:
card_name = message['text'].split('FN:')[1].split('\n')[0]
card_phone = message['text'].split('\nEND:')[0].split(':')[-1]
message['text'] = f'[Usuario mando tarjeta de {card_name}({card_phone})]'
if 'video upload disabled' in message['text'].lower():
message['text'] = f'[Usuario mando un video]'
if message['sender_type'] == 'user':
message['text'] = '👤: *' + message['text'] + '*'
output.append(message)
output = sorted(output, key=lambda x: x['created_at'], reverse=True)
return output
def get_printable_conversation(user_id):
user_messages = ["- " + message['text'] for message in get_canonical_messages(user_id)]
return '\n'.join(user_messages)
def get_last_message(user_id):
return get_user_messages(user_id)[-1]
def get_last_canonical_message_id(user_id):
user_conversation = list(conversations.find({'user_id': user_id}))[0]
messages = user_conversation['canonical_conversation']
return len(messages)-1
def get_canonical_message(user_id, message_id):
user_conversation = list(conversations.find({'user_id': user_id}))[0]
messages = user_conversation['canonical_conversation']
if message_id is None: return {'text': '*No se encontro mensaje*'}
return messages[message_id]
def get_canonical_user_message(user_id, position=0):
# The idea is to use -1 for last canonical last user message, an 0 for first
user_conversation = list(conversations.find({'user_id': user_id}))[0]
messages = user_conversation['canonical_conversation']
counter = 0
for i, message in enumerate(messages):
if message['sender_type'] == 'user':
user_message_pos = i
if position == 0:
return user_message_pos
return user_message_pos
def get_current_redirect_user(agent_id):
agent_context = context(agent_id)
return agent_context['redirect_user']