williamagyapong commited on
Commit
1cf1e40
·
verified ·
1 Parent(s): 24eb598

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +38 -14
main.py CHANGED
@@ -57,10 +57,11 @@ def messenger_button(recipient_phone, message, header='Transaction Confirmation'
57
  },
58
  )
59
 
60
- def messenger_reply_button(recipient_phone, message):
61
  messenger.send_reply_button(
62
  recipient_id=f"{recipient_phone}",
63
  button={
 
64
  "type": "button",
65
  "body": {
66
  "text": f"{message}"
@@ -90,12 +91,39 @@ def respond(query_str: str):
90
  response = "hello, I don't have a brain yet"
91
  return response
92
 
93
- def process_user_msg(message):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  response = str(generateResponse(message))
95
  parsed_trans_data = parse_multiple_transactions(response)
96
  # return None, parsed_trans_data, response
97
  logging.info(f"\nAnswer: {response}\n")
98
- return response, parsed_trans_data
 
 
 
 
 
 
 
 
 
 
 
 
99
 
100
 
101
  def process_intent(response, parsed_trans_data, mobile):
@@ -122,10 +150,10 @@ def process_intent(response, parsed_trans_data, mobile):
122
  elif intent == 'delete':
123
  pass
124
 
125
- elif intent == 'read':
126
- response = str(read_datalake(mobile, message))
127
- parsed_trans_data = ""
128
- firestore_msg = response
129
 
130
  else:
131
  firestore_msg = f'The detected intent, {intent}, is not currently supported!'
@@ -161,11 +189,7 @@ def hook():
161
 
162
  messenger.send_message(message=f"{response}", recipient_id=mobile)
163
  else:
164
- # firestore_msg1, parsed_trans_data1, response1 = process_user_msg(message, mobile)
165
- response, parsed_trans_data = process_user_msg(message)
166
- messenger_reply_button(mobile, f"Raw Response: {response}, \n \n Parsed Response: {parsed_trans_data}")
167
-
168
- # messenger.send_message(f"Raw Response: {response1}, \n \n Parsed Response: {parsed_trans_data1}, \n \n Final Response: {firestore_msg1}", recipient_id=mobile)
169
 
170
  elif message_type == "audio":
171
  audio = messenger.get_audio(data)
@@ -178,10 +202,10 @@ def hook():
178
  transcribed_message = transcript.text
179
 
180
  # logging.info(f"\nAudio: {audio}\n")
181
- response, parsed_trans_data = process_user_msg(message)
182
- messenger_reply_button(mobile, f"Raw Response: {response}, \n \n Parsed Response: {parsed_trans_data}")
183
  # firestore_msg1, parsed_trans_data1, response1 = process_user_msg(transcribed_message, mobile)
184
  # messenger.send_message(message=f"Raw Response: {response1}, \n \n Parsed Response: {parsed_trans_data1}, \n \n Final Response: {firestore_msg1}", recipient_id=mobile)
 
185
  elif message_type == "interactive":
186
  message_response = messenger.get_interactive_response(data)
187
  interactive_type = message_response.get("type")
 
57
  },
58
  )
59
 
60
+ def messenger_reply_button(recipient_phone, message, header = 'Transaction Confirmation'):
61
  messenger.send_reply_button(
62
  recipient_id=f"{recipient_phone}",
63
  button={
64
+ "header": f"{header}",
65
  "type": "button",
66
  "body": {
67
  "text": f"{message}"
 
91
  response = "hello, I don't have a brain yet"
92
  return response
93
 
94
+ def persist_temporary_transaction(transactions, mobile):
95
+ """
96
+ Persists the transaction data temporarily in Firestore.
97
+ """
98
+ intent = transactions[0]['intent'].lower()
99
+ temp_ref = db.collection("users").document(mobile).collection("temp_transactions").document(intent)
100
+ data = {
101
+ "transactions": transactions,
102
+ "status": "pending",
103
+ "created_at": datetime.now().isoformat()
104
+ }
105
+ temp_ref.set(data)
106
+ return True
107
+
108
+
109
+ def process_user_msg(message, mobile):
110
  response = str(generateResponse(message))
111
  parsed_trans_data = parse_multiple_transactions(response)
112
  # return None, parsed_trans_data, response
113
  logging.info(f"\nAnswer: {response}\n")
114
+ intent = parsed_trans_data[0]['intent'].lower()
115
+ if intent == 'read':
116
+ response2 = str(read_datalake(mobile, message))
117
+ # parsed_trans_data = ""
118
+ msg = response2
119
+ messenger.send_message(f"Raw Response: {response}, \n \n Parsed Response: {parsed_trans_data}, \n \n Final Response: {msg}", recipient_id=mobile)
120
+ else:
121
+ # Persist transaction data temporarily for Create, Update, or Delete operations
122
+ persist_temporary_transaction(parsed_trans_data, mobile)
123
+ # Give user the chance to confirm/cancel transaction before processing other intents
124
+ messenger_reply_button(mobile, f"Raw Response: {response}, \n \n Parsed Response: {parsed_trans_data}")
125
+
126
+ return True
127
 
128
 
129
  def process_intent(response, parsed_trans_data, mobile):
 
150
  elif intent == 'delete':
151
  pass
152
 
153
+ # elif intent == 'read':
154
+ # response = str(read_datalake(mobile, message))
155
+ # parsed_trans_data = ""
156
+ # firestore_msg = response
157
 
158
  else:
159
  firestore_msg = f'The detected intent, {intent}, is not currently supported!'
 
189
 
190
  messenger.send_message(message=f"{response}", recipient_id=mobile)
191
  else:
192
+ process_user_msg(message, mobile)
 
 
 
 
193
 
194
  elif message_type == "audio":
195
  audio = messenger.get_audio(data)
 
202
  transcribed_message = transcript.text
203
 
204
  # logging.info(f"\nAudio: {audio}\n")
205
+ process_user_msg(transcribed_message, mobile)
 
206
  # firestore_msg1, parsed_trans_data1, response1 = process_user_msg(transcribed_message, mobile)
207
  # messenger.send_message(message=f"Raw Response: {response1}, \n \n Parsed Response: {parsed_trans_data1}, \n \n Final Response: {firestore_msg1}", recipient_id=mobile)
208
+
209
  elif message_type == "interactive":
210
  message_response = messenger.get_interactive_response(data)
211
  interactive_type = message_response.get("type")