FlowSubtaskHandler: (
    subtaskId: string,
    previousResponse: TwitterUserAuthFlowResponse,
    credentials: TwitterUserAuthCredentials,
    api: FlowSubtaskHandlerApi,
) => Promise<FlowTokenResult>

A handler function for processing Twitter authentication flow subtasks. Library consumers can implement and register custom handlers for new or existing subtask types using the Scraper.registerAuthSubtaskHandler method.

Each subtask handler is called when its corresponding subtask ID is encountered during the authentication flow. The handler receives the subtask ID, the previous response data, the user's credentials, and an API interface for interacting with the authentication flow.

Handlers should process their specific subtask and return either a successful response or an error. Success responses typically lead to the next subtask in the flow, while errors will halt the authentication process.

Type declaration

import { Scraper, FlowSubtaskHandler } from "@the-convocation/twitter-scraper";

// Custom handler for a hypothetical verification subtask
const verificationHandler: FlowSubtaskHandler = async (
subtaskId,
response,
credentials,
api
) => {
// Extract the verification data from the response
const verificationData = response.subtasks?.[0].exampleData?.value;
if (!verificationData) {
return {
status: 'error',
err: new Error('No verification data found in response')
};
}

// Process the verification data somehow
const result = await processVerification(verificationData);

// Submit the result using the flow API
return await api.sendFlowRequest({
flow_token: api.getFlowToken(),
subtask_inputs: [{
subtask_id: subtaskId,
example_verification: {
value: result,
link: "next_link"
}
}]
});
};

const scraper = new Scraper();
scraper.registerAuthSubtaskHandler("ExampleVerificationSubtask", verificationHandler);

// Later, when logging in...
await scraper.login("username", "password");