Long running external API call integration
# 🤝help
a
Hi Botpress team! I have an issue I've been struggling with for some time and definitely you could shed some light. My bot needs to API-call an external system which works in a queue based pattern: 1. I send a request to it and it returns me a job id 2. Aftwerwards, I can either poll for the job completion status through one (or many) follow-up call or wait for a callback to move on Considerations: - These jobs usually take 3 to 5 minutes to complete - I am using the WhatsApp integration as the main delivery channel to users What would be the best way to implement this in botpress? After fiddling with webhook triggers, I could not connect the callback call with the WhatsApp conversation (although it worked for webchat). Polling the service in an execute code card led me to a timeout after 60 seconds and the bot stopped responding...
I replicated this question in the Office Hours session earlier today, and Patrick suggested me to try creating several Execute Card statements to do the polling every 40 seconds or so. I implemented it and was not successful - after 60 seconds, the bot just stops responding and does not get back to the user and do not throw an error... I am attaching the implementation. Did I possibly find a bug here? Execute code cards:
Copy code
// Auxiliary functions and constants
const POLLING_INTERVAL_MS = 40000
const TIMEOUT_INTERVAL_SECONDS = 180
const wait = (ms = 1000) => {
  return new Promise((resolve) => {
    setTimeout(resolve, ms)
  })
}

// Initialize query params
const query = `
  query IngestAdhocContentStandalonePublication($id: uuid!) {
    ingestAdhocContentStandalonePublication(id: $id) {
      // --- redacted ---
    }
  }`
const variables = { id: workflow.ingestionId }
const config = {
  headers: {
    'Content-Type': 'application/json',
    'x-api-key': env.backend_gql_api_key
  }
}

if (workflow.ingestionStatus !== 'success' && workflow.ingestionStatus !== 'failed') {
  // Poll ingestion processing job
  const { data } = await axios.post(env.backend_gql_api_endpoint, { query, variables }, config)
  if (data.data?.errors?.length > 0 || data.data?.ingestAdhocContentStandalonePublication?.output?.postId) {
    if (data.data.errors?.length > 0) {
      // Flag failed ingestion
      workflow.ingestionStatus = 'failed'
    } else {
      // Store results in workflow variables
      const { postId, title, publisher, description } = data.data.ingestAdhocContentStandalonePublication.output
      workflow.postId = postId
      workflow.title = title
      workflow.publisher = publisher
      workflow.description = description
      workflow.ingestionStatus = 'success'
    }
  } else {
    await wait(POLLING_INTERVAL_MS)
  }
}
This topic is being discussed in the feature request channel. Please upvote if you feel it is relevant. https://discord.com/channels/1108396290624213082/1215742095625687060
7 Views