|
| 1 | +# Example: Bot Flow |
| 2 | + |
| 3 | +Bot flow with NLU intent detection, slot filling, and confirmation. Bot flows use `AskForIntent` and `AskForSlot` for natural language conversations — the Genesys Dialog Engine handles intent matching and entity extraction at runtime. After deploying and publishing, use the `test_bot_flow` MCP tool to simulate a text conversation and verify the NLU behaves as expected. |
| 4 | + |
| 5 | +```typescript |
| 6 | +import type { ArchitectScripting } from "purecloud-flow-scripting-api-sdk-javascript"; |
| 7 | + |
| 8 | +const nluCreationData = { |
| 9 | + nluDomainVersion: { |
| 10 | + language: "en-us", |
| 11 | + intents: [ |
| 12 | + { |
| 13 | + name: "CheckBalance", |
| 14 | + utterances: [ |
| 15 | + { segments: [{ text: "I want to check my balance" }] }, |
| 16 | + { segments: [{ text: "What is my account balance" }] }, |
| 17 | + { segments: [{ text: "How much do I owe" }] }, |
| 18 | + ], |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "MakePayment", |
| 22 | + utterances: [ |
| 23 | + { segments: [{ text: "I want to make a payment" }] }, |
| 24 | + { segments: [{ text: "Pay my bill" }] }, |
| 25 | + { segments: [{ text: "I need to pay" }] }, |
| 26 | + ], |
| 27 | + }, |
| 28 | + ], |
| 29 | + }, |
| 30 | +}; |
| 31 | + |
| 32 | +export async function buildFlow(scripting: ArchitectScripting) { |
| 33 | + const { archFactoryFlows, archFactoryActions, archFactoryTasks } = |
| 34 | + scripting.factories; |
| 35 | + |
| 36 | + const flow = await archFactoryFlows.createFlowBotAsync( |
| 37 | + "Example Bot Flow", |
| 38 | + "Bot flow with NLU intent detection, slot filling, and task routing", |
| 39 | + undefined, |
| 40 | + undefined, |
| 41 | + undefined, |
| 42 | + nluCreationData, |
| 43 | + ); |
| 44 | + |
| 45 | + const initialState = flow.startUpObject; |
| 46 | + |
| 47 | + // Greet the user — bot flows use Communicate with plain string expressions |
| 48 | + const greeting = archFactoryActions.addActionCommunicate( |
| 49 | + initialState, |
| 50 | + "Greeting", |
| 51 | + ); |
| 52 | + greeting.communication.setExpression( |
| 53 | + '"Hello! How can I help you today?"', |
| 54 | + ); |
| 55 | + |
| 56 | + // AskForIntent — the Dialog Engine matches user input to configured intents |
| 57 | + const askIntent = archFactoryActions.addActionAskForIntent( |
| 58 | + initialState, |
| 59 | + "Detect Intent", |
| 60 | + ); |
| 61 | + |
| 62 | + // Create tasks for each intent — then associate them in botFlowSettings |
| 63 | + const balanceTask = archFactoryTasks.addTask(flow, "Check Balance"); |
| 64 | + const balanceReply = archFactoryActions.addActionCommunicate( |
| 65 | + balanceTask, |
| 66 | + "Balance Response", |
| 67 | + ); |
| 68 | + balanceReply.communication.setExpression( |
| 69 | + '"Let me look up your balance. One moment please."', |
| 70 | + ); |
| 71 | + archFactoryActions.addActionExitBotFlow(balanceTask, "Done"); |
| 72 | + |
| 73 | + const paymentTask = archFactoryTasks.addTask(flow, "Make Payment"); |
| 74 | + const paymentReply = archFactoryActions.addActionCommunicate( |
| 75 | + paymentTask, |
| 76 | + "Payment Response", |
| 77 | + ); |
| 78 | + paymentReply.communication.setExpression( |
| 79 | + '"I can help you make a payment. Let me transfer you to an agent."', |
| 80 | + ); |
| 81 | + archFactoryActions.addActionExitBotFlow(paymentTask, "Done"); |
| 82 | + |
| 83 | + // Associate intents with tasks via botFlowSettings |
| 84 | + const balanceIntent = |
| 85 | + flow.botFlowSettings.getIntentSettingsByIntentName("CheckBalance"); |
| 86 | + if (balanceIntent) { |
| 87 | + balanceIntent.confirmation.setExpression( |
| 88 | + '"I think you want to check your balance, is that correct?"', |
| 89 | + ); |
| 90 | + balanceIntent.associateWithTask(balanceTask); |
| 91 | + } |
| 92 | + |
| 93 | + const paymentIntent = |
| 94 | + flow.botFlowSettings.getIntentSettingsByIntentName("MakePayment"); |
| 95 | + if (paymentIntent) { |
| 96 | + paymentIntent.confirmation.setExpression( |
| 97 | + '"You want to make a payment, is that correct?"', |
| 98 | + ); |
| 99 | + paymentIntent.associateWithTask(paymentTask); |
| 100 | + } |
| 101 | + |
| 102 | + // Handle no intent detected |
| 103 | + const noIntentPath = askIntent.outputNoIntent; |
| 104 | + const fallback = archFactoryActions.addActionCommunicate( |
| 105 | + noIntentPath, |
| 106 | + "No Intent", |
| 107 | + ); |
| 108 | + fallback.communication.setExpression( |
| 109 | + '"I\'m sorry, I didn\'t understand that. Could you try rephrasing?"', |
| 110 | + ); |
| 111 | + |
| 112 | + return await flow.publishAsync(); |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +## Testing with `test_bot_flow` |
| 117 | + |
| 118 | +Bot flows must be **published** before testing. The example above uses `publishAsync()` which validates, saves, and publishes in one call. |
| 119 | + |
| 120 | +**Start a test session** with the flow ID returned by `deploy_flow`: |
| 121 | +``` |
| 122 | +Tool: test_bot_flow |
| 123 | +Input: { "flowId": "<flow-id>" } |
| 124 | +``` |
| 125 | + |
| 126 | +The bot responds with its greeting and waits for input. Send a message to trigger intent detection: |
| 127 | +``` |
| 128 | +Tool: test_bot_flow |
| 129 | +Input: { "sessionId": "<session-id>", "message": "I want to check my balance" } |
| 130 | +``` |
| 131 | + |
| 132 | +The response includes: |
| 133 | +- **Text segments** — the bot's reply (greeting, confirmation, slot prompts) |
| 134 | +- **RichMedia segments** — quick reply buttons (e.g. Yes/No for intent confirmation) |
| 135 | +- **`nextActionType`** — `WaitForInput` (send another message), `Disconnect`/`Exit` (conversation ended) |
| 136 | + |
| 137 | +Walk through each conversation path to verify intent detection, slot filling, and task routing work correctly. The Genesys Cloud UI does not expose this testing capability for Bot Flows. |
| 138 | + |
| 139 | +Key differences from Digital Bot Flows: |
| 140 | +- **`createFlowBotAsync`** — creates a bot flow with NLU support via Genesys Dialog Engine |
| 141 | +- **`AskForIntent`** — lets the Dialog Engine match user input to configured intents (vs `DigitalMenu` with explicit choices) |
| 142 | +- **`AskForSlot`** — prompts for and extracts slot values using NLU entity recognition |
| 143 | +- **`botFlowSettings.getIntentSettingsByIntentName()`** — configures confirmation prompts and associates intents with reusable tasks |
| 144 | +- **`settingsPrompts`** — bot flows have prompt settings (not available on digital bot flows) |
0 commit comments