Skip to content

Commit

Permalink
more generalized error handling for bot integrations
Browse files Browse the repository at this point in the history
  • Loading branch information
devxpy committed Jul 16, 2024
1 parent 9e21e6d commit 31f8f2d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 36 deletions.
38 changes: 10 additions & 28 deletions daras_ai_v2/bots.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import mimetypes
import traceback
import typing
from datetime import datetime

Expand All @@ -8,7 +7,6 @@
from fastapi import HTTPException
from furl import furl
from pydantic import BaseModel, Field
from sentry_sdk import capture_exception

from app_users.models import AppUser
from bots.models import (
Expand Down Expand Up @@ -47,9 +45,9 @@
)

ERROR_MSG = """
`{}`
⚠️ Sorry, I ran into an error while processing your request. Please try again, or send "Reset" to start over.
⚠️ Sorry, I ran into an error while processing your request. Please try again, or type "Reset" to start over.
`{}`
""".strip()

FEEDBACK_THUMBS_UP_MSG = "🎉 What did you like about my response?"
Expand Down Expand Up @@ -251,8 +249,9 @@ def _mock_api_output(input_text):
def msg_handler(bot: BotInterface):
try:
_msg_handler(bot)
except Exception:
bot.send_msg(text="Sorry, an error occurred. Please try again later.")
except Exception as e:
# send error msg as repsonse
bot.send_msg(text=ERROR_MSG.format(e))
raise


Expand Down Expand Up @@ -336,13 +335,7 @@ def _msg_handler(bot: BotInterface):


def _handle_feedback_msg(bot: BotInterface, input_text):
try:
last_feedback = Feedback.objects.filter(
message__conversation=bot.convo
).latest()
except Feedback.DoesNotExist as e:
bot.send_msg(text=ERROR_MSG.format(e))
return
last_feedback = Feedback.objects.filter(message__conversation=bot.convo).latest()
# save the feedback
last_feedback.text = input_text
# translate feedback to english
Expand Down Expand Up @@ -543,24 +536,13 @@ def _save_msgs(


def _handle_interactive_msg(bot: BotInterface):
try:
button = bot.get_interactive_msg_info()
except NotImplementedError as e:
bot.send_msg(text=ERROR_MSG.format(e))
return
button = bot.get_interactive_msg_info()
match button.button_id:
# handle feedback button press
case ButtonIds.feedback_thumbs_up | ButtonIds.feedback_thumbs_down:
try:
context_msg = Message.objects.get(
platform_msg_id=button.context_msg_id, conversation=bot.convo
)
except Message.DoesNotExist as e:
traceback.print_exc()
capture_exception(e)
# send error msg as repsonse
bot.send_msg(text=ERROR_MSG.format(e))
return
context_msg = Message.objects.get(
platform_msg_id=button.context_msg_id, conversation=bot.convo
)
if button.button_id == ButtonIds.feedback_thumbs_up:
rating = Feedback.Rating.RATING_THUMBS_UP
# bot.convo.state = ConvoState.ASK_FOR_FEEDBACK_THUMBS_UP
Expand Down
12 changes: 4 additions & 8 deletions routers/twilio_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
"Sorry, I didn't get that. Please call again in a more quiet environment."
)

WEBHOOK_ERROR_MSG = "Sorry. This number has been incorrectly configured. Contact the bot integration owner or try again later."

router = APIRouter()


Expand Down Expand Up @@ -241,10 +243,7 @@ def twilio_voice_call_error():
"""If an unhandled error occurs in the voice call webhook, return a generic error message."""

resp = VoiceResponse()
resp.say(
"Sorry. This number has been incorrectly configured. Contact the bot integration owner or try again later."
)

resp.say(WEBHOOK_ERROR_MSG)
return twiml_response(resp)


Expand Down Expand Up @@ -276,10 +275,7 @@ def twilio_sms_error():
"""If an unhandled error occurs in the SMS webhook, return a generic error message."""

resp = MessagingResponse()
resp.message(
"Sorry. This number has been incorrectly configured. Contact the bot integration owner or try again later."
)

resp.message(WEBHOOK_ERROR_MSG)
return twiml_response(resp)


Expand Down

0 comments on commit 31f8f2d

Please sign in to comment.