Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

106
Views
Problema para llamar a una API dentro de un Webhook - Javascript

Estoy creando un chatbot usando un Webhook en Javascript y en un momento necesito conectarme a mi API (usando un POST) para obtener algunos datos y enviar la información recibida dentro de un mensaje al usuario. El problema aquí es que no se ha llamado a la API y el flujo simplemente pasa por alto mi conexión (ni siquiera veo la conexión en el servidor API).

Ejecuté algunos testículos con la API del Webhook y funcionó. Intenté usar la función asíncrona y esperar , pero la aplicación continúa "ignorando" mi llamada a la API.

A continuación se muestra mi código con mis pruebas. He realizado algunos cambios en la URL solo por motivos de seguridad, pero la lógica es la misma.

 var price = 0 var payload = {'product_id': 'n/a'} **//Creating this global variable just for testing** //CREATING THE FUNCTION TO CONNECT TO MY API USING AXIOS function my_api(payload){ app.get("/", async(request, response) => { const {data} = await axios.post('https://www.test.com/my_api', payload) price = data.price }); } //FILLING MY TEST PAYLOAD payload = {'product_id':10} //CALLING THE API OUT OF THE WEBHOOK AND PRINTING THE RETURN ON MY WEBSITE CONSOLE my_api(payload) **//If I call the API here, the application is accessed by the function. I confirmed that writing a response.send(price) command in the API function and check my API Server log** //STARTING MY WEBHOOK app.post("/webhook", async function (request, response) { const agent = new WebhookClient({ request: request, response: response }); //THE FUNCTION WHERE THE API IS CALLED AFTER THE USER'S INPUT IN THE LAST INTENT AND I NEED TO WAIT IT RETURN WITH THE RESULT TO SEND THE MESSAGE async function product_price(agent){ user_input = agent.query var payload = {'product_id': user_input} await my_api(payload) **//When I call the API function here it don't connect to my application** agent.add('The price for this product is '+price) } }

¿Tiene alguna idea de cómo puedo conectarme a la API en el Webhook?

Intenté crear una función fuera del Webhook y llamar a la función my_api (carga útil) dentro de él, pero no funcionó.

Gracias por su atención.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Tiene un punto final de descanso /webhook y en su controlador crea una función product_price pero nunca la ejecuta. Para solucionarlo, solo tiene que llamarlo como product_price() pero en su código no es necesario crear esta función en absoluto.

 //STARTING MY WEBHOOK app.post("/webhook", async function (request, response) { const agent = new WebhookClient({ request: request, response: response }); const user_input = agent.query const payload = {'product_id': user_input} await my_api(payload) agent.add('The price for this product is '+price) }

Su código tiene algunas cosas raras, que también deberían mejorarse: en my_api crea un punto final de descanso para / pero ¿por qué? Supongo que solo quieres hacer una solicitud para obtener el precio.

 async function my_api(payload){ const {data} = await axios.post('https://www.test.com/my_api', payload) price = data.price }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!