Search code examples
javascriptnode.jsapi-designbotpress

Creating an API that reads text in and uses it


SOLVED (see below)

I'm trying to create an API so that I can read in text (from a word document) and have the bot in botpress respond with a section of that text.

I'm confused about a couple of things:

  1. What structure should my API follow (what file should functions go into and how can I connect them, OR, can I just put in the function in the main app.js file)

  2. How do I call the section I want of that word document so that the bot can respond with it?

As you can see, I can call different elements in the array (doing {{session.response.0}} and the bot will respond with Tony On enter or {{session.response.1}} "Lisa".

I only have one app.js file in my api structure but no other ones. This is my api file (app.js)

    var express =  require("express");
    var fs = require('fs');
    var app = express();
    var port = process.env.PORT || 3002;

    app.get("/url", (req, res, next) =>{
       res.json(["Tony", "Lisa", "Michael","Ginger","Food"]);
    });


    fs.readFile('/home/user/Desktop/test/doc.html', 'utf8', function(err, contents) {
        res.json(contents);
    });


    app.listen(port, () => {
       console.log("Server running on port: " + port);
    });

This is my action file (aka the thing that calls/links the api to botpress):

const axios = require('axios')

/**
 * @title testApi
 * @category Test
 * @author test
 */
const testApi = async () => {
  // We call the test API
  const { data } = await axios.get('http://localhost:3002/url/')

  // We assign the response to the session variable so we can use it later
  session.response = data
}

// Actions are async, so make sure to return a promise
return testApi()

SOLVED EDIT: FOUND A TEXT READER (https://github.com/dbashford/textract)


Solution

  • Used the 'textract' document reader: https://github.com/dbashford/textract