Botpress Cloud: Getting Started

Tips & Tricks

Tips and Tricks

Send an API Request

These are guides on using the Execute Code Card in Botpress to access APIs to read and update data. Following the guide, you'll be able to send emails (using the SendGrid API), and read data from an api (the Bored API, which gives interesting todo suggestions).


The API calls will be performed using the Botpress built-in axios client, and the code will run in the Execute Code Card.

The Axios Library

Botpress comes with a built-in reference to the axios NPM library which allows you to easily do calls to APIs. Whenever you want to call an API, you can just reference axios in the code editor. There is no need to require or import it! For more information about how to use axiosNPM: https://www.npmjs.com/package/axios

The SendGrid Service

We will be using SendGrid as an example of how to send emails. You can choose any other provider, like Gmail, Microsoft, etc. But you will need to change the code based on the provider you want to integrate with. For more information about SendGrid: https://sendgrid.com/

The Bored API

The Bored API's motto is : Let's find you something to do! We will be using Bored API as an example of how to call APIs to fetch information. For more information about Bored API: https://www.boredapi.com/

Getting Started - Update Information

  1. First, we need to create an account: Create a SendGrid account. You can choose Free for trial purposes.
  2. As soon as you land in your newly created account, on the left-hand side menu, there is a menu item named Email API expand it and click on Integration Guide.
  3. Next, you will be prompted to Create an Identity. Enter the needed details and click Create, then you will receive a verification email. Open it and click Verify.
  4. Now, your sender is created and ready to be used. Let's create the API key we will be using when doing our API calls: Create an API Key. On the top right of the screen, click on Create API Key. Create a name for your key - this is irrelevant to Botpress; it is more for your sake to know what this API key is used for.
  5. After the key is created, copy the key value - keep it safe - we are going to use it soon.
  6. Then we go to Botpress, open our bot, and add an Execute Code card. Then in the card, make sure to disable the generative AI by clicking on the button.
  7. Then paste the below code in it after changing the variables [sendgrid_api_key], [from_email], and [to_email].
  • sendgrid_api_key is the API key generated in the Send Grid API Key.
  • from_email is the email configured as a sender.
  • to_email is the email we are sending to.

1
const sendgrid_api_key = *********
2
const from_email = '**************'
3
const to_email = '*****************'
4
5
const response = await axios.post(
6
'https://api.sendgrid.com/v3/mail/send',
7
{
8
personalizations: [
9
{
10
to: [
11
{
12
email: to_email
13
}
14
]
15
}
16
],
17
from: {
18
email: from_email
19
},
20
subject: 'Sending with SendGrid is Fun',
21
content: [
22
{
23
type: 'text/plain',
24
value: 'and easy to do anywhere, even with cURL'
25
}
26
]
27
},
28
{
29
headers: {
30
Authorization: 'Bearer ' + SENDGRID_API_KEY,
31
'Content-Type': 'application/json'
32
}
33
}
34
)

After successfully integrating the SendGrid API into Botpress, it's time to test it.

For more information: https://docs.sendgrid.com/for-developers/sending-email/api-getting-started

Getting Started - Get Information

Go to Botpress, open our bot, and add an Execute Code card. Then in the card, make sure to disable the generative AI by clicking on the [button image] button.

Then paste the below code in it. The code calls Bored Panda API to retrieve information about random activities:

1
const response = await axios.get('https://www.boredapi.com/api/activity?participants=1')

The response will look something like that:

1
{
2
"activity": "Go stargazing",
3
"type": "relaxation",
4
"participants": 1,
5
"price": 0,
6
"link": "",
7
"key": "8832605",
8
"accessibility": 0.1
9
}

To access the information in the response, use response.data.type. You can also pass this information back into your workflow by adding the retrieved information into a workflow variable: workflow.participantType = response.data.type.

Conclusion

Integrating with a system or platform that has APIs may require some technical experience, but it is easy to acquire. Following the steps outlined in this guide, you can easily call APIs and retrieve information to enhance your application.

Use ChatGPT API in Execute Code

If you are using Botpress and want to integrate the ChatGPT API to generate AI-powered responses for your chatbot, follow these simple steps:

Step 1: Create a variable in your Botpress workflow to store the ChatGPT API's response.

Step 2: Add the Execute Code card to your Botpress flow and paste the code below:

1
const apiKey = '<OpenAI's APIKey>';
2
const {data} = await axios.post("https://api.openai.com/v1/chat/completions", {
3
"model": "gpt-3.5-turbo-0301",
4
"messages": [{"role": "user", "content": event.preview}]
5
}, { headers: {'Authorization': `Bearer ${apiKey}`}});
6
7
workflow.variableName = data.choices[0].message.content;

Make sure to replace <OpenAI's APIKey> with your actual OpenAI API key, and variableName with the name of the variable you created in Step 1.

Step 3: Test your chatbot in the Botpress Emulator to see if it's generating AI-powered responses.

Note

It's important to note that using this method, ChatGPT does not preserve the context of the messages that you've sent. However, it's a great way to get started with integrating AI-powered responses into your chatbot without needing any technical knowledge.