Calling an API in a Custom Action - What?

So, as was i was going through the documentation, i tried to to exactly as the documentation told me so, but the content of the code, the text and the image are cofusing.

For example:

Start by creating a new javascript file in /data/bots/<your_bot>/actions/getQuote.js . Then copy the following code:

Do i have to copy on the the next snippet of code or all the code that appear in the page?

## Bot Reply

Let’s make the bot reply with the quote:

const eventDestination = {
  channel: event.channel,
  target: event.target,
  botId: event.botId,
  threadId: event.threadId
}

const payloads = await bp.cms.renderElement('builtin_text', { text: quote, typing: true }, eventDestination)

await bp.events.replyToEvent(event, payloads)

Where should i put this? In the getQuote.js file?

What about this?:

// Assigns the quote to the temporary memory
temp.quote = quote

Is is on the getQuote.js file?

And why, when i pay attention to the image i see this sayQuote(args) action? What is the contet of this action? sayQuote.js file?

Also, when i try to acess the api https://opinionated-quotes-api.gigalixirapp.com/v1 directly, i get this error:

Sorry, looks like your app is not running. Try scaling up by running gigalixir ps:scale --replicas=1

I am super new to Botpress and i would like to continue using it. Thanks in advance.

Caio Reis.

In your getQuote.js, you have to copy only the snippet of code and add the rest in the correct place.

If you want to make the bot reply without using the flow editor but to make a response directly from the action, you can put this in the same getQuote.js file. It would look like this :

const axios = require('axios')

const getQuote = async () => {
  // We declare a new axios instance
  const client = axios.create({
    baseURL: 'https://opinionated-quotes-api.gigalixirapp.com/v1'
  })

  // And we get a new quote
  const quote = await client.get('/quotes').then(res => res.data.quotes[0].quote)

  const eventDestination = {
       channel: event.channel,
       target: event.target,
       botId: event.botId,
      threadId: event.threadId
    }

  const payloads = await bp.cms.renderElement('builtin_text', { text: quote, typing: true }, eventDestination)

  await bp.events.replyToEvent(event, payloads)
}
return getQuote()

Yes if you want too, it’s an example on how to store the quote. If you store it like this, you can access it with the flow editor later one like so
image

Here is the corresponding code :

const axios = require('axios')

const getQuote = async () => {
  // We declare a new axios instance
  const client = axios.create({
    baseURL: 'https://opinionated-quotes-api.gigalixirapp.com/v1'
  })

  // And we get a new quote
  const quote = await client.get('/quotes').then(res => res.data.quotes[0].quote)

  temp.quote = quote
}
return getQuote()

I think your right and this a mistake in the documentation. @rndlaine can you correct me if I’m wrong ?

It did not work. Is this API still working? Maybe this is the problem. Thanks in advance

I don’t know about the quote API, to be honest, it was just an example that I did not test to show how the code was supposed to be to answer your previous question.

Hi @caiolindo,

  1. It appears the API is down. We’ll update the documentation to use a reliable API.
  2. You’re right the node name should be “getQuote” not “sayQuote”.
  3. Except for the API part, you should have everything you need. The only thing that you need to change is the api baseURL, the axios query and the cms content of Here's the quote: {{}}.

Let me know how it goes.

2 Likes

Yes, thanks for the reply. I will test it and will reply as soon as i finish testing.

Also please note that there’s a skill to call an API. Take a look at Call API Skill. It uses the Spotify API.

Also please note that there’s a skill to call an API. Take a look at Call API Skill . It uses the Spotify API.

The spotify API that shows in the picture also does not work i think.

I tried to use this skill, but i can’t get to acess the objects quite right. For example:
i have this api https://api.github.com/users/KrunalLathiya, which has the contents

{
  "login": "KrunalLathiya",
  "id": 14403830,
  "node_id": "MDQ6VXNlcjE0NDAzODMw",
  "avatar_url": "https://avatars1.githubusercontent.com/u/14403830?v=4",
  "gravatar_id": "",
  "url": "https://api.github.com/users/KrunalLathiya",
  "html_url": "https://github.com/KrunalLathiya",
  "followers_url": "https://api.github.com/users/KrunalLathiya/followers",
  "following_url": "https://api.github.com/users/KrunalLathiya/following{/other_user}",
  "gists_url": "https://api.github.com/users/KrunalLathiya/gists{/gist_id}",
  "starred_url": "https://api.github.com/users/KrunalLathiya/starred{/owner}{/repo}",
  "subscriptions_url": "https://api.github.com/users/KrunalLathiya/subscriptions",
  "organizations_url": "https://api.github.com/users/KrunalLathiya/orgs",
  "repos_url": "https://api.github.com/users/KrunalLathiya/repos",
  "events_url": "https://api.github.com/users/KrunalLathiya/events{/privacy}",
  "received_events_url": "https://api.github.com/users/KrunalLathiya/received_events",
  "type": "User",
  "site_admin": false,
  "name": "Krunal",
  "company": "Freelancer",
  "blog": "https://appdividend.com",
  "location": "Rajkot",
  "email": null,
  "hireable": true,
  "bio": "Web Developer and Author",
  "public_repos": 116,
  "public_gists": 0,
  "followers": 403,
  "following": 12,
  "created_at": "2015-09-20T19:22:19Z",
  "updated_at": "2019-05-09T16:10:37Z"
}

If i try to create a node with the text “This is the login: {{temp.response.login}}” i get nothing.

Or should i do {{temp.response[0]}}?

Or should i do {{temp.response.login[0]}}?

Sorry for making such basic mistakes, i am super new to Botpress.

(I just saw your comment on the post about Call API Skill. I’ve replied to the original comment)

Here I used the same API as you did and mapped the response to session instead:

image

Notice response.body.

I got it!! Thank you Very much, now i know how to navigate through any api structure.

Now, for the next lesson, i will manipulate an API by inserting and deleting data from it.

Thanks rndlaine!

1 Like