https://discord.gg/botpress logo
#🤝help
Stack-ai API into Botpress
# 🤝help
b

bumpy-boots-2043

07/14/2023, 10:06 AM
Hello, I am looking to add my stack-ai API into BotPress. The bot will read information from the question the user has asked "@question" and stack-ai will feed the answer back to the user in the variable "@apiMessage". I have used the below code in the card 'execute code' however I am receiving an error message when testing my bot. I would greatly appreciate the help, as I've been stuck on this for days. // Add your stack AI endpoint URL in place of "YOUR_STACK_AI_ENDPOINT_URL" const endpoint = "https://www.stack-inference.com/EXAMPLE" // Add your stack AI API key in place of "YOUR_API_KEY_HERE" const headers = { Authorization:
Bearer TOKENEXAMPLE
, "Content-Type": "application/json" } const data = { "in-0": workflow.question, } try { const response = await axios.post(endpoint, data, { headers }) workflow.apiResponse = response.data["out-0"] } catch (error) { throw new Error(
stack-error: ${error}
) }
a

acceptable-kangaroo-64719

07/14/2023, 11:48 AM
use this code:
Copy code
js
// Set these to the names of your stack-AI in/out nodes
var inNode = 'in-0'
var outNode = 'out-0'
// Change this to your Stack-AI URL
let url =
  'https://www.stack-inference.com/run_deployed_flow?flow_id=64b12e0b300d2b8f5f828099&org=8dc59f7b-40a4-41c4-ad11-d5e0f4691e27'

try {
  const response = await axios.post(
    url,
    { [inNode]: `${event.preview}` },
    // Be sure to set your API key as an env variable
    { headers: { Authorization: `Bearer ${env.apiKey}`, 'Content-Type': 'application/json' } }
  )
  workflow.stackAIResponse = response.data[outNode]
} catch (e) {
  console.error(`There was an error calling Stack-AI: ${e}`)
}
Be sure to set an config variable called
apiKey
with your Stack ai API key
b

bumpy-boots-2043

07/14/2023, 1:18 PM
That worked, thank you so much!