Skip to content

Commit

Permalink
Save cookies even if error occurred
Browse files Browse the repository at this point in the history
  • Loading branch information
F33RNI committed Oct 8, 2023
1 parent 7b7ff35 commit 1df561e
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions BardModule.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,6 @@ def process_request(self, request_response: RequestResponseContainer) -> None:
.format(request_response.user["user_name"], request_response.user["user_id"]))
request_response.response = bard_response["content"]

# Save cookies
session_cookies = load_json(self.config["bard"]["cookies_file"], logging_enabled=True)
for i in range(len(session_cookies)):
session_cookies[i]["value"] = self._chatbot.session.cookies.get(session_cookies[i]["name"],
domain=session_cookies[i]["domain"],
path=session_cookies[i]["path"])
save_json(self.config["bard"]["cookies_file"], session_cookies, True)

# Save conversation
logging.info("Saving conversation_id as {} and response_id as {} and choice_id as {}".
format(self._chatbot.conversation_id, self._chatbot.response_id, self._chatbot.choice_id))
Expand All @@ -191,6 +183,18 @@ def process_request(self, request_response: RequestResponseContainer) -> None:
request_response.response = self.messages[lang]["response_error"].replace("\\n", "\n").format(error_text)
request_response.error = True

# Try to save cookies
try:
if self._chatbot and self._chatbot.session and self._chatbot.session.cookies:
session_cookies = load_json(self.config["bard"]["cookies_file"], logging_enabled=True)
for i in range(len(session_cookies)):
session_cookies[i]["value"] = self._chatbot.session.cookies.get(session_cookies[i]["name"],
domain=session_cookies[i]["domain"],
path=session_cookies[i]["path"])
save_json(self.config["bard"]["cookies_file"], session_cookies, True)
except Exception as e:
logging.error("Error saving cookies!", exc_info=e)

# Finish message
BotHandler.async_helper(BotHandler.send_message_async(self.config, self.messages, request_response, end=True))

Expand Down

0 comments on commit 1df561e

Please sign in to comment.