@botonic/plugin-flow-builder 0.25.0-alpha.9 → 0.25.0-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/lib/cjs/action/index.js +4 -2
  2. package/lib/cjs/action/index.js.map +1 -1
  3. package/lib/cjs/action/knowledge-bases.js +2 -1
  4. package/lib/cjs/action/knowledge-bases.js.map +1 -1
  5. package/lib/cjs/api.d.ts +2 -1
  6. package/lib/cjs/api.js +15 -3
  7. package/lib/cjs/api.js.map +1 -1
  8. package/lib/cjs/constants.d.ts +3 -1
  9. package/lib/cjs/constants.js +4 -2
  10. package/lib/cjs/constants.js.map +1 -1
  11. package/lib/cjs/content-fields/flow-text.d.ts +4 -2
  12. package/lib/cjs/content-fields/flow-text.js +17 -10
  13. package/lib/cjs/content-fields/flow-text.js.map +1 -1
  14. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list-row.d.ts +2 -1
  15. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list-row.js +2 -3
  16. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list-row.js.map +1 -1
  17. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list-section.d.ts +2 -1
  18. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list-section.js +2 -2
  19. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list-section.js.map +1 -1
  20. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list.d.ts +2 -1
  21. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list.js +2 -2
  22. package/lib/cjs/content-fields/whatsapp-button-list/flow-whatsapp-button-list.js.map +1 -1
  23. package/lib/cjs/functions/conditional-bot-variable.d.ts +8 -0
  24. package/lib/cjs/functions/conditional-bot-variable.js +11 -0
  25. package/lib/cjs/functions/conditional-bot-variable.js.map +1 -0
  26. package/lib/cjs/functions/conditional-country.d.ts +7 -0
  27. package/lib/cjs/functions/conditional-country.js +9 -0
  28. package/lib/cjs/functions/conditional-country.js.map +1 -0
  29. package/lib/cjs/functions/conditional-provider.js +1 -3
  30. package/lib/cjs/functions/conditional-provider.js.map +1 -1
  31. package/lib/cjs/functions/index.d.ts +4 -0
  32. package/lib/cjs/functions/index.js +4 -0
  33. package/lib/cjs/functions/index.js.map +1 -1
  34. package/lib/cjs/index.js +1 -1
  35. package/lib/cjs/index.js.map +1 -1
  36. package/lib/cjs/utils.d.ts +2 -0
  37. package/lib/cjs/utils.js +18 -1
  38. package/lib/cjs/utils.js.map +1 -1
  39. package/lib/esm/action/index.js +4 -2
  40. package/lib/esm/action/index.js.map +1 -1
  41. package/lib/esm/action/knowledge-bases.js +2 -1
  42. package/lib/esm/action/knowledge-bases.js.map +1 -1
  43. package/lib/esm/api.d.ts +2 -1
  44. package/lib/esm/api.js +16 -4
  45. package/lib/esm/api.js.map +1 -1
  46. package/lib/esm/constants.d.ts +3 -1
  47. package/lib/esm/constants.js +3 -1
  48. package/lib/esm/constants.js.map +1 -1
  49. package/lib/esm/content-fields/flow-text.d.ts +4 -2
  50. package/lib/esm/content-fields/flow-text.js +18 -11
  51. package/lib/esm/content-fields/flow-text.js.map +1 -1
  52. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list-row.d.ts +2 -1
  53. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list-row.js +2 -3
  54. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list-row.js.map +1 -1
  55. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list-section.d.ts +2 -1
  56. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list-section.js +2 -2
  57. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list-section.js.map +1 -1
  58. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list.d.ts +2 -1
  59. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list.js +2 -2
  60. package/lib/esm/content-fields/whatsapp-button-list/flow-whatsapp-button-list.js.map +1 -1
  61. package/lib/esm/functions/conditional-bot-variable.d.ts +8 -0
  62. package/lib/esm/functions/conditional-bot-variable.js +7 -0
  63. package/lib/esm/functions/conditional-bot-variable.js.map +1 -0
  64. package/lib/esm/functions/conditional-country.d.ts +7 -0
  65. package/lib/esm/functions/conditional-country.js +5 -0
  66. package/lib/esm/functions/conditional-country.js.map +1 -0
  67. package/lib/esm/functions/conditional-provider.js +1 -3
  68. package/lib/esm/functions/conditional-provider.js.map +1 -1
  69. package/lib/esm/functions/index.d.ts +4 -0
  70. package/lib/esm/functions/index.js +4 -0
  71. package/lib/esm/functions/index.js.map +1 -1
  72. package/lib/esm/index.js +1 -1
  73. package/lib/esm/index.js.map +1 -1
  74. package/lib/esm/utils.d.ts +2 -0
  75. package/lib/esm/utils.js +16 -0
  76. package/lib/esm/utils.js.map +1 -1
  77. package/package.json +4 -4
  78. package/src/action/index.tsx +4 -2
  79. package/src/action/knowledge-bases.ts +2 -2
  80. package/src/api.ts +21 -4
  81. package/src/constants.ts +3 -1
  82. package/src/content-fields/flow-text.tsx +23 -17
  83. package/src/content-fields/whatsapp-button-list/flow-whatsapp-button-list-row.tsx +4 -2
  84. package/src/content-fields/whatsapp-button-list/flow-whatsapp-button-list-section.tsx +4 -2
  85. package/src/content-fields/whatsapp-button-list/flow-whatsapp-button-list.tsx +4 -2
  86. package/src/functions/conditional-bot-variable.ts +18 -0
  87. package/src/functions/conditional-country.ts +14 -0
  88. package/src/functions/conditional-provider.ts +1 -2
  89. package/src/functions/index.ts +4 -0
  90. package/src/index.ts +5 -1
  91. package/src/utils.ts +26 -0
@@ -1,8 +1,9 @@
1
- import { Text } from '@botonic/react'
1
+ import { ActionRequest, Text } from '@botonic/react'
2
2
  import React from 'react'
3
3
 
4
4
  import { FlowBuilderApi } from '../api'
5
- import { VARIABLE_REGEX } from '../constants'
5
+ import { ACCESS_TOKEN_VARIABLE_KEY, VARIABLE_PATTERN } from '../constants'
6
+ import { getValueFromKeyPath } from '../utils'
6
7
  import { ContentFieldsBase } from './content-fields-base'
7
8
  import { FlowButton } from './flow-button'
8
9
  import { HtButtonStyle, HtTextNode } from './hubtype-fields'
@@ -21,38 +22,43 @@ export class FlowText extends ContentFieldsBase {
21
22
  const newText = new FlowText(cmsText.id)
22
23
  newText.code = cmsText.code
23
24
  newText.buttonStyle = cmsText.content.buttons_style || HtButtonStyle.BUTTON
24
- newText.text = this.replaceVariables(
25
- this.getTextByLocale(locale, cmsText.content.text),
26
- cmsApi.request.session.user.extra_data
27
- )
25
+ newText.text = this.getTextByLocale(locale, cmsText.content.text)
28
26
  newText.buttons = cmsText.content.buttons.map(button =>
29
27
  FlowButton.fromHubtypeCMS(button, locale, cmsApi)
30
28
  )
31
29
  return newText
32
30
  }
33
31
 
34
- static replaceVariables(
35
- text: string,
36
- extraData?: Record<string, any>
37
- ): string {
38
- const matches = text.match(VARIABLE_REGEX)
32
+ static replaceVariables(text: string, request: ActionRequest): string {
33
+ const matches = text.match(VARIABLE_PATTERN)
39
34
 
40
35
  let replacedText = text
41
- if (matches && extraData) {
36
+ if (matches && request) {
42
37
  matches.forEach(match => {
43
- const variable = match.slice(1, -1)
44
- const value = extraData[variable] ?? match
45
- replacedText = replacedText.replace(match, value)
38
+ const keyPath = match.slice(1, -1)
39
+ const botVariable = keyPath.endsWith(ACCESS_TOKEN_VARIABLE_KEY)
40
+ ? match
41
+ : getValueFromKeyPath(request, keyPath)
42
+ replacedText = replacedText.replace(
43
+ match,
44
+ this.isValidType(botVariable) ? botVariable : match
45
+ )
46
46
  })
47
47
  }
48
48
 
49
49
  return replacedText
50
50
  }
51
51
 
52
- toBotonic(id: string): JSX.Element {
52
+ private static isValidType(botVariable: any): boolean {
53
+ const validTypes = ['boolean', 'string', 'number']
54
+ return validTypes.includes(typeof botVariable)
55
+ }
56
+
57
+ toBotonic(id: string, request: ActionRequest): JSX.Element {
58
+ const replacedText = FlowText.replaceVariables(this.text, request)
53
59
  return (
54
60
  <Text key={id}>
55
- {this.text}
61
+ {replacedText}
56
62
  {this.buttons.map((button, buttonIndex) =>
57
63
  button.renderButton(buttonIndex, this.buttonStyle)
58
64
  )}
@@ -1,5 +1,6 @@
1
1
  import { WhatsappButtonListRowProps } from '@botonic/react'
2
2
 
3
+ import { FlowBuilderApi } from '../../api'
3
4
  import { SOURCE_INFO_SEPARATOR } from '../../constants'
4
5
  import { ContentFieldsBase } from '../content-fields-base'
5
6
  import { HtWhatsappButtonListRow } from '../hubtype-fields'
@@ -11,12 +12,13 @@ export class FlowWhatsappButtonListRow extends ContentFieldsBase {
11
12
 
12
13
  static fromHubtypeCMS(
13
14
  component: HtWhatsappButtonListRow,
14
- locale: string
15
+ locale: string,
16
+ cmsApi: FlowBuilderApi
15
17
  ): FlowWhatsappButtonListRow {
16
18
  const newRow = new FlowWhatsappButtonListRow(component.id)
17
19
  newRow.title = this.getTextByLocale(locale, component.text)
18
20
  newRow.description = this.getTextByLocale(locale, component.description)
19
- newRow.targetId = component.target?.id
21
+ newRow.targetId = cmsApi.getPayload(component.target)
20
22
  return newRow
21
23
  }
22
24
 
@@ -3,6 +3,7 @@ import {
3
3
  WhatsappButtonListSectionProps,
4
4
  } from '@botonic/react'
5
5
 
6
+ import { FlowBuilderApi } from '../../api'
6
7
  import { HtWhatsappButtonListSection } from '../hubtype-fields'
7
8
  import { ContentFieldsBase } from './../content-fields-base'
8
9
  import { FlowWhatsappButtonListRow } from './flow-whatsapp-button-list-row'
@@ -13,12 +14,13 @@ export class FlowWhatsappButtonListSection extends ContentFieldsBase {
13
14
 
14
15
  static fromHubtypeCMS(
15
16
  component: HtWhatsappButtonListSection,
16
- locale: string
17
+ locale: string,
18
+ cmsApi: FlowBuilderApi
17
19
  ): FlowWhatsappButtonListSection {
18
20
  const newButton = new FlowWhatsappButtonListSection(component.id)
19
21
  newButton.title = this.getTextByLocale(locale, component.title)
20
22
  newButton.rows = component.rows.map(row =>
21
- FlowWhatsappButtonListRow.fromHubtypeCMS(row, locale)
23
+ FlowWhatsappButtonListRow.fromHubtypeCMS(row, locale, cmsApi)
22
24
  )
23
25
  return newButton
24
26
  }
@@ -1,6 +1,7 @@
1
1
  import { WhatsappButtonList } from '@botonic/react'
2
2
  import React from 'react'
3
3
 
4
+ import { FlowBuilderApi } from '../../api'
4
5
  import { HtWhatsappButtonListNode } from '../hubtype-fields'
5
6
  import { ContentFieldsBase } from './../content-fields-base'
6
7
  import { FlowWhatsappButtonListSection } from './flow-whatsapp-button-list-section'
@@ -13,7 +14,8 @@ export class FlowWhatsappButtonList extends ContentFieldsBase {
13
14
 
14
15
  static fromHubtypeCMS(
15
16
  component: HtWhatsappButtonListNode,
16
- locale: string
17
+ locale: string,
18
+ cmsApi: FlowBuilderApi
17
19
  ): FlowWhatsappButtonList {
18
20
  const newWhatsappButtonList = new FlowWhatsappButtonList(component.id)
19
21
  newWhatsappButtonList.code = component.code
@@ -26,7 +28,7 @@ export class FlowWhatsappButtonList extends ContentFieldsBase {
26
28
  component.content.button_text
27
29
  )
28
30
  newWhatsappButtonList.sections = component.content.sections.map(section =>
29
- FlowWhatsappButtonListSection.fromHubtypeCMS(section, locale)
31
+ FlowWhatsappButtonListSection.fromHubtypeCMS(section, locale, cmsApi)
30
32
  )
31
33
  return newWhatsappButtonList
32
34
  }
@@ -0,0 +1,18 @@
1
+ import { ActionRequest } from '@botonic/react'
2
+
3
+ import { getValueFromKeyPath } from '../utils'
4
+
5
+ interface ConditionalCountryArgs {
6
+ request: ActionRequest
7
+ results: string[]
8
+ keyPath: string
9
+ }
10
+
11
+ export function conditionalBotVariable({
12
+ request,
13
+ results,
14
+ keyPath,
15
+ }: ConditionalCountryArgs): string {
16
+ const botVariable = getValueFromKeyPath(request, keyPath)
17
+ return results.find(result => result === botVariable) ?? 'default'
18
+ }
@@ -0,0 +1,14 @@
1
+ import { ActionRequest } from '@botonic/react'
2
+
3
+ interface ConditionalCountryArgs {
4
+ request: ActionRequest
5
+ results: string[]
6
+ }
7
+
8
+ export function conditionalCountry({
9
+ request,
10
+ results,
11
+ }: ConditionalCountryArgs): string {
12
+ const country = request.session.user.extra_data.country
13
+ return results.find(result => result === country) || 'default'
14
+ }
@@ -10,6 +10,5 @@ export function conditionalProvider({
10
10
  results,
11
11
  }: ConditionalProviderArgs): string {
12
12
  const provider = request.session.user.provider
13
- if (results.includes(provider)) return provider
14
- return 'default'
13
+ return results.find(result => result === provider) || 'default'
15
14
  }
@@ -1,3 +1,5 @@
1
+ import { conditionalBotVariable } from './conditional-bot-variable'
2
+ import { conditionalCountry } from './conditional-country'
1
3
  import { conditionalProvider } from './conditional-provider'
2
4
  import { conditionalQueueStatus } from './conditional-queue-status'
3
5
 
@@ -5,4 +7,6 @@ export const DEFAULT_FUNCTIONS = {
5
7
  // TODO: Rename api action name
6
8
  'check-queue-status': conditionalQueueStatus,
7
9
  'get-channel-type': conditionalProvider,
10
+ 'check-country': conditionalCountry,
11
+ 'check-bot-variable': conditionalBotVariable,
8
12
  }
package/src/index.ts CHANGED
@@ -153,7 +153,11 @@ export default class BotonicPluginFlowBuilder implements Plugin {
153
153
  case HtNodeWithContentType.VIDEO:
154
154
  return FlowVideo.fromHubtypeCMS(hubtypeContent, locale)
155
155
  case HtNodeWithContentType.WHATSAPP_BUTTON_LIST:
156
- return FlowWhatsappButtonList.fromHubtypeCMS(hubtypeContent, locale)
156
+ return FlowWhatsappButtonList.fromHubtypeCMS(
157
+ hubtypeContent,
158
+ locale,
159
+ this.cmsApi
160
+ )
157
161
  case HtNodeWithContentType.HANDOFF:
158
162
  return FlowHandoff.fromHubtypeCMS(hubtypeContent, locale, this.cmsApi)
159
163
  default:
package/src/utils.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import { Session } from '@botonic/core'
2
+ import { ActionRequest } from '@botonic/react'
2
3
 
3
4
  import { BotonicPluginFlowBuilderOptions, ProcessEnvNodeEnvs } from './types'
4
5
 
@@ -21,3 +22,28 @@ export function resolveGetAccessToken(
21
22
  throw new Error('No method defined for getting access token')
22
23
  }
23
24
  }
25
+
26
+ export function getValueFromKeyPath(
27
+ request: ActionRequest,
28
+ keyPath: string
29
+ ): any {
30
+ if (keyPath.startsWith('input') || keyPath.startsWith('session')) {
31
+ return keyPath
32
+ .split('.')
33
+ .reduce((object, key) => resolveObjectKey(object, key), request)
34
+ }
35
+
36
+ return keyPath
37
+ .split('.')
38
+ .reduce(
39
+ (object, key) => resolveObjectKey(object, key),
40
+ request.session.user.extra_data
41
+ )
42
+ }
43
+
44
+ function resolveObjectKey(object: any, key: string): any {
45
+ if (object && object[key] !== undefined) {
46
+ return object[key]
47
+ }
48
+ return undefined
49
+ }