@botonic/core 1.0.0-dev.0 → 1.0.0-dev.2

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 (258) hide show
  1. package/lib/cjs/constants.d.ts +4 -15
  2. package/lib/cjs/constants.js +5 -16
  3. package/lib/cjs/constants.js.map +1 -1
  4. package/lib/cjs/core-bot.d.ts +12 -9
  5. package/lib/cjs/core-bot.js +54 -43
  6. package/lib/cjs/core-bot.js.map +1 -1
  7. package/lib/cjs/debug/index.d.ts +1 -1
  8. package/lib/cjs/debug/index.js +2 -6
  9. package/lib/cjs/debug/index.js.map +1 -1
  10. package/lib/cjs/debug/inspector.d.ts +1 -1
  11. package/lib/cjs/handoff.d.ts +5 -5
  12. package/lib/cjs/handoff.js +13 -10
  13. package/lib/cjs/handoff.js.map +1 -1
  14. package/lib/cjs/hubtype-service.d.ts +4 -4
  15. package/lib/cjs/hubtype-service.js +1 -1
  16. package/lib/cjs/hubtype-service.js.map +1 -1
  17. package/lib/cjs/i18n.d.ts +1 -1
  18. package/lib/cjs/index.d.ts +8 -163
  19. package/lib/cjs/index.js +8 -51
  20. package/lib/cjs/index.js.map +1 -1
  21. package/lib/cjs/models/bot-state.d.ts +11 -0
  22. package/lib/cjs/models/bot-state.js +3 -0
  23. package/lib/cjs/models/bot-state.js.map +1 -0
  24. package/lib/cjs/models/channels.d.ts +9 -0
  25. package/lib/cjs/models/channels.js +14 -0
  26. package/lib/cjs/models/channels.js.map +1 -0
  27. package/lib/cjs/models/events/base-event.d.ts +20 -0
  28. package/lib/cjs/models/events/base-event.js +17 -0
  29. package/lib/cjs/models/events/base-event.js.map +1 -0
  30. package/lib/cjs/models/events/botonic-event.d.ts +4 -0
  31. package/lib/cjs/models/events/botonic-event.js +3 -0
  32. package/lib/cjs/models/events/botonic-event.js.map +1 -0
  33. package/lib/{esm/models/events/connections/index.d.ts → cjs/models/events/connection/connection-event.d.ts} +1 -1
  34. package/lib/cjs/models/events/{connections/index.js → connection/connection-event.js} +1 -1
  35. package/lib/cjs/models/events/connection/connection-event.js.map +1 -0
  36. package/lib/cjs/models/events/connection/index.d.ts +1 -0
  37. package/lib/cjs/models/events/connection/index.js +5 -0
  38. package/lib/cjs/models/events/connection/index.js.map +1 -0
  39. package/lib/cjs/models/events/index.d.ts +4 -21
  40. package/lib/cjs/models/events/index.js +5 -8
  41. package/lib/cjs/models/events/index.js.map +1 -1
  42. package/lib/cjs/models/events/integration/index.d.ts +1 -0
  43. package/lib/cjs/models/events/integration/index.js +5 -0
  44. package/lib/cjs/models/events/integration/index.js.map +1 -0
  45. package/lib/cjs/models/events/integration/integration-event.d.ts +6 -0
  46. package/lib/cjs/models/events/integration/integration-event.js +3 -0
  47. package/lib/cjs/models/events/integration/integration-event.js.map +1 -0
  48. package/lib/cjs/models/events/message/carousel.d.ts +1 -1
  49. package/lib/cjs/models/events/message/custom.d.ts +2 -2
  50. package/lib/cjs/models/events/message/index.d.ts +9 -30
  51. package/lib/cjs/models/events/message/index.js +10 -29
  52. package/lib/cjs/models/events/message/index.js.map +1 -1
  53. package/lib/cjs/models/events/message/location.d.ts +1 -1
  54. package/lib/cjs/models/events/message/media.d.ts +1 -1
  55. package/lib/cjs/models/events/message/media.js +5 -5
  56. package/lib/cjs/models/events/message/media.js.map +1 -1
  57. package/lib/cjs/models/events/message/message-event.d.ts +30 -0
  58. package/lib/cjs/models/events/message/message-event.js +32 -0
  59. package/lib/cjs/models/events/message/message-event.js.map +1 -0
  60. package/lib/cjs/models/events/message/postback.d.ts +1 -1
  61. package/lib/cjs/models/events/message/text.d.ts +1 -1
  62. package/lib/cjs/models/index.d.ts +6 -0
  63. package/lib/cjs/models/index.js +10 -0
  64. package/lib/cjs/models/index.js.map +1 -0
  65. package/lib/cjs/models/legacy-types.d.ts +184 -0
  66. package/lib/cjs/models/legacy-types.js +59 -0
  67. package/lib/cjs/models/legacy-types.js.map +1 -0
  68. package/lib/cjs/models/session.d.ts +3 -0
  69. package/lib/cjs/models/session.js +3 -0
  70. package/lib/cjs/models/session.js.map +1 -0
  71. package/lib/cjs/models/user.d.ts +8 -4
  72. package/lib/cjs/output-parser/botonic-output-parser.d.ts +16 -0
  73. package/lib/cjs/output-parser/botonic-output-parser.js +37 -0
  74. package/lib/cjs/output-parser/botonic-output-parser.js.map +1 -0
  75. package/lib/cjs/output-parser/factory.d.ts +1 -1
  76. package/lib/cjs/output-parser/factory.js +7 -8
  77. package/lib/cjs/output-parser/factory.js.map +1 -1
  78. package/lib/cjs/output-parser/index.d.ts +3 -16
  79. package/lib/cjs/output-parser/index.js +4 -34
  80. package/lib/cjs/output-parser/index.js.map +1 -1
  81. package/lib/cjs/output-parser/parsers.d.ts +3 -9
  82. package/lib/cjs/output-parser/parsers.js +21 -12
  83. package/lib/cjs/output-parser/parsers.js.map +1 -1
  84. package/lib/cjs/plugins.d.ts +2 -3
  85. package/lib/cjs/plugins.js +9 -6
  86. package/lib/cjs/plugins.js.map +1 -1
  87. package/lib/cjs/routing/index.d.ts +2 -0
  88. package/lib/cjs/routing/index.js +6 -0
  89. package/lib/cjs/routing/index.js.map +1 -0
  90. package/lib/cjs/routing/router-utils.d.ts +11 -0
  91. package/lib/cjs/routing/router-utils.js +89 -0
  92. package/lib/cjs/routing/router-utils.js.map +1 -0
  93. package/lib/cjs/routing/router.d.ts +54 -0
  94. package/lib/cjs/routing/router.js +319 -0
  95. package/lib/cjs/routing/router.js.map +1 -0
  96. package/lib/cjs/utils.d.ts +1 -0
  97. package/lib/cjs/utils.js +7 -1
  98. package/lib/cjs/utils.js.map +1 -1
  99. package/lib/esm/constants.d.ts +4 -15
  100. package/lib/esm/constants.js +4 -15
  101. package/lib/esm/constants.js.map +1 -1
  102. package/lib/esm/core-bot.d.ts +12 -9
  103. package/lib/esm/core-bot.js +52 -41
  104. package/lib/esm/core-bot.js.map +1 -1
  105. package/lib/esm/debug/index.d.ts +1 -1
  106. package/lib/esm/debug/index.js +1 -1
  107. package/lib/esm/debug/index.js.map +1 -1
  108. package/lib/esm/debug/inspector.d.ts +1 -1
  109. package/lib/esm/handoff.d.ts +5 -5
  110. package/lib/esm/handoff.js +13 -10
  111. package/lib/esm/handoff.js.map +1 -1
  112. package/lib/esm/hubtype-service.d.ts +4 -4
  113. package/lib/esm/hubtype-service.js +1 -1
  114. package/lib/esm/hubtype-service.js.map +1 -1
  115. package/lib/esm/i18n.d.ts +1 -1
  116. package/lib/esm/index.d.ts +8 -163
  117. package/lib/esm/index.js +8 -45
  118. package/lib/esm/index.js.map +1 -1
  119. package/lib/esm/models/bot-state.d.ts +11 -0
  120. package/lib/esm/models/bot-state.js +2 -0
  121. package/lib/esm/models/bot-state.js.map +1 -0
  122. package/lib/esm/models/channels.d.ts +9 -0
  123. package/lib/esm/models/channels.js +11 -0
  124. package/lib/esm/models/channels.js.map +1 -0
  125. package/lib/esm/models/events/base-event.d.ts +20 -0
  126. package/lib/esm/models/events/base-event.js +14 -0
  127. package/lib/esm/models/events/base-event.js.map +1 -0
  128. package/lib/esm/models/events/botonic-event.d.ts +4 -0
  129. package/lib/esm/models/events/botonic-event.js +2 -0
  130. package/lib/esm/models/events/botonic-event.js.map +1 -0
  131. package/lib/{cjs/models/events/connections/index.d.ts → esm/models/events/connection/connection-event.d.ts} +1 -1
  132. package/lib/esm/models/events/{connections/index.js → connection/connection-event.js} +1 -1
  133. package/lib/esm/models/events/connection/connection-event.js.map +1 -0
  134. package/lib/esm/models/events/connection/index.d.ts +1 -0
  135. package/lib/esm/models/events/connection/index.js +2 -0
  136. package/lib/esm/models/events/connection/index.js.map +1 -0
  137. package/lib/esm/models/events/index.d.ts +4 -21
  138. package/lib/esm/models/events/index.js +4 -7
  139. package/lib/esm/models/events/index.js.map +1 -1
  140. package/lib/esm/models/events/integration/index.d.ts +1 -0
  141. package/lib/esm/models/events/integration/index.js +2 -0
  142. package/lib/esm/models/events/integration/index.js.map +1 -0
  143. package/lib/esm/models/events/integration/integration-event.d.ts +6 -0
  144. package/lib/esm/models/events/integration/integration-event.js +2 -0
  145. package/lib/esm/models/events/integration/integration-event.js.map +1 -0
  146. package/lib/esm/models/events/message/carousel.d.ts +1 -1
  147. package/lib/esm/models/events/message/custom.d.ts +2 -2
  148. package/lib/esm/models/events/message/index.d.ts +9 -30
  149. package/lib/esm/models/events/message/index.js +9 -28
  150. package/lib/esm/models/events/message/index.js.map +1 -1
  151. package/lib/esm/models/events/message/location.d.ts +1 -1
  152. package/lib/esm/models/events/message/media.d.ts +1 -1
  153. package/lib/esm/models/events/message/media.js +1 -1
  154. package/lib/esm/models/events/message/media.js.map +1 -1
  155. package/lib/esm/models/events/message/message-event.d.ts +30 -0
  156. package/lib/esm/models/events/message/message-event.js +29 -0
  157. package/lib/esm/models/events/message/message-event.js.map +1 -0
  158. package/lib/esm/models/events/message/postback.d.ts +1 -1
  159. package/lib/esm/models/events/message/text.d.ts +1 -1
  160. package/lib/esm/models/index.d.ts +6 -0
  161. package/lib/esm/models/index.js +7 -0
  162. package/lib/esm/models/index.js.map +1 -0
  163. package/lib/esm/models/legacy-types.d.ts +184 -0
  164. package/lib/esm/models/legacy-types.js +56 -0
  165. package/lib/esm/models/legacy-types.js.map +1 -0
  166. package/lib/esm/models/session.d.ts +3 -0
  167. package/lib/esm/models/session.js +2 -0
  168. package/lib/esm/models/session.js.map +1 -0
  169. package/lib/esm/models/user.d.ts +8 -4
  170. package/lib/esm/output-parser/botonic-output-parser.d.ts +16 -0
  171. package/lib/esm/output-parser/botonic-output-parser.js +33 -0
  172. package/lib/esm/output-parser/botonic-output-parser.js.map +1 -0
  173. package/lib/esm/output-parser/factory.d.ts +1 -1
  174. package/lib/esm/output-parser/factory.js +1 -2
  175. package/lib/esm/output-parser/factory.js.map +1 -1
  176. package/lib/esm/output-parser/index.d.ts +3 -16
  177. package/lib/esm/output-parser/index.js +3 -32
  178. package/lib/esm/output-parser/index.js.map +1 -1
  179. package/lib/esm/output-parser/parsers.d.ts +3 -9
  180. package/lib/esm/output-parser/parsers.js +14 -7
  181. package/lib/esm/output-parser/parsers.js.map +1 -1
  182. package/lib/esm/plugins.d.ts +2 -3
  183. package/lib/esm/plugins.js +9 -6
  184. package/lib/esm/plugins.js.map +1 -1
  185. package/lib/esm/routing/index.d.ts +2 -0
  186. package/lib/esm/routing/index.js +3 -0
  187. package/lib/esm/routing/index.js.map +1 -0
  188. package/lib/esm/routing/router-utils.d.ts +11 -0
  189. package/lib/esm/routing/router-utils.js +79 -0
  190. package/lib/esm/routing/router-utils.js.map +1 -0
  191. package/lib/esm/routing/router.d.ts +54 -0
  192. package/lib/esm/routing/router.js +315 -0
  193. package/lib/esm/routing/router.js.map +1 -0
  194. package/lib/esm/utils.d.ts +1 -0
  195. package/lib/esm/utils.js +5 -0
  196. package/lib/esm/utils.js.map +1 -1
  197. package/package.json +6 -6
  198. package/src/constants.ts +6 -15
  199. package/src/core-bot.ts +84 -47
  200. package/src/debug/index.ts +1 -6
  201. package/src/debug/inspector.ts +1 -1
  202. package/src/handoff.ts +15 -12
  203. package/src/hubtype-service.ts +5 -5
  204. package/src/i18n.ts +1 -1
  205. package/src/index.ts +8 -230
  206. package/src/models/bot-state.ts +12 -0
  207. package/src/models/channels.ts +9 -0
  208. package/src/models/events/base-event.ts +22 -0
  209. package/src/models/events/botonic-event.ts +26 -0
  210. package/src/models/events/{connections/index.ts → connection/connection-event.ts} +1 -1
  211. package/src/models/events/connection/index.ts +1 -0
  212. package/src/models/events/index.ts +4 -39
  213. package/src/models/events/integration/index.ts +1 -0
  214. package/src/models/events/integration/integration-event.ts +13 -0
  215. package/src/models/events/message/carousel.ts +1 -1
  216. package/src/models/events/message/custom.ts +2 -2
  217. package/src/models/events/message/index.ts +9 -37
  218. package/src/models/events/message/location.ts +1 -1
  219. package/src/models/events/message/media.ts +1 -1
  220. package/src/models/events/message/message-event.ts +37 -0
  221. package/src/models/events/message/postback.ts +1 -1
  222. package/src/models/events/message/text.ts +1 -1
  223. package/src/models/index.ts +6 -0
  224. package/src/models/legacy-types.ts +262 -0
  225. package/src/models/session.ts +3 -0
  226. package/src/models/user.ts +10 -4
  227. package/src/output-parser/botonic-output-parser.ts +38 -0
  228. package/src/output-parser/factory.ts +1 -3
  229. package/src/output-parser/index.ts +3 -38
  230. package/src/output-parser/parsers.ts +29 -17
  231. package/src/plugins.ts +18 -8
  232. package/src/routing/index.ts +2 -0
  233. package/src/routing/router-utils.ts +100 -0
  234. package/src/routing/router.ts +415 -0
  235. package/src/utils.ts +6 -0
  236. package/lib/cjs/errors.d.ts +0 -5
  237. package/lib/cjs/errors.js +0 -11
  238. package/lib/cjs/errors.js.map +0 -1
  239. package/lib/cjs/models/events/connections/index.js.map +0 -1
  240. package/lib/cjs/output-parser/util.d.ts +0 -2
  241. package/lib/cjs/output-parser/util.js +0 -14
  242. package/lib/cjs/output-parser/util.js.map +0 -1
  243. package/lib/cjs/router.d.ts +0 -35
  244. package/lib/cjs/router.js +0 -262
  245. package/lib/cjs/router.js.map +0 -1
  246. package/lib/esm/errors.d.ts +0 -5
  247. package/lib/esm/errors.js +0 -7
  248. package/lib/esm/errors.js.map +0 -1
  249. package/lib/esm/models/events/connections/index.js.map +0 -1
  250. package/lib/esm/output-parser/util.d.ts +0 -2
  251. package/lib/esm/output-parser/util.js +0 -9
  252. package/lib/esm/output-parser/util.js.map +0 -1
  253. package/lib/esm/router.d.ts +0 -35
  254. package/lib/esm/router.js +0 -258
  255. package/lib/esm/router.js.map +0 -1
  256. package/src/errors.ts +0 -11
  257. package/src/output-parser/util.ts +0 -8
  258. package/src/router.ts +0 -316
package/src/core-bot.ts CHANGED
@@ -1,16 +1,23 @@
1
- import { Inspector } from './debug/inspector'
1
+ import { Inspector } from './debug'
2
2
  import { getString } from './i18n'
3
- import { BotRequest, BotResponse, Locales, Routes, Session } from './index'
4
- import { BotonicEvent } from './models/events'
3
+ import {
4
+ BotonicEvent,
5
+ BotRequest,
6
+ BotResponse,
7
+ BotState,
8
+ Locales,
9
+ Route,
10
+ Routes,
11
+ Session,
12
+ } from './models'
5
13
  import { BotonicOutputParser } from './output-parser'
6
14
  import { loadPlugins, runPlugins } from './plugins'
7
- import { Router } from './router'
8
- import { isFunction } from './utils'
15
+ import { getComputedRoutes, Router } from './routing'
9
16
 
10
17
  interface CoreBotConfig {
11
18
  appId?: string
12
19
  defaultDelay?: number
13
- defaultRoutes?: Routes
20
+ defaultRoutes?: Route[]
14
21
  defaultTyping?: number
15
22
  inspector?: Inspector
16
23
  locales: Locales
@@ -22,18 +29,20 @@ interface CoreBotConfig {
22
29
  export class CoreBot {
23
30
  appId?: string
24
31
  defaultDelay?: number
25
- defaultRoutes?: Routes
32
+ defaultRoutes: Route[]
26
33
  defaultTyping?: number
27
- inspector?: Inspector
34
+ inspector: Inspector
28
35
  locales: Locales
29
36
  plugins?: Record<string, Plugin>
30
37
  renderer: any
31
38
  rootElement: any
32
- router: any
39
+ router: Router | null
33
40
  routes: Routes
34
41
  theme?: any
42
+ botonicOutputParser = new BotonicOutputParser()
35
43
 
36
44
  constructor({
45
+ // TODO: Receives dataProvider
37
46
  renderer,
38
47
  routes,
39
48
  locales,
@@ -60,85 +69,113 @@ export class CoreBot {
60
69
  this.inspector = inspector || new Inspector()
61
70
  this.routes = routes
62
71
  this.defaultRoutes = defaultRoutes || []
63
- this.router = isFunction(this.routes)
64
- ? null
65
- : new Router(
66
- // @ts-ignore
67
- [...this.routes, ...this.defaultRoutes],
68
- this.inspector.routeInspector
69
- )
72
+ this.router =
73
+ this.routes instanceof Function
74
+ ? null
75
+ : new Router(
76
+ [...this.routes, ...this.defaultRoutes],
77
+ this.inspector.routeInspector
78
+ )
70
79
  }
71
80
 
72
- getString(id: string, session: Session): string {
73
- // @ts-ignore
74
- return getString(this.locales, session.__locale, id)
81
+ getString(id: string, botState: BotState): string {
82
+ if (!botState.locale) {
83
+ console.error('Locale is not defined')
84
+ return ''
85
+ }
86
+ return getString(this.locales, botState.locale, id)
75
87
  }
76
88
 
77
- setLocale(locale: string, session: Session): void {
78
- session.__locale = locale
89
+ setLocale(locale: string, botState: BotState): void {
90
+ botState.locale = locale
79
91
  }
80
92
 
81
93
  async input({
82
94
  input,
83
95
  session,
84
- lastRoutePath,
96
+ botState,
97
+ dataProvider,
85
98
  }: BotRequest): Promise<BotResponse> {
86
- session = session || {}
87
- if (!session.__locale) session.__locale = 'en'
88
-
99
+ if (!botState?.locale) botState.locale = 'en'
89
100
  if (this.plugins) {
90
- await runPlugins(this.plugins, 'pre', input, session, lastRoutePath)
101
+ await runPlugins(
102
+ this.plugins,
103
+ 'pre',
104
+ input,
105
+ session,
106
+ botState,
107
+ undefined,
108
+ undefined,
109
+ dataProvider
110
+ )
91
111
  }
92
112
 
93
- if (isFunction(this.routes)) {
113
+ if (this.routes instanceof Function) {
94
114
  this.router = new Router(
95
115
  [
96
- // @ts-ignore
97
- ...(await this.routes({ input, session, lastRoutePath })),
98
- // @ts-ignore
116
+ ...(await getComputedRoutes(this.routes, {
117
+ input,
118
+ session,
119
+ botState,
120
+ })),
99
121
  ...this.defaultRoutes,
100
122
  ],
101
- // @ts-ignore
102
123
  this.inspector.routeInspector
103
124
  )
104
125
  }
105
126
 
106
- const output = this.router.processInput(input, session, lastRoutePath)
127
+ const output = (this.router as Router).processInput(
128
+ input,
129
+ session,
130
+ botState
131
+ )
107
132
 
108
133
  const request = {
109
- getString: stringId => this.getString(stringId, session),
110
- setLocale: locale => this.setLocale(locale, session),
134
+ getString: stringId => this.getString(stringId, botState),
135
+ setLocale: locale => this.setLocale(locale, botState),
111
136
  session: session || {},
112
137
  params: output.params || {},
113
138
  input: input,
114
139
  plugins: this.plugins,
115
140
  defaultTyping: this.defaultTyping,
116
141
  defaultDelay: this.defaultDelay,
117
- lastRoutePath,
142
+ botState,
143
+ dataProvider,
118
144
  }
119
145
 
120
- const actions = [output.action, output.retryAction, output.defaultAction]
146
+ const response = await this.renderer({
147
+ request,
148
+ actions: [output.fallbackAction, output.action, output.emptyAction],
149
+ })
150
+
151
+ const messageEvents: Partial<BotonicEvent>[] = this.botonicOutputParser.xmlToMessageEvents(
152
+ response
153
+ )
121
154
 
122
- const response = await this.renderer({ request, actions })
123
- let parsedResponse: Partial<BotonicEvent>[] | null = null
124
- try {
125
- parsedResponse = new BotonicOutputParser().xmlToMessageEvents(response)
126
- } catch (e) {}
155
+ botState.lastRoutePath = output.botState.lastRoutePath // not needed if updated below
127
156
 
128
- lastRoutePath = output.lastRoutePath
129
157
  if (this.plugins) {
130
158
  await runPlugins(
131
159
  this.plugins,
132
160
  'post',
133
161
  input,
134
- session,
135
- lastRoutePath,
162
+ session, // passing output.session instead
163
+ botState, // passing output.botState instead
136
164
  response,
137
- parsedResponse
165
+ messageEvents,
166
+ dataProvider
138
167
  )
139
168
  }
140
169
 
141
- session.is_first_interaction = false
142
- return { input, response, parsedResponse, session, lastRoutePath }
170
+ botState.isFirstInteraction = false
171
+
172
+ return {
173
+ input, // Delete
174
+ response, // xml/rendered react for actions (not needed)
175
+ messageEvents, // xml to Botonic Events
176
+ session, // to be output.session
177
+ botState, // to be output.botState
178
+ dataProvider, // no need to return it
179
+ }
143
180
  }
144
181
  }
@@ -1,6 +1 @@
1
- export {
2
- FocusRouteInspector,
3
- Inspector,
4
- LogRouteInspector,
5
- RouteInspector,
6
- } from './inspector'
1
+ export * from './inspector'
@@ -1,4 +1,4 @@
1
- import { Input, Route, RouteMatcher } from '../index'
1
+ import { Route, RouteMatcher } from '../models'
2
2
  export class RouteInspector {
3
3
  routeMatched(
4
4
  _route: Route,
package/src/handoff.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import axios from 'axios'
2
2
 
3
- import { Session } from './index'
3
+ import { PATH_PAYLOAD_IDENTIFIER } from './constants'
4
+ import { BotState, Session } from './models'
4
5
 
5
6
  const HUBTYPE_API_URL = 'https://api.hubtype.com'
6
7
 
@@ -56,7 +57,7 @@ export async function getOpenQueues(
56
57
  }
57
58
 
58
59
  export class HandOffBuilder {
59
- _session: SessionWithBotonicAction
60
+ _botState: any
60
61
  _queue: string
61
62
  _onFinish: string
62
63
  _email: string
@@ -65,8 +66,8 @@ export class HandOffBuilder {
65
66
  _caseInfo: string
66
67
  _shadowing: boolean
67
68
 
68
- constructor(session: SessionWithBotonicAction) {
69
- this._session = session
69
+ constructor(botState: any) {
70
+ this._botState = botState
70
71
  }
71
72
 
72
73
  withQueue(queueNameOrId: string): this {
@@ -80,7 +81,7 @@ export class HandOffBuilder {
80
81
  }
81
82
 
82
83
  withOnFinishPath(path: string): this {
83
- this._onFinish = `__PATH_PAYLOAD__${path}`
84
+ this._onFinish = `${PATH_PAYLOAD_IDENTIFIER}${path}`
84
85
  return this
85
86
  }
86
87
 
@@ -111,7 +112,7 @@ export class HandOffBuilder {
111
112
 
112
113
  async handOff(): Promise<void> {
113
114
  return _humanHandOff(
114
- this._session,
115
+ this._botState,
115
116
  this._queue,
116
117
  this._onFinish,
117
118
  this._email,
@@ -153,7 +154,7 @@ interface HubtypeHandoffParams {
153
154
  on_finish?: string
154
155
  }
155
156
  async function _humanHandOff(
156
- session: SessionWithBotonicAction,
157
+ botState: any,
157
158
  queueNameOrId = '',
158
159
  onFinish: string,
159
160
  agentEmail = '',
@@ -184,7 +185,8 @@ async function _humanHandOff(
184
185
  if (onFinish) {
185
186
  params.on_finish = onFinish
186
187
  }
187
- session._botonic_action = `create_case:${JSON.stringify(params)}`
188
+ botState.botonicAction = `create_case:${JSON.stringify(params)}`
189
+ botState.isHandoff = true
188
190
  }
189
191
 
190
192
  export async function storeCaseRating(
@@ -255,14 +257,15 @@ export async function getAgentVacationRanges(
255
257
  }
256
258
 
257
259
  export function cancelHandoff(
258
- session: SessionWithBotonicAction,
260
+ botState: BotState,
259
261
  typification: string | null = null
260
262
  ): void {
261
263
  let action = 'discard_case'
262
264
  if (typification) action = `${action}:${JSON.stringify({ typification })}`
263
- session._botonic_action = action
265
+ botState.botonicAction = action
266
+ botState.isHandoff = false // TODO: Review handoff functionalities
264
267
  }
265
268
 
266
- export function deleteUser(session: SessionWithBotonicAction): void {
267
- session._botonic_action = `delete_user`
269
+ export function deleteUser(botState: BotState): void {
270
+ botState.botonicAction = `delete_user`
268
271
  }
@@ -2,7 +2,7 @@ import axios, { AxiosResponse } from 'axios'
2
2
  import Pusher, { AuthOptions, Channel } from 'pusher-js'
3
3
  import Channels from 'pusher-js/types/src/core/channels/channels'
4
4
 
5
- import { Input, SessionUser } from './index'
5
+ import { Input } from './models'
6
6
  import { getWebpackEnvVar } from './utils'
7
7
 
8
8
  interface UnsentInput {
@@ -21,7 +21,7 @@ interface ServerConfig {
21
21
  }
22
22
  interface HubtypeServiceArgs {
23
23
  appId: string
24
- user: SessionUser
24
+ user: any
25
25
  lastMessageId: string
26
26
  lastMessageUpdateDate: string
27
27
  onEvent: any
@@ -50,7 +50,7 @@ const PONG_TIMEOUT = 5 * 1000 // https://pusher.com/docs/channels/using_channels
50
50
  */
51
51
  export class HubtypeService {
52
52
  appId: string
53
- user: SessionUser
53
+ user: any
54
54
  lastMessageId: string
55
55
  lastMessageUpdateDate: string
56
56
  onEvent: any
@@ -185,7 +185,7 @@ export class HubtypeService {
185
185
  }
186
186
 
187
187
  handleConnectionChange(online: boolean): void {
188
- this.onPusherEvent({ action: 'connectionChange', online })
188
+ this.onPusherEvent({ action: 'connection_change', online })
189
189
  }
190
190
 
191
191
  onPusherEvent(event: any): void {
@@ -213,7 +213,7 @@ export class HubtypeService {
213
213
  /**
214
214
  * @return {Promise<void>}
215
215
  */
216
- async postMessage(user: SessionUser, message: any): Promise<void> {
216
+ async postMessage(user: any, message: any): Promise<void> {
217
217
  try {
218
218
  // @ts-ignore
219
219
  await this.init(user)
package/src/i18n.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // eslint-disable-next-line filenames/match-regex
2
- import { Locales } from './index'
2
+ import { Locales } from './models'
3
3
 
4
4
  export const getString = (
5
5
  locales: Locales,
package/src/index.ts CHANGED
@@ -1,233 +1,11 @@
1
- import { BotonicEvent } from './models/events'
2
- export { Providers } from './constants'
3
- export { CoreBot } from './core-bot'
1
+ export * from './constants'
2
+ export * from './core-bot'
4
3
  export * from './debug'
5
4
  export * from './handoff'
6
- export { HubtypeService } from './hubtype-service'
7
- export { getString } from './i18n'
8
- export { Router } from './router'
5
+ export * from './hubtype-service'
6
+ export * from './i18n'
7
+ export * from './models'
8
+ export * from './output-parser'
9
+ export * from './plugins'
10
+ export * from './routing'
9
11
  export * from './utils'
10
-
11
- export type CaseStatusType =
12
- | typeof CASE_STATUS.ATTENDING
13
- | typeof CASE_STATUS.IDLE
14
- | typeof CASE_STATUS.RESOLVED
15
- | typeof CASE_STATUS.WAITING
16
-
17
- export type CaseResolution =
18
- | typeof CASE_RESOLUTION.BANNED
19
- | typeof CASE_RESOLUTION.NOK
20
- | typeof CASE_RESOLUTION.NOT_SOLVED
21
- | typeof CASE_RESOLUTION.OK
22
-
23
- export const PROVIDER = Object.freeze({
24
- DEV: 'dev',
25
- FACEBOOK: 'facebook',
26
- GENERIC: 'generic',
27
- INTERCOM: 'intercom',
28
- SMOOCH: 'smooch',
29
- TELEGRAM: 'telegram',
30
- TWITTER: 'twitter',
31
- WEBCHAT: 'webchat',
32
- WECHAT: 'wechat',
33
- WHATSAPP: 'whatsapp',
34
- })
35
-
36
- export const INPUT = Object.freeze({
37
- TEXT: 'text',
38
- POSTBACK: 'postback',
39
- AUDIO: 'audio',
40
- IMAGE: 'image',
41
- VIDEO: 'video',
42
- DOCUMENT: 'document',
43
- LOCATION: 'location',
44
- CONTACT: 'contact',
45
- BUTTON_MESSAGE: 'buttonmessage',
46
- CAROUSEL: 'carousel',
47
- CUSTOM: 'custom',
48
- WEBCHAT_SETTINGS: 'webchatsettings',
49
- WHATSAPP_TEMPLATE: 'whatsapptemplate',
50
- RAW: 'raw',
51
- })
52
-
53
- export const CASE_STATUS = Object.freeze({
54
- WAITING: 'status_waiting',
55
- ATTENDING: 'status_attending',
56
- IDLE: 'status_idle',
57
- RESOLVED: 'status_resolved',
58
- })
59
-
60
- export const CASE_RESOLUTION = Object.freeze({
61
- OK: 'result_ok',
62
- NOK: 'result_nok',
63
- NOT_SOLVED: 'result_not_solved',
64
- BANNED: 'result_banned',
65
- })
66
-
67
- export interface Locales {
68
- [id: string]: string | string[] | Locales
69
- }
70
-
71
- interface PluginConstructor<T> {
72
- new (arg: T): Plugin
73
- }
74
-
75
- export interface PluginConfig<T> {
76
- id: string
77
- options?: T
78
- resolve: { default: PluginConstructor<T> }
79
- }
80
-
81
- export type InputType =
82
- | typeof INPUT.AUDIO
83
- | typeof INPUT.BUTTON_MESSAGE
84
- | typeof INPUT.CAROUSEL
85
- | typeof INPUT.CONTACT
86
- | typeof INPUT.CUSTOM
87
- | typeof INPUT.DOCUMENT
88
- | typeof INPUT.IMAGE
89
- | typeof INPUT.LOCATION
90
- | typeof INPUT.POSTBACK
91
- | typeof INPUT.TEXT
92
- | typeof INPUT.VIDEO
93
- | typeof INPUT.WEBCHAT_SETTINGS
94
- | typeof INPUT.WHATSAPP_TEMPLATE
95
-
96
- export interface IntentResult {
97
- intent: string
98
- confidence: number
99
- }
100
-
101
- /** Generated by Translation plugins: GoogleTranslation **/
102
- export interface Translations {
103
- [languageCode: string]: string
104
- }
105
-
106
- /** Fields set by NLU plugins: Luis, Dialogflow, ... **/
107
- export interface NluResult {
108
- // the name of the highest confidence intent
109
- confidence: number
110
- intent: string
111
- intents: IntentResult[]
112
- language: string
113
- // entity recognition results in the format provided by the NLU engine
114
- entities?: any
115
- translations: Translations
116
- }
117
-
118
- export interface Input extends Partial<NluResult> {
119
- data?: string
120
- path?: string
121
- payload?: string
122
- type: InputType
123
- }
124
-
125
- export type ProviderType =
126
- | typeof PROVIDER.DEV
127
- | typeof PROVIDER.FACEBOOK
128
- | typeof PROVIDER.GENERIC
129
- | typeof PROVIDER.INTERCOM
130
- | typeof PROVIDER.SMOOCH
131
- | typeof PROVIDER.TELEGRAM
132
- | typeof PROVIDER.TWITTER
133
- | typeof PROVIDER.WEBCHAT
134
- | typeof PROVIDER.WECHAT
135
- | typeof PROVIDER.WHATSAPP
136
-
137
- export interface SessionUser {
138
- id: string
139
- // login
140
- username?: string
141
- // person name
142
- name?: string
143
- // whatsapp, telegram,...
144
- provider: ProviderType
145
- // The provider's user id
146
- extra_data?: any
147
- imp_id?: string
148
- provider_id?: string
149
- }
150
-
151
- // eslint-disable @typescript-eslint/naming-convention
152
- export interface Session {
153
- bot: {
154
- id: string
155
- name?: string
156
- }
157
- __locale?: string
158
- __retries?: number
159
- is_first_interaction: boolean
160
- last_session?: any
161
- organization?: string
162
- user: SessionUser
163
- // after handoff
164
- _hubtype_case_status?: CaseStatusType
165
- _hubtype_case_typification?: string
166
- _shadowing?: boolean
167
- }
168
- // eslint-enable @typescript-eslint/naming-convention
169
-
170
- type InputMatcher = (input: Input) => boolean
171
- type ParamsMatcher =
172
- | { [key: string]: string }
173
- | ((params: { [key: string]: string }) => boolean)
174
- type SessionMatcher = (session: Session) => boolean
175
- type RequestMatcher = (request: BotRequest) => boolean
176
- type StringMatcher = RegExp | string | ((data: string) => boolean)
177
-
178
- export type RouteMatcher =
179
- | InputMatcher
180
- | ParamsMatcher
181
- | RequestMatcher
182
- | SessionMatcher
183
- | StringMatcher
184
-
185
- export interface Route {
186
- action?: any
187
- childRoutes?: Route[]
188
- defaultAction?: any
189
- lastRoutePath?: string
190
- ignoreRetry?: boolean
191
- path?: StringMatcher
192
- redirect?: string
193
- retry?: number
194
- retryAction?: any
195
-
196
- // matchers
197
- input?: InputMatcher
198
- intent?: StringMatcher
199
- params?: ParamsMatcher
200
- payload?: StringMatcher
201
- request?: RequestMatcher
202
- session?: SessionMatcher
203
- text?: StringMatcher
204
- data?: StringMatcher
205
- type?: StringMatcher
206
- }
207
-
208
- export type Routes<R = Route> = R[] | ((_: BotRequest) => R[])
209
-
210
- export interface BotRequest {
211
- input: Input
212
- lastRoutePath: string
213
- session: Session
214
- }
215
-
216
- /** The response of the bot for the triggered actions, which can be
217
- * the one matched by the routes, the default action and the retry actions.
218
- * See Response at @botonic/react's index.d.ts for the React type
219
- * */
220
- export interface BotResponse extends BotRequest {
221
- response: any
222
- parsedResponse: Partial<BotonicEvent>[] | null
223
- }
224
-
225
- export type PluginPreRequest = BotRequest
226
- export type PluginPostRequest = BotResponse
227
-
228
- export interface Plugin {
229
- post(_: PluginPostRequest): void
230
- pre(pluginRequest: PluginPreRequest): void
231
- }
232
-
233
- export type Params = { [key: string]: string }
@@ -0,0 +1,12 @@
1
+ import { RoutePath } from './legacy-types'
2
+
3
+ export interface BotState {
4
+ botId: string
5
+ isFirstInteraction: boolean
6
+ isHandoff: boolean
7
+ isShadowing: boolean
8
+ lastRoutePath: RoutePath
9
+ locale?: string
10
+ retries: number
11
+ botonicAction?: any
12
+ }
@@ -0,0 +1,9 @@
1
+ export enum Channels {
2
+ DEV = 'dev',
3
+ FACEBOOK = 'facebook',
4
+ GENERIC = 'generic',
5
+ TELEGRAM = 'telegram',
6
+ TWITTER = 'twitter',
7
+ WEBCHAT = 'webchat',
8
+ WHATSAPP = 'whatsapp',
9
+ }
@@ -0,0 +1,22 @@
1
+ export enum EventTypes {
2
+ CONNECTION = 'connection',
3
+ MESSAGE = 'message',
4
+ ACK = 'ack',
5
+ TRACK = 'track',
6
+ // Integration Events
7
+ NEW_USER = 'newUser',
8
+ RECEIVED_MESSAGE = 'receivedMessage',
9
+ BOT_EXECUTED = 'botExecuted',
10
+ BOT_ACTION = 'botAction',
11
+ ACTION_SENT = 'actionSent',
12
+ }
13
+
14
+ export interface BaseEvent {
15
+ eventId: string
16
+ userId: string
17
+ eventType: EventTypes
18
+ createdAt: string
19
+ modifiedAt?: string
20
+ channel: string
21
+ idFromChannel: string
22
+ }
@@ -0,0 +1,26 @@
1
+ import { ConnectionEvent } from './connection'
2
+ import { IntegrationEvent } from './integration'
3
+ import {
4
+ AudioMessageEvent,
5
+ CarouselMessageEvent,
6
+ CustomMessageEvent,
7
+ DocumentMessageEvent,
8
+ ImageMessageEvent,
9
+ LocationMessageEvent,
10
+ PostbackMessageEvent,
11
+ TextMessageEvent,
12
+ VideoMessageEvent,
13
+ } from './message'
14
+
15
+ export type BotonicEvent =
16
+ | TextMessageEvent
17
+ | PostbackMessageEvent
18
+ | AudioMessageEvent
19
+ | DocumentMessageEvent
20
+ | ImageMessageEvent
21
+ | VideoMessageEvent
22
+ | LocationMessageEvent
23
+ | CarouselMessageEvent
24
+ | CustomMessageEvent
25
+ | ConnectionEvent
26
+ | IntegrationEvent
@@ -1,4 +1,4 @@
1
- import { BaseEvent, EventTypes } from '..'
1
+ import { BaseEvent, EventTypes } from '../base-event'
2
2
 
3
3
  export enum ConnectionEventStatuses {
4
4
  CONNECTED = 'connected',
@@ -0,0 +1 @@
1
+ export * from './connection-event'