> ## Documentation Index
> Fetch the complete documentation index at: https://docs.agnost.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Botpress

> Capture Botpress Studio bot conversations with two hooks

Botpress Studio runs your bot in Botpress Cloud and lets you run code at fixed points of every turn with [Hooks](https://botpress.com/docs/studio/concepts/hooks). Two hooks send each turn to the Agnost AI capture API: one collects the bot's replies, and one sends the user's message, the replies, the latency, and the turn's AI spend when the turn ends. Your Workflows do not change.

Setup takes about five minutes.

## Manual setup: Botpress Studio hooks

### 1. Store your organization ID

Sign in to [app.agnost.ai](https://app.agnost.ai), open **Settings**, and copy the **Organization ID**. It is a UUID.

In Botpress Studio, open **Bot Settings**, scroll to **Configuration Variables**, select **+**, and add a variable named `AGNOST_ORG_ID` with your organization ID as the value.

### 2. Collect the bot's replies

Open **Hooks**, select **Create Hook**, choose **Before Outgoing**, and name it `agnost-collect-replies`. Paste:

```javascript theme={null}
const replies = event.state.session.agnostReplies || []
replies.push(outgoingEvent.preview)
event.state.session.agnostReplies = replies
```

This hook runs before each bot message is sent and keeps the message's plain-text preview in session state.

### 3. Send each turn to Agnost AI

Create another hook, choose **After Turn End**, and name it `agnost-send-turn`. Paste:

```javascript theme={null}
const replies = event.state.session.agnostReplies || []
event.state.session.agnostReplies = []

const agnost = axios.create({
  baseURL: 'https://api.agnost.ai/api/v1',
  headers: { 'x-org-id': env.AGNOST_ORG_ID },
  timeout: 5000,
})
const startedAt = new Date(event.createdOn).getTime()
const latency = Date.now() - startedAt

try {
  await agnost.post('/capture-session', {
    session_id: event.conversationId,
    user_data: { user_id: event.userId },
    client_config: 'botpress',
  })
  await agnost.post('/capture-event', {
    session_id: event.conversationId,
    primitive_name: 'botpress-bot',
    args: event.preview,
    result: replies.join('\n'),
    success: true,
    latency,
    timestamp: startedAt,
    metadata: {
      cost_usd: String((metadata.tokens?.cost ?? 0) / 1e9),
      billed_tokens: String(metadata.tokens?.billedTokens ?? 0),
    },
  })
} catch (error) {
  console.error('Agnost AI capture failed:', error.message)
}
```

After Turn End runs once the bot has received the user's message, processed it, and replied. `axios` and `env` are available in every Studio hook, so no package install is needed. A failed request is logged and never interrupts the conversation.

Change `botpress-bot` to the name you want to see for this bot in Agnost AI. Publish the bot to apply the hooks.

### Data mapping

* `event.conversationId` becomes the Agnost AI conversation ID. Repeating `capture-session` on each turn is safe; Agnost AI keeps one conversation.
* `event.userId` becomes the user ID.
* `event.preview`, the plain text of the user's message, is the event input.
* The previews of all bot messages in the turn, joined with newlines, are the output. Buttons and cards appear as their text preview.
* Latency runs from the user's message to the end of the turn.
* Botpress reports the turn's AI spend in nanodollars. The hook converts it to USD as `cost_usd`, which Agnost AI counts toward the conversation cost, and keeps `billed_tokens` as metadata.

## Why there is no SDK or OpenTelemetry section

Studio code cannot import external packages, so the Agnost AI SDK cannot run inside a hook. The hooks call the [Capture Session](https://docs.agnost.ai/capture-session.md) and [Capture Event](https://docs.agnost.ai/capture-event.md) API directly instead. Botpress Studio bots run in Botpress Cloud, and Studio has no setting to export OpenTelemetry traces to another backend.

### Verify

1. Open the Studio emulator or your published webchat and send two messages in one conversation.
2. Open **Conversations** in [app.agnost.ai](https://app.agnost.ai). The two messages appear as two events in one conversation.
3. Open an event and confirm the input is your message, the output is the bot's full reply, and the metadata includes `cost_usd`.

### Troubleshooting

* **Nothing appears in Agnost AI:** open the bot's logs in Studio and look for `Agnost AI capture failed`. A `400` means `AGNOST_ORG_ID` is missing or not a UUID.
* **Requests succeed but no data appears:** the organization ID is checked after the request is accepted, so a wrong but valid UUID returns `202`. Copy it again from **Settings** in Agnost AI.
* **Outputs are empty:** confirm the `agnost-collect-replies` hook is a **Before Outgoing** hook and that both hooks are published.
* **The output includes the previous turn's replies:** the After Turn End hook failed before it cleared the session state. Check the bot's logs for errors in that hook.

### References

* [Botpress hooks](https://botpress.com/docs/studio/concepts/hooks)
* [Botpress event properties](https://botpress.com/docs/studio/guides/advanced/event-properties)
* [Botpress configuration variables](https://botpress.com/docs/studio/concepts/variables/scopes/configuration)
* [Using code in Studio (available libraries)](https://botpress.com/docs/studio/guides/advanced/use-code)
* [Agnost AI Capture Event API](https://docs.agnost.ai/capture-event.md)

## Next steps

* [Conversations](https://docs.agnost.ai/using-conversations.md): review complete Botpress conversations.
* [Events](https://docs.agnost.ai/using-events.md): inspect each user message and the bot's reply.
* [Patterns](https://docs.agnost.ai/using-patterns.md): organize production conversations by what users wanted.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.