Cloud
Getting Started
Tips & Tricks

Tips & Tricks

Tips and Tricks

Using Markdown in Botpress

Markdown is a lightweight markup language that you can use to add formatting elements to plaintext text documents. It is a simple way to add formatting to your text. You can use Markdown in Botpress to format your text in the following ways:

Markdown SyntaxExample
Bold**Bold**
Italic*Italic*
Strikethrough~~Strikethrough~~
Code`Code`
Code Block```Code Block```
Link[Link](https://www.botpress.com)
Image![Image](https://avatars.githubusercontent.com/u/23510677?s=200&v=4)
Unordered List- Item 1
Ordered List1. Item 1
Horizontal line---

Supported Integrations
Markdown is supported only in Webchat Integration

Timely Greeting

Pick an Execute Code card and enter the prompt as "Timely Greeting". It will generate the following code:

const currentTime = luxon.DateTime.now()
const currentHour = currentTime.hour
 
let timelyGreeting = ''
if (currentHour >= 0 && currentHour < 12) {
  timelyGreeting = 'Good morning'
} else if (currentHour >= 12 && currentHour < 18) {
  timelyGreeting = 'Good afternoon'
} else {
  timelyGreeting = 'Good evening'
}
 
workflow.timelyGreeting = timelyGreeting

This code will set the variable timelyGreeting to the appropriate greeting based on the current time. Make sure to create this variable in your workflow.

Using External APIs in Botpress

These are guides on using the Execute Code Card in Botpress to access external APIs to read and update data.

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

Example - The Bored API (opens in a new tab)

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.

  1. (Optional) Create a workflow variable to store the information retrieved from the API. In this example, we will create a variable named participantType.
  2. Pick the Execute Code Card and disable the Generative AI by clicking on the ✨ button.
  3. Paste the following code in the Execute Code Card:
const response = await axios.get('https://www.boredapi.com/api/activity?participants=1')
workflow.participantType = response.data.activity

Response:

    {
      "activity": "Go stargazing",
      "type": "relaxation",
      "participants": 1,
      "price": 0,
      "link": "",
      "key": "8832605",
      "accessibility": 0.1
    }

Send Emails using SendGrid API (opens in a new tab)

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.

  1. Create a free SendGrid account (opens in a new tab): For trial purposes.
  2. Navigate to Email API: After logging into your new account, click on Email API in the menu, then select Integration Guide.
  3. Create an Identity: Provide your details when prompted and click Create. Verify your identity through the verification email you receive.
  4. Create an API Key (opens in a new tab): Click on Create API Key at the top right corner and name the key for your reference.
  5. Save the API Key: After creating the key, copy the key value for later use. Remember to keep it safe.
  6. Configure Botpress: Open your bot in Botpress, and add an Execute Code card. Ensure to disable the generative AI within the card.
  7. Paste the Code: Adjust the following variables in the code and paste it into the card:
    • sendgrid_api_key: This is the API key generated in the SendGrid API Key (opens in a new tab).
    • from_email: This is the email you configured as the sender.
    • to_email: This is the email to which you are sending.

const sendgrid_api_key = *********
const from_email = '**************'
const to_email = '*****************'
 
const response = await axios.post(
  'https://api.sendgrid.com/v3/mail/send',
  {
    personalizations: [{to: [{email: to_email}]}],
    from: {
      email: from_email
    },
    subject: 'Sending with SendGrid is Fun',
    content: [
      {
        type: 'text/plain',
        value: 'and easy to do anywhere, even with cURL'
      }
    ]
  },
  {
    headers: {
      Authorization: 'Bearer ' + SENDGRID_API_KEY,
      'Content-Type': 'application/json'
    }
  }
)

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 (opens in a new tab)

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:

const apiKey = '<OpenAI's APIKey>';
const {data} = await axios.post("https://api.openai.com/v1/chat/completions", {
  "model": "gpt-3.5-turbo-0301",
  "messages": [{"role": "user", "content": event.preview}]
}, { headers: {'Authorization': `Bearer ${apiKey}`}});
 
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.