Get Started Developing Bot As Code
1. Install The Botpress CLI
To get started, first install the Botpress CLI globally on your machine. You can do this by running the following command in your terminal:
npm
, but feel free to use the package manager of your choice like yarn
or pnpm
.You can check that your installation was successful by running the following command:
2. Log in to Botpress
Next, log in to your Botpress account using the Botpress CLI. You can do this by running the following command in your terminal:
This command will prompt you for a personal access token. You can create a new token by going to the Dashboard and navigating to the Personal Access Tokens page.
3. Initialize a New Bot
Next, create a new bot using the Botpress CLI. This will set up a new directory with the necessary files and structure for your bot.
4. Install Dependencies and Build
Once the bot is initialized, navigate to the bot directory and install the dependencies:
5. Browse the Bot Files
You can now open the bot in VS Code or another code editor of your choice:
Here’s a brief overview of the files and folders you’ll find in the bot directory:
-
bot.definition.ts
: Contains the bot definition. It exports a data structure that describes your bot—what it can do, and how it can be used. This includes the bot’s actions, events. The definition also allows declaring your bot integration dependencies. Keep reading this documentation to learn about all the concepts you can define in this file. -
src/index.ts
: Contains the bot implementation. It exports a data structure that implements what was defined in the bot definition. Keep reading this documentation to learn about the available callbacks and handlers you can implement. -
.botpress/
: Contains the bot output, generated by the build command. It includes typings to help you implement your bot, and bundled JavaScript used to execute it. You can explore its contents, but avoid importing from nested files or folders, as its structure may change. Everything meant to be imported is available from the root of this folder. -
package.json
,tsconfig.json
: As with any Node.js TypeScript project, these files contain your package’s metadata and dependencies, and the TypeScript configuration. Thetsconfig.json
file is preconfigured to offer the best experience with the Botpress SDK. Runnpm run check:type
to check for typing errors.
6. Install the Chat Integration
The Chat integration allows you to chat with your bot using HTTP requests. See the Chat Integration page for more information.
To install in your bot-as-code, run the following command:
Then navigate to the bot.definition.ts
file and add the following code:
To refresh typings available in your bot implementation, run the following command:
7. Implement a message handlers
Let’s add some content to the bot. Open the src/index.ts
file and add the following code:
This bot will respond to any message sent to it. It will echo back the text message sent by the user. If the user sends a non-text message, like an image, the bot will respond with a message saying it can only respond to text messages.
8. Deploy the Bot
Once you’ve made the necessary changes to your bot, you can deploy it to Botpress Cloud. Run the following command:
You’ll be prompted for some information and confirmations. The --create-new-bot
flag will create a new bot in your workspace. If you want to deploy to an existing bot, you can omit this flag. The CLI will prompt you for the bot in which you want to deploy.
After deploying, go to the Dashboard. You should see your bot listed there.
9. Chat with your bot
Now you can chat with your bot using the CLI. Run the following command:
This command will prompt you for the bot you want to chat with. Select the bot you just deployed, and start chatting with it. Press the escape key to exit the chat.