| Developers Guide

| Developers Guide

  • Docs
  • API
  • Help
  • Blog

โ€บTutorials

Notes on Versions Released

  • Migrating & Updating
  • Updating to New Releases

Getting Started

  • Introduction
  • Installation
  • Quick Start

Main Concepts

  • Overview
  • Glossary
  • CMS
  • Dialog Engine
  • Actions & Hooks
  • Memory
  • Modules
  • NLU
  • Emulator

Advanced Guides

  • Debug
  • Configuration
  • Authentication Methods
  • Custom Module
  • Performances
  • Hosting
  • Version Control
  • Development Pipelines
  • Clustering

Channels

  • Website Embedding
  • Facebook Messenger
  • Telegram
  • Microsoft Teams
  • Converse API
  • Slack
  • Smooch (Sunshine Conversations)
  • FAQ

Tutorials

  • Deploying a cluster on Digital Ocean
  • How to act on an intent
  • Acting Proactively
  • Using Carousel Postback
  • How to use Slot Skill
  • How to use Call API Skill
  • Calling an API in a Custom Action
  • Shortlinks
  • Connecting your bot with your existing backend
  • Supported databases
  • Timeouts
  • Human in the loop
  • Jump To
  • Inter-bot Communication / Delegation
  • Contextual FAQ
  • Authenticate a user against a 3rd Party OAuth
  • Listening for file changes
  • Using a 3rd party NLU
  • Using the UiPath integration for Botpress

Pro Edition

  • About
  • Licensing
  • Configuring RBAC
  • Monitoring & Alerting
  • Bot Pipelines
  • Internationalization

Calling an API in a Custom Action

Overview

One of the most popular use-case for Actions is to call an API, get some data and use it in your flow. That's what we're going to demonstrate here.

In this tutorial, we're going to fetch the list of Botpress repositories on Github and display the description of the first one. This will show you how to call an API, save the response and use the response in a content element by using templating.

Create an action

We're going to use axios as http client because its already a Botpress dependency. See our Custom Code section to learn more about how dependencies work in Actions.

Start by creating a new javascript file in /data/global/actions/callApi.js. Then copy the following code:

const axios = require('axios')

/**
 * @title Call the Github API to fetch Botpress repositories
 * @category Turorial
 * @author Botpress, Inc.
 */
const callApi = async () => {
  // We call the Github API
  const { data } = await axios.get('https://api.github.com/orgs/botpress/repos')

  // 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 callApi()

๐Ÿ“– What is the session variable? Learn how to use Memory to store data.

Create a node

Select your action

Add an onEnter action instruction and select the callApi action from the list:

Action

๐Ÿ“– What are onEnter instructions? Understand how the node Lifecycle works.

Add a text content element

Create another onEnter instruction to make your bot say something. Once the first instruction is executed, the response will be stored in the session variable.

Here we use templating to get the description of the first repository from the list:

Content

๐Ÿ“– What is this templating thing? Take a look at the Mustache demo to understand how templating works in Botpress.

Once everything is done, this is what your node should looks like:

Node Properties

Try it out!

The next time you chat with the bot, it will reply with the description of the Botpress repository:

Chat

โ† How to use Call API SkillShortlinks โ†’
  • Overview
  • Create an action
  • Create a node
    • Select your action
    • Add a text content element
  • Try it out!
| Developers Guide
Docs
Getting Started (or other categories)Guides (or other categories)API Reference (or other categories)
Community
User ShowcaseStack OverflowProject ChatTwitter
More
BlogGitHubStar
Facebook Open Source
Copyright ยฉ 2021 Botpress Inc.