diff --git a/bots/models.py b/bots/models.py index 7829106e1..99f9779ff 100644 --- a/bots/models.py +++ b/bots/models.py @@ -1111,7 +1111,9 @@ class Meta: "slack_channel_is_personal", ], ), - models.Index(fields=["bot_integration", "twilio_phone_number"]), + models.Index( + fields=["bot_integration", "twilio_phone_number", "twilio_call_sid"] + ), models.Index(fields=["-created_at", "bot_integration"]), ] diff --git a/daras_ai_v2/twilio_bot.py b/daras_ai_v2/twilio_bot.py index 4529952ec..0a4a792c2 100644 --- a/daras_ai_v2/twilio_bot.py +++ b/daras_ai_v2/twilio_bot.py @@ -86,7 +86,7 @@ class TwilioVoice(BotInterface): platform = Platform.TWILIO @classmethod - def from_data(cls, data: dict): + def from_webhook_data(cls, data: dict): ## data samples: # {'AccountSid': ['XXXX'], 'ApiVersion': ['2010-04-01'], 'CallSid': ['XXXX'], 'CallStatus': ['ringing'], 'CallToken': ['XXXX'], 'Called': ['XXXX'], 'CalledCity': ['XXXX'], 'CalledCountry': ['XXXX'], 'CalledState': ['XXXX'], 'CalledZip': ['XXXX'], 'Caller': ['XXXX'], 'CallerCity': ['XXXX'], 'CallerCountry': ['XXXX'], 'CallerState': ['XXXX'], 'CallerZip': ['XXXX'], 'Direction': ['inbound'], 'From': ['XXXX'], 'FromCity': ['XXXX'], 'FromCountry': ['XXXX'], 'FromState': ['XXXX'], 'FromZip': ['XXXX'], 'StirVerstat': ['XXXX'], 'To': ['XXXX'], 'ToCity': ['XXXX'], 'ToCountry': ['XXXX'], 'ToState': ['XXXX'], 'ToZip': ['XXXX']} # {'AccountSid': ['XXXX'], 'ApiVersion': ['2010-04-01'], 'CallSid': ['XXXX'], 'CallStatus': ['in-progress'], 'Called': ['XXXX'], 'CalledCity': ['XXXX'], 'CalledCountry': ['XXXX'], 'CalledState': ['XXXX'], 'CalledZip': ['XXXX'], 'Caller': ['XXXX'], 'CallerCity': ['XXXX'], 'CallerCountry': ['XXXX'], 'CallerState': ['XXXX'], 'CallerZip': ['XXXX'], 'Confidence': ['0.9128386'], 'Direction': ['inbound'], 'From': ['XXXX'], 'FromCity': ['XXXX'], 'FromCountry': ['XXXX'], 'FromState': ['XXXX'], 'FromZip': ['XXXX'], 'Language': ['en-US'], 'SpeechResult': ['Hello.'], 'To': ['XXXX'], 'ToCity': ['XXXX'], 'ToCountry': ['XXXX'], 'ToState': ['XXXX'], 'ToZip': ['XXXX']} @@ -96,25 +96,29 @@ def from_data(cls, data: dict): if account_sid == settings.TWILIO_ACCOUNT_SID: account_sid = "" call_sid = data["CallSid"][0] - caller_number = data["Caller"][0] user_number, bot_number = data["From"][0], data["To"][0] try: # cases where user is calling the bot bi = BotIntegration.objects.get( twilio_account_sid=account_sid, twilio_phone_number=bot_number ) + will_be_missed = bi.twilio_use_missed_call except BotIntegration.DoesNotExist: # cases where bot is calling the user user_number, bot_number = bot_number, user_number bi = BotIntegration.objects.get( twilio_account_sid=account_sid, twilio_phone_number=bot_number ) + will_be_missed = False - will_be_missed = caller_number == user_number and bi.twilio_use_missed_call if will_be_missed: - # for call_sids that we will reject and re-call, the convo is not used, so we don't want to create a new one - convo = None - if bi.twilio_fresh_conversation_per_call and not will_be_missed: + # for calls that we will reject and callback, the convo is not used so we don't want to create one + convo = Conversation( + bot_integration=bi, + twilio_phone_number=user_number, + twilio_call_sid=call_sid, + ) + elif bi.twilio_fresh_conversation_per_call: convo = Conversation.objects.get_or_create( bot_integration=bi, twilio_phone_number=user_number, @@ -124,6 +128,7 @@ def from_data(cls, data: dict): convo = Conversation.objects.get_or_create( bot_integration=bi, twilio_phone_number=user_number )[0] + return cls( convo, text=data.get("SpeechResult", [None])[0], diff --git a/routers/twilio_api.py b/routers/twilio_api.py index 6913a3ca7..152de7f77 100644 --- a/routers/twilio_api.py +++ b/routers/twilio_api.py @@ -69,7 +69,7 @@ def twilio_voice_call( """Handle incoming Twilio voice call.""" try: - bot = TwilioVoice.from_data(data) + bot = TwilioVoice.from_webhook_data(data) except BotIntegration.DoesNotExist as e: logger.debug(f"could not find bot integration for {data=} {e=}") resp = VoiceResponse() @@ -198,7 +198,7 @@ def twilio_voice_call_asked( ): """After the initial call, the user has asked a question via Twilio/Gooey ASR. Handle their question.""" - bot = TwilioVoice.from_data(data) + bot = TwilioVoice.from_webhook_data(data) resolve_twilio_tts_voice(bot) background_tasks.add_task(msg_handler, bot)