I’m using Twilio’s REST API to manage a Studio Flow trigger, routing incoming SMS messages to my web application’s webhook. The initial user SMS creates an execution, transitioning the flow from Trigger to a Send and Wait for Reply Widget. This widget successfully sends an SMS to the user.
However, when the user responds, the Send and Wait for Reply Widget doesn’t capture the incoming SMS. Instead of progressing to the final Send Message widget and ending the execution, it remains stuck in an ‘executing’ state, ignoring subsequent user messages. How can I ensure incoming SMS responses are correctly captured by the Send and Wait for Reply Widget to allow the execution to continue?
For additional context, I’ve included an image of my Studio Flow diagram and the webhook endpoint code below:
export async function POST(request: NextRequest) {
try {
// Parse the request body
const formData = await request.formData();
const inboundNumber = formData.get("From") as string;
const twilioNumber = formData.get("To") as string;
const body = formData.get("Body") as string;
console.log("From:", inboundNumber);
console.log("To:");
console.log("Message Body:", body);
// Get the execution list instance
const executionList: ExecutionListInstance =
twilioClient.studio.v2.flows(simpleFlow).executions;
// Fetch all executions (this will get the most recent ones first)
const executions = await executionList.list({ limit: 20 });
// Filter executions manually for active ones with the specific 'to' number
const activeExecutionsForNumber = executions.filter(
(execution) =>
execution.status === "active" &&
execution.contactChannelAddress === inboundNumber
);
// if there is already an active execution going on skip creation of another one
if (activeExecutionsForNumber.length > 0) {
console.log("Active execution already exists for this number");
} else {
// Start a new execution of the Studio Flow
const execution = await executionList.create({
to: inboundNumber,
from: twilioNumber,
parameters: {
incomingMessage: body,
},
});
console.log("New Execution SID:", execution.sid);
}
// Send a 200 OK response without any content
return new NextResponse(null, {
status: 204,
});
} catch (error) {
console.error("Error:", error);
return new NextResponse("Internal Server Error", { status: 500 });
}
}[enter image description here][1]