Skip to content

Commit

Permalink
feat(react): Allow user input preprocess in blockInputs (#2219)
Browse files Browse the repository at this point in the history
* feat(react): allow user input preprocessing before checking the regexp in blockInputs

* style(react): remove BotOptions interface and use core.CoreBotConfig instead
  • Loading branch information
asastre authored Feb 23, 2022
1 parent 5070e99 commit 42c9085
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/botonic-core/src/core-bot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { BotonicOutputParser } from './output-parser'
import { loadPlugins, runPlugins } from './plugins'
import { getComputedRoutes, Router } from './routing'

interface CoreBotConfig {
export interface CoreBotConfig {
appId?: string
defaultDelay?: number
defaultRoutes?: Route[]
Expand Down
6 changes: 5 additions & 1 deletion packages/botonic-react/src/components/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ export interface PersistentMenuProps {
options: any
}

export type BlockInputOption = { match: RegExp[]; message: string }
export type BlockInputOption = {
preprocess?: (message: string) => string
match: RegExp[]
message: string
}

export interface BlobProps {
blobTick?: boolean
Expand Down
6 changes: 1 addition & 5 deletions packages/botonic-react/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ export interface Route extends core.Route {
}
type Routes = core.Routes<Route>

export interface BotOptions extends core.BotOptions {
routes: Routes
}

export class ReactBot extends core.CoreBot {
renderReactActions({
actions,
Expand All @@ -37,7 +33,7 @@ export class ReactBot extends core.CoreBot {
}

export class NodeApp {
constructor(options: Omit<BotOptions, 'renderer'>)
constructor(options: Omit<core.CoreBotConfig, 'renderer'>)
bot: ReactBot
input(request: core.BotRequest): Promise<BotResponse>
renderNode(args): string
Expand Down
6 changes: 5 additions & 1 deletion packages/botonic-react/src/webchat/webchat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,13 @@ export const Webchat = forwardRef((props, ref) => {
)

const getBlockInputs = (rule, inputData) => {
const processedInput = rule.preprocess
? rule.preprocess(inputData)
: inputData

return rule.match.some(regex => {
if (typeof regex === 'string') regex = deserializeRegex(regex)
return regex.test(inputData)
return regex.test(processedInput)
})
}

Expand Down

0 comments on commit 42c9085

Please sign in to comment.