Check status in Twilio Flex Conversation

34 Views Asked by At

I am developing a system with Twilio Flex where automated SMS responses are sent to users based on specific triggers. Here's the workflow:

1 - If a user sends an SMS with the phrase "order status", they automatically receive an SMS with their order status.

2 - If a user sends "Please help me", this initiates a conversation in Twilio Flex, and an agent is notified to engage with the customer.

The challenge arises during a live conversation with an agent. If the user sends "order status" while engaged in a conversation, I want to prevent the automated system from sending an automatic reply since the agent is already handling the conversation.

I need to check if the incoming number is currently engaged in a conversation on Twilio Flex. If it is, the automated response should be disabled, and the conversation should continue with the agent uninterrupted.

Below is the sample code I have for checking the status of conversations:

const existingInteractions = await twilioClient.conversations.v1.conversations(ConversationSid).participants.list();

let interactionToCreate = false;

for (const interaction of existingInteractions) {
    if (interaction.messagingBinding && interaction.messagingBinding.address === incomingNumber) {
        const conversationDetails = await twilioClient.conversations.v1.conversations(interaction.conversationSid).fetch();
        if (conversationDetails.state === 'active') {
            console.log("User is actively engaged in Twilio Flex conversation, so no automated SMS should be sent.");
            return;
        }
        if (conversationDetails.state === 'inactive' || conversationDetails.state === 'closed') {
            console.log(`Conversation ${interaction.conversationSid} is inactive or closed. Considering creating a new interaction.`);
            interactionToCreate = true;
            break;
        }
    }
}

This code snippet is just for reference and does not currently meet my requirements. I'm looking for a way to check the engagement status of the incoming number to either allow or prevent the automated SMS response.

How can I achieve this with Twilio's API or Flex settings? Any insights or code examples would be greatly appreciated.

I implemented the following code to check if an incoming number is already engaged in an active conversation on Twilio Flex:

// ... [previous code] ...

for (const interaction of existingInteractions) {
    if (interaction.messagingBinding && interaction.messagingBinding.address === incomingNumber) {
        const conversationDetails = await twilioClient.conversations.v1.conversations(interaction.conversationSid).fetch();
        if (conversationDetails.state === 'active') {
            console.log("User is actively engaged in Twilio Flex conversation, so no automated SMS should be sent.");
            return;
        }
        if (conversationDetails.state === 'inactive' || conversationDetails.state === 'closed') {
            console.log(`Conversation ${interaction.conversationSid} is inactive or closed. Considering creating a new interaction.`);
            interactionToCreate = true;
            break;
        }
    }
}

What I expected:

I expected this code to prevent automated responses ("order status") from being sent if the user is already in an active conversation with an agent on Twilio Flex. Automated responses should be sent only when the user is not engaged in an active conversation.

What actually happened:

The issue I encountered is that when a new number sends an SMS with "help me please," my system recognizes this as an active conversation since it has just been created. This triggers the if (conversationDetails.state === 'active') check, and the code returns before I can create a new conversation on Twilio Flex. Here's the part where I'm supposed to create the conversation:

  const taskFlex = await twilioClient.flexApi.v1.interaction.create({
                channel: {
                    type: 'sms',
                    initiated_by: 'customer',
                    properties: {
                        media_channel_sid: ConversationSid
                    }
                }, routing: {
                    properties: {
                        workspace_sid: workspaceSid,
                        workflow_sid: workflowSid,
                        task_channel_unique_name: 'SMS',
                        attributes: {
                            name: incomingNumber,
                        }
                    }
                }
            }).then(interaction => console.log(interaction.sid));

The problem is that my condition for checking active conversations is too broad and doesn't differentiate between conversations that are just initiated and those that are actively being handled by an agent. As a result, I can't proceed with creating a task in Twilio Flex for new "help me please" messages because the code incorrectly assumes that all active conversations should not receive automated responses.

What I need help with:

How can I modify my code to differentiate between a newly initiated conversation that should result in a task being created in Twilio Flex, and an ongoing conversation where automated SMS responses should be disabled? I need to make sure that the automated system only sends SMS responses when the user is not actively engaged with an agent. Any advice on how to refine my condition checks or use a different approach would be greatly appreciated.

0

There are 0 best solutions below