@botonic/core 0.26.0 → 0.27.0-alpha.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 (106) hide show
  1. package/lib/cjs/core-bot.d.ts +1 -3
  2. package/lib/cjs/core-bot.js +3 -33
  3. package/lib/cjs/core-bot.js.map +1 -1
  4. package/lib/cjs/handoff.d.ts +16 -16
  5. package/lib/cjs/handoff.js +9 -2
  6. package/lib/cjs/handoff.js.map +1 -1
  7. package/lib/cjs/index.d.ts +0 -1
  8. package/lib/cjs/index.js +0 -3
  9. package/lib/cjs/index.js.map +1 -1
  10. package/lib/cjs/models/legacy-types.d.ts +44 -39
  11. package/lib/cjs/models/legacy-types.js +15 -32
  12. package/lib/cjs/models/legacy-types.js.map +1 -1
  13. package/lib/cjs/plugins.d.ts +2 -3
  14. package/lib/cjs/plugins.js +2 -4
  15. package/lib/cjs/plugins.js.map +1 -1
  16. package/lib/cjs/utils.d.ts +8 -0
  17. package/lib/cjs/utils.js +32 -1
  18. package/lib/cjs/utils.js.map +1 -1
  19. package/lib/esm/core-bot.d.ts +1 -3
  20. package/lib/esm/core-bot.js +4 -34
  21. package/lib/esm/core-bot.js.map +1 -1
  22. package/lib/esm/handoff.d.ts +16 -16
  23. package/lib/esm/handoff.js +9 -2
  24. package/lib/esm/handoff.js.map +1 -1
  25. package/lib/esm/index.d.ts +0 -1
  26. package/lib/esm/index.js +0 -3
  27. package/lib/esm/index.js.map +1 -1
  28. package/lib/esm/models/legacy-types.d.ts +44 -39
  29. package/lib/esm/models/legacy-types.js +14 -31
  30. package/lib/esm/models/legacy-types.js.map +1 -1
  31. package/lib/esm/plugins.d.ts +2 -3
  32. package/lib/esm/plugins.js +2 -4
  33. package/lib/esm/plugins.js.map +1 -1
  34. package/lib/esm/utils.d.ts +8 -0
  35. package/lib/esm/utils.js +24 -0
  36. package/lib/esm/utils.js.map +1 -1
  37. package/package.json +1 -6
  38. package/src/core-bot.ts +2 -56
  39. package/src/handoff.ts +29 -19
  40. package/src/index.ts +0 -3
  41. package/src/models/legacy-types.ts +78 -76
  42. package/src/plugins.ts +2 -8
  43. package/src/utils.ts +32 -0
  44. package/lib/cjs/data-provider/dynamodb-data-provider.d.ts +0 -26
  45. package/lib/cjs/data-provider/dynamodb-data-provider.js +0 -107
  46. package/lib/cjs/data-provider/dynamodb-data-provider.js.map +0 -1
  47. package/lib/cjs/data-provider/dynamodb-utils.d.ts +0 -11
  48. package/lib/cjs/data-provider/dynamodb-utils.js +0 -121
  49. package/lib/cjs/data-provider/dynamodb-utils.js.map +0 -1
  50. package/lib/cjs/data-provider/factory.d.ts +0 -25
  51. package/lib/cjs/data-provider/factory.js +0 -31
  52. package/lib/cjs/data-provider/factory.js.map +0 -1
  53. package/lib/cjs/data-provider/index.d.ts +0 -4
  54. package/lib/cjs/data-provider/index.js +0 -8
  55. package/lib/cjs/data-provider/index.js.map +0 -1
  56. package/lib/cjs/data-provider/local-data-provider.d.ts +0 -26
  57. package/lib/cjs/data-provider/local-data-provider.js +0 -137
  58. package/lib/cjs/data-provider/local-data-provider.js.map +0 -1
  59. package/lib/cjs/output-parser/botonic-output-parser.d.ts +0 -16
  60. package/lib/cjs/output-parser/botonic-output-parser.js +0 -37
  61. package/lib/cjs/output-parser/botonic-output-parser.js.map +0 -1
  62. package/lib/cjs/output-parser/factory.d.ts +0 -4
  63. package/lib/cjs/output-parser/factory.js +0 -36
  64. package/lib/cjs/output-parser/factory.js.map +0 -1
  65. package/lib/cjs/output-parser/index.d.ts +0 -3
  66. package/lib/cjs/output-parser/index.js +0 -7
  67. package/lib/cjs/output-parser/index.js.map +0 -1
  68. package/lib/cjs/output-parser/parsers.d.ts +0 -26
  69. package/lib/cjs/output-parser/parsers.js +0 -171
  70. package/lib/cjs/output-parser/parsers.js.map +0 -1
  71. package/lib/esm/data-provider/dynamodb-data-provider.d.ts +0 -26
  72. package/lib/esm/data-provider/dynamodb-data-provider.js +0 -103
  73. package/lib/esm/data-provider/dynamodb-data-provider.js.map +0 -1
  74. package/lib/esm/data-provider/dynamodb-utils.d.ts +0 -11
  75. package/lib/esm/data-provider/dynamodb-utils.js +0 -113
  76. package/lib/esm/data-provider/dynamodb-utils.js.map +0 -1
  77. package/lib/esm/data-provider/factory.d.ts +0 -25
  78. package/lib/esm/data-provider/factory.js +0 -27
  79. package/lib/esm/data-provider/factory.js.map +0 -1
  80. package/lib/esm/data-provider/index.d.ts +0 -4
  81. package/lib/esm/data-provider/index.js +0 -5
  82. package/lib/esm/data-provider/index.js.map +0 -1
  83. package/lib/esm/data-provider/local-data-provider.d.ts +0 -26
  84. package/lib/esm/data-provider/local-data-provider.js +0 -133
  85. package/lib/esm/data-provider/local-data-provider.js.map +0 -1
  86. package/lib/esm/output-parser/botonic-output-parser.d.ts +0 -16
  87. package/lib/esm/output-parser/botonic-output-parser.js +0 -33
  88. package/lib/esm/output-parser/botonic-output-parser.js.map +0 -1
  89. package/lib/esm/output-parser/factory.d.ts +0 -4
  90. package/lib/esm/output-parser/factory.js +0 -32
  91. package/lib/esm/output-parser/factory.js.map +0 -1
  92. package/lib/esm/output-parser/index.d.ts +0 -3
  93. package/lib/esm/output-parser/index.js +0 -4
  94. package/lib/esm/output-parser/index.js.map +0 -1
  95. package/lib/esm/output-parser/parsers.d.ts +0 -26
  96. package/lib/esm/output-parser/parsers.js +0 -151
  97. package/lib/esm/output-parser/parsers.js.map +0 -1
  98. package/src/data-provider/dynamodb-data-provider.ts +0 -105
  99. package/src/data-provider/dynamodb-utils.ts +0 -149
  100. package/src/data-provider/factory.ts +0 -60
  101. package/src/data-provider/index.ts +0 -4
  102. package/src/data-provider/local-data-provider.ts +0 -154
  103. package/src/output-parser/botonic-output-parser.ts +0 -38
  104. package/src/output-parser/factory.ts +0 -45
  105. package/src/output-parser/index.ts +0 -3
  106. package/src/output-parser/parsers.ts +0 -237
@@ -1,149 +0,0 @@
1
- import DynamoDB from 'aws-sdk/clients/dynamodb'
2
- import { Entity, Table } from 'dynamodb-toolbox'
3
- import { EntityAttributes } from 'dynamodb-toolbox/dist/classes/Entity'
4
-
5
- import { MessageEventTypes } from '../models'
6
-
7
- // Table Definitions
8
- export const GLOBAL_SECONDARY_INDEX_NAME = 'GSI1'
9
- export const GLOBAL_SECONDARY_INDEX_ATTR = 'websocketId'
10
- export const PARTITION_KEY_NAME = 'PK'
11
- export const SORT_KEY_NAME = 'SK'
12
-
13
- // Entities Definitions
14
- export const USER_PREFIX = 'USR#'
15
- export const EVENT_PREFIX = 'EVT#'
16
-
17
- function capitalize(string: string) {
18
- return string.charAt(0).toUpperCase() + string.slice(1)
19
- }
20
-
21
- export function getUserEventsTable(tableName: string, region: string): Table {
22
- const documentClient = new DynamoDB.DocumentClient({
23
- apiVersion: '2012-08-10',
24
- region: region,
25
- })
26
- return new Table({
27
- name: tableName,
28
- partitionKey: PARTITION_KEY_NAME,
29
- sortKey: SORT_KEY_NAME,
30
- entityField: true,
31
- indexes: {
32
- GSI1: {
33
- partitionKey: GLOBAL_SECONDARY_INDEX_ATTR,
34
- },
35
- },
36
- DocumentClient: documentClient,
37
- })
38
- }
39
-
40
- export function getUserEntity(table: Table): Entity<any> {
41
- return new Entity({
42
- name: 'User',
43
- attributes: {
44
- id: { partitionKey: true, prefix: USER_PREFIX },
45
- [`${SORT_KEY_NAME}`]: {
46
- hidden: true,
47
- sortKey: true,
48
- prefix: USER_PREFIX,
49
- },
50
- userId: [SORT_KEY_NAME, 0],
51
- websocketId: 'string',
52
- isOnline: 'boolean',
53
- route: 'string',
54
- session: 'string',
55
- },
56
- table,
57
- })
58
- }
59
-
60
- export function getConnectionEventEntity(table: Table): Entity<any> {
61
- return new Entity({
62
- name: 'Connection Event',
63
- attributes: {
64
- userId: { partitionKey: true, prefix: USER_PREFIX },
65
- [`${SORT_KEY_NAME}`]: {
66
- hidden: true,
67
- sortKey: true,
68
- prefix: EVENT_PREFIX,
69
- },
70
- eventId: [SORT_KEY_NAME, 0],
71
- eventType: 'string',
72
- createdAt: 'string',
73
- modifiedAt: 'string',
74
- status: 'string',
75
- },
76
- table,
77
- })
78
- }
79
-
80
- export function getMessageEventEntities(
81
- table: Table
82
- ): Record<string, Entity<any>> {
83
- const textAttributes: EntityAttributes = {
84
- text: 'string',
85
- markdown: 'boolean',
86
- buttons: 'list',
87
- replies: 'list',
88
- }
89
-
90
- const mediaAttributes: EntityAttributes = {
91
- src: 'string',
92
- buttons: 'list',
93
- }
94
-
95
- const locationAttributes: EntityAttributes = {
96
- lat: 'number',
97
- long: 'number',
98
- }
99
-
100
- const carouselAttributes: EntityAttributes = {
101
- elements: 'list',
102
- }
103
-
104
- const customAttributes: EntityAttributes = {
105
- json: 'map',
106
- replies: 'list',
107
- }
108
-
109
- const entityWithAttributes = (type: string, attributes: EntityAttributes) =>
110
- new Entity({
111
- name: `${capitalize(type)} Message Event`, // Entity Names must be unique
112
- attributes: {
113
- userId: { partitionKey: true, prefix: USER_PREFIX },
114
- [`${SORT_KEY_NAME}`]: {
115
- hidden: true,
116
- sortKey: true,
117
- prefix: EVENT_PREFIX,
118
- },
119
- eventId: [SORT_KEY_NAME, 0],
120
- eventType: 'string',
121
- createdAt: 'string',
122
- modifiedAt: 'string',
123
- from: 'string',
124
- ack: 'string',
125
- typing: 'number',
126
- delay: 'number',
127
- type: 'string',
128
- ...attributes,
129
- },
130
- table,
131
- })
132
-
133
- return {
134
- text: entityWithAttributes(MessageEventTypes.TEXT, textAttributes),
135
- audio: entityWithAttributes(MessageEventTypes.AUDIO, mediaAttributes),
136
- image: entityWithAttributes(MessageEventTypes.IMAGE, mediaAttributes),
137
- document: entityWithAttributes(MessageEventTypes.DOCUMENT, mediaAttributes),
138
- video: entityWithAttributes(MessageEventTypes.VIDEO, mediaAttributes),
139
- location: entityWithAttributes(
140
- MessageEventTypes.LOCATION,
141
- locationAttributes
142
- ),
143
- carousel: entityWithAttributes(
144
- MessageEventTypes.CAROUSEL,
145
- carouselAttributes
146
- ),
147
- custom: entityWithAttributes(MessageEventTypes.CUSTOM, customAttributes),
148
- }
149
- }
@@ -1,60 +0,0 @@
1
- import { BotonicEvent, User } from '../models'
2
- import { DynamoDBDataProvider } from './dynamodb-data-provider'
3
- import { LocalDevDataProvider } from './local-data-provider'
4
-
5
- export const dataProviderProtocols = {
6
- DYNAMO_DB: 'dynamodb',
7
- FILE: 'file',
8
- }
9
-
10
- export interface DataProvider {
11
- getUsers(limit?: number, offset?: number): User[] | Promise<User[]>
12
- getUser(id: string): User | Promise<User | undefined> | undefined
13
- //TODO: replace getUserByWebsocketId to getUserByField?
14
- getUserByWebsocketId(
15
- websocketId: string
16
- ): User | Promise<User | undefined> | undefined
17
- getUserByField(
18
- field: string,
19
- value: any
20
- ): User | Promise<User | undefined> | undefined
21
- saveUser(user: User): User | Promise<User>
22
- updateUser(user: User): User | Promise<User>
23
- deleteUser(id: string): User | Promise<User | undefined> | undefined
24
- getEvents(
25
- limit?: number,
26
- offset?: number
27
- ): BotonicEvent[] | Promise<BotonicEvent[]>
28
- getEvent(
29
- id: string
30
- ): BotonicEvent | Promise<BotonicEvent | undefined> | undefined
31
- saveEvent(event: BotonicEvent): BotonicEvent | Promise<BotonicEvent>
32
- updateEvent(event: BotonicEvent): BotonicEvent | Promise<BotonicEvent>
33
- deleteEvent(
34
- id: string
35
- ): BotonicEvent | Promise<BotonicEvent | undefined> | undefined
36
- }
37
-
38
- /** URL examples:
39
- * dynamodb://my-table.my-region.aws.com
40
- * postgresql://my-database.my-db-provider.com
41
- * file://path/to/local/db.json
42
- */
43
- export function dataProviderFactory(url?: string): DataProvider {
44
- if (!url) {
45
- return new LocalDevDataProvider()
46
- }
47
-
48
- const protocol = url.split('://')[0]
49
- switch (protocol) {
50
- case dataProviderProtocols.DYNAMO_DB:
51
- return new DynamoDBDataProvider(url)
52
- case dataProviderProtocols.FILE:
53
- return new LocalDevDataProvider(url)
54
- default:
55
- console.error(
56
- `Protocol ${protocol} not implemented, using local DB instead.`
57
- )
58
- return new LocalDevDataProvider()
59
- }
60
- }
@@ -1,4 +0,0 @@
1
- export * from './dynamodb-data-provider'
2
- export * from './dynamodb-utils'
3
- export * from './factory'
4
- export * from './local-data-provider'
@@ -1,154 +0,0 @@
1
- import { JsonDB } from 'node-json-db'
2
- import { Config } from 'node-json-db/dist/lib/JsonDBConfig'
3
-
4
- import { BotonicEvent, User } from '../models'
5
- import { DataProvider } from './factory'
6
-
7
- export class LocalDevDataProvider implements DataProvider {
8
- private readonly DB_PATH = 'tmp/localDb'
9
- private readonly SEPARATOR = '/'
10
- private readonly paths = {
11
- USERS: '/users',
12
- EVENTS: '/events',
13
- CONNECTIONS: '/connections',
14
- }
15
- db: JsonDB
16
-
17
- constructor(url?: string) {
18
- const dbPath = url ? url.split('://')[1] : this.DB_PATH
19
- this.db = new JsonDB(new Config(dbPath, true, false, this.SEPARATOR))
20
- }
21
-
22
- getUsers(limit = 10, offset = 0): User[] {
23
- const path = this.paths.USERS
24
- this.db.reload()
25
- const userList = this.db.getObject<Record<string, User>>(path)
26
- const users = Object.values(userList)
27
- const to = limit !== 0 ? offset + limit : undefined
28
- return users.slice(offset, to)
29
- }
30
-
31
- getUser(userId: string): User | undefined {
32
- const path = this.createPath([this.paths.USERS, userId])
33
- this.db.reload()
34
- return this.db.exists(path) ? this.db.getObject<User>(path) : undefined
35
- }
36
-
37
- getUserByWebsocketId(websocketId: string): User | undefined {
38
- return this.getUserByField('websocketId', websocketId)
39
- }
40
-
41
- getUserByField(field: string, value: any): User | undefined {
42
- const path = this.paths.USERS
43
- this.db.reload()
44
- const dbUser = this.db.exists(path)
45
- ? this.db.find<User>(path, (user: User) => user[field] === value)
46
- : undefined
47
- return dbUser
48
- }
49
-
50
- saveUser(user: User): User {
51
- const path = this.createPath([this.paths.USERS, user.id])
52
- this.db.reload()
53
- if (!this.db.exists(path)) {
54
- this.db.push(path, user)
55
- }
56
- return user
57
- }
58
-
59
- updateUser(user: User): User {
60
- const path = this.createPath([this.paths.USERS, user.id])
61
- this.db.reload()
62
- if (this.db.exists(path)) {
63
- this.db.push(path, user, true)
64
- }
65
- return user
66
- }
67
-
68
- deleteUser(id: string): User | undefined {
69
- const path = this.createPath([this.paths.USERS, id])
70
- this.db.reload()
71
- const user = this.db.exists(path)
72
- ? this.db.getObject<User>(path)
73
- : undefined
74
- if (user) {
75
- this.db.delete(path)
76
- return user
77
- }
78
- return undefined
79
- }
80
-
81
- getEvents(limit = 10, offset = 0): BotonicEvent[] {
82
- const path = this.paths.EVENTS
83
- this.db.reload()
84
- const eventList = this.db.getObject<Record<string, BotonicEvent>>(path)
85
- const events = Object.values(eventList)
86
- const to = limit !== 0 ? offset + limit : undefined
87
- return events.slice(offset, to)
88
- }
89
-
90
- getEvent(id: string): BotonicEvent | undefined {
91
- const path = this.createPath([this.paths.EVENTS, id])
92
- this.db.reload()
93
- return this.db.exists(path)
94
- ? this.db.getObject<BotonicEvent>(path)
95
- : undefined
96
- }
97
-
98
- saveEvent(event: BotonicEvent): BotonicEvent {
99
- const path = this.createPath([this.paths.EVENTS, event.eventId])
100
- this.db.reload()
101
- if (!this.db.exists(path)) {
102
- this.db.push(path, event)
103
- }
104
- return event
105
- }
106
-
107
- updateEvent(event: BotonicEvent): BotonicEvent {
108
- const path = this.createPath([this.paths.EVENTS, event.eventId])
109
- this.db.reload()
110
- if (this.db.exists(path)) {
111
- this.db.push(path, event, true)
112
- }
113
- return event
114
- }
115
-
116
- deleteEvent(id: string): BotonicEvent | undefined {
117
- const path = this.createPath([this.paths.EVENTS, id])
118
- this.db.reload()
119
- const event = this.db.exists(path)
120
- ? this.db.getObject<BotonicEvent>(path)
121
- : undefined
122
- if (event) {
123
- this.db.delete(path)
124
- return event
125
- }
126
- return undefined
127
- }
128
-
129
- addConnection(websocketId: string) {
130
- const path = this.createPath([this.paths.CONNECTIONS, websocketId])
131
- this.db.reload()
132
- if (!this.db.exists(path)) {
133
- this.db.push(path, '', false)
134
- }
135
- }
136
-
137
- updateConnection(websocketId: string, userId: string) {
138
- const path = this.createPath([this.paths.CONNECTIONS, websocketId])
139
- this.db.reload()
140
- if (this.db.exists(path)) {
141
- this.db.push(path, userId, true)
142
- }
143
- }
144
-
145
- deleteConnection(websocketId: string) {
146
- const path = this.createPath([this.paths.CONNECTIONS, websocketId])
147
- this.db.reload()
148
- this.db.delete(path)
149
- }
150
-
151
- private createPath(urlChunks: string[]): string {
152
- return urlChunks.join(this.SEPARATOR)
153
- }
154
- }
@@ -1,38 +0,0 @@
1
- // eslint-disable-next-line import/named
2
- import { parse, X2jOptions } from 'fast-xml-parser'
3
- import { decode } from 'html-entities'
4
-
5
- import { BotonicEvent } from '../models'
6
- import { MessageParsingFactory } from './factory'
7
-
8
- export const TEXT_NODE_NAME = 'text'
9
-
10
- export class BotonicOutputParser {
11
- private static OPTIONS: Partial<X2jOptions> = {
12
- ignoreAttributes: false,
13
- attributeNamePrefix: '',
14
- textNodeName: TEXT_NODE_NAME,
15
- arrayMode: true,
16
- attrValueProcessor: a => decode(a),
17
- }
18
-
19
- constructor(private readonly factory = new MessageParsingFactory()) {}
20
-
21
- xmlToMessageEvents(html: string): Partial<BotonicEvent>[] {
22
- const jsonMessages = this.xmlToJSONMessages(html)
23
- return jsonMessages.map(msgToParse => this.factory.parse(msgToParse))
24
- }
25
-
26
- /**
27
- * Right now, when saving BotonicEvents to DataProvider, we need the standarized Botonic event
28
- * to be saved. This is, converting a botonic input like: '{id: 'msgId', data: 'rawData', payload: 'somePayload'}'
29
- * into a BotonicEvent with the expected properties.
30
- */
31
- parseFromUserInput(input: any): Partial<BotonicEvent> {
32
- return this.factory.parse(input)
33
- }
34
-
35
- private xmlToJSONMessages(html: any): any {
36
- return parse(html, BotonicOutputParser.OPTIONS).message
37
- }
38
- }
@@ -1,45 +0,0 @@
1
- import { BotonicEvent, MEDIA_TYPES, MessageEventTypes } from '../models'
2
- import {
3
- parseButtons,
4
- parseCarousel,
5
- parseContact,
6
- parseCustom,
7
- parseForm,
8
- parseLocation,
9
- parseMedia,
10
- parseMessage,
11
- parseMissed,
12
- parsePostback,
13
- parseReplies,
14
- parseText,
15
- } from './parsers'
16
-
17
- export class MessageParsingFactory {
18
- parse(msgToParse: any): Partial<BotonicEvent> {
19
- const type = msgToParse.type
20
- const parsedMessage = parseMessage({ toParse: msgToParse })
21
- if (MEDIA_TYPES.includes(type)) {
22
- return parseMedia(parseButtons(parsedMessage)).parsed
23
- }
24
- switch (type) {
25
- case MessageEventTypes.TEXT:
26
- // statement 1
27
- return parseText(parseReplies(parseButtons(parsedMessage))).parsed
28
- case MessageEventTypes.POSTBACK:
29
- return parsePostback(parsedMessage).parsed
30
- case MessageEventTypes.LOCATION:
31
- return parseLocation(parsedMessage).parsed
32
- case MessageEventTypes.CAROUSEL:
33
- return parseCarousel(parsedMessage).parsed
34
- case MessageEventTypes.CUSTOM:
35
- return parseCustom(parseReplies(parsedMessage)).parsed
36
- case MessageEventTypes.FORM:
37
- return parseForm(parsedMessage).parsed
38
- case MessageEventTypes.CONTACT:
39
- return parseContact(parsedMessage).parsed
40
- case MessageEventTypes.MISSED:
41
- return parseMissed(parsedMessage).parsed
42
- }
43
- throw new Error(`Parsing for type: '${type}' not implemented.`)
44
- }
45
- }
@@ -1,3 +0,0 @@
1
- export * from './botonic-output-parser'
2
- export * from './factory'
3
- export * from './parsers'
@@ -1,237 +0,0 @@
1
- import {
2
- AudioMessageEvent,
3
- BotonicMessageEvent,
4
- Button,
5
- CarouselElement,
6
- CarouselMessageEvent,
7
- CustomMessageEvent,
8
- DocumentMessageEvent,
9
- EventTypes,
10
- FormMessageEvent,
11
- ImageMessageEvent,
12
- LocationMessageEvent,
13
- MissedMessageEvent,
14
- PostbackMessageEvent,
15
- Reply,
16
- TextMessageEvent,
17
- VideoMessageEvent,
18
- WithButtons,
19
- WithReplies,
20
- } from '../models'
21
- import { ContactMessageEvent } from '../models/events/message/contact'
22
- import { TEXT_NODE_NAME } from './botonic-output-parser'
23
-
24
- export function parseNumber(strNumber: string): number {
25
- return parseInt(strNumber)
26
- }
27
-
28
- export function parseBoolean(strNumber: string): boolean {
29
- if (strNumber === '0') return false
30
- return true
31
- }
32
-
33
- export type ParseFunction<Out> = (args: { toParse: any; parsed?: any }) => {
34
- toParse: any
35
- parsed: Partial<Out>
36
- }
37
-
38
- // COMMON
39
- export const parseMessage: ParseFunction<BotonicMessageEvent> = args => {
40
- const typingAndDelay = {}
41
- if (args.toParse.delay !== undefined) {
42
- typingAndDelay['delay'] = parseNumber(args.toParse.delay)
43
- }
44
- if (args.toParse.typing !== undefined) {
45
- typingAndDelay['typing'] = parseNumber(args.toParse.typing)
46
- }
47
- return {
48
- toParse: args.toParse,
49
- parsed: {
50
- // Following properties added later before saving event: eventId, userId, createdAt, from, ack
51
- eventType: EventTypes.MESSAGE,
52
- type: args.toParse.type,
53
- ack: args.toParse.ack,
54
- from: args.toParse.from,
55
- ...typingAndDelay,
56
- },
57
- }
58
- }
59
-
60
- // BUTTONS
61
- export const parseButton = (button: any): Button => {
62
- const title = button[TEXT_NODE_NAME]
63
- if ('payload' in button) {
64
- return { title, payload: button.payload }
65
- }
66
- if ('url' in button) {
67
- const b = { title, url: button.url, target: button.target }
68
- if (button.target) b.target = button.target
69
- return b
70
- }
71
- if ('webview' in button) {
72
- return {
73
- title,
74
- webview: button.webview,
75
- params: button.params,
76
- }
77
- }
78
- throw new Error('Invalid parsed Button')
79
- }
80
-
81
- export const parseButtons: ParseFunction<WithButtons> = args => {
82
- const hasButtons = args.toParse?.button?.length > 0
83
- let buttons: Button[] = []
84
- if (hasButtons) {
85
- buttons = args.toParse.button.map(button => parseButton(button))
86
- }
87
- return {
88
- toParse: args.toParse,
89
- parsed: {
90
- ...args.parsed,
91
- buttons,
92
- },
93
- }
94
- }
95
-
96
- // REPLIES
97
- export const parseReply = (reply: any): Reply => ({
98
- title: reply[TEXT_NODE_NAME],
99
- payload: reply.payload,
100
- })
101
-
102
- export const parseReplies: ParseFunction<WithReplies> = args => {
103
- const hasReplies = args.toParse?.reply?.length > 0
104
- let replies: Reply[] = []
105
- if (hasReplies) {
106
- replies = args.toParse.reply.map(reply => parseReply(reply))
107
- }
108
- return {
109
- toParse: args.toParse,
110
- parsed: {
111
- ...args.parsed,
112
- replies,
113
- },
114
- }
115
- }
116
-
117
- // TEXT
118
- export const parseText: ParseFunction<TextMessageEvent> = args => {
119
- return {
120
- toParse: args.toParse,
121
- parsed: {
122
- ...args.parsed,
123
- text: args.toParse[TEXT_NODE_NAME],
124
- markdown: parseBoolean(args.toParse.markdown),
125
- },
126
- }
127
- }
128
-
129
- // POSTBACK
130
- export const parsePostback: ParseFunction<PostbackMessageEvent> = args => {
131
- return {
132
- toParse: args.toParse,
133
- parsed: {
134
- ...args.parsed,
135
- payload: args.toParse.payload,
136
- },
137
- }
138
- }
139
-
140
- // MEDIA
141
- export const parseMedia: ParseFunction<
142
- | AudioMessageEvent
143
- | DocumentMessageEvent
144
- | ImageMessageEvent
145
- | VideoMessageEvent
146
- > = args => {
147
- return {
148
- toParse: args.toParse,
149
- parsed: {
150
- ...args.parsed,
151
- src: args.toParse.src,
152
- },
153
- }
154
- }
155
-
156
- // LOCATION
157
- export const parseLocation: ParseFunction<LocationMessageEvent> = args => {
158
- return {
159
- toParse: args.toParse,
160
- parsed: {
161
- ...args.parsed,
162
- lat: args.toParse.lat,
163
- long: args.toParse.long,
164
- },
165
- }
166
- }
167
-
168
- // CAROUSEL
169
- export const parseElement = (element: any): CarouselElement => {
170
- const e: CarouselElement = {
171
- pic: element.pic,
172
- title: element.title,
173
- subtitle: element.desc,
174
- }
175
- const hasButtons = element.button !== undefined
176
- if (hasButtons) e.buttons = element.button.map(b => parseButton(b))
177
- return e
178
- }
179
-
180
- export const parseCarousel: ParseFunction<CarouselMessageEvent> = args => {
181
- return {
182
- toParse: args.toParse,
183
- parsed: {
184
- ...args.parsed,
185
- elements: args.toParse.element.map(element => parseElement(element)),
186
- },
187
- }
188
- }
189
-
190
- // CUSTOM
191
- export const parseCustom: ParseFunction<CustomMessageEvent> = args => {
192
- return {
193
- toParse: args.toParse,
194
- parsed: {
195
- ...args.parsed,
196
- json: JSON.parse(args.toParse.json),
197
- },
198
- }
199
- }
200
-
201
- // MISSED
202
- export const parseMissed: ParseFunction<MissedMessageEvent> = args => {
203
- return {
204
- toParse: args.toParse,
205
- parsed: {
206
- ...args.parsed,
207
- reason: args.toParse.reason,
208
- media_type: args.toParse.media_type,
209
- },
210
- }
211
- }
212
-
213
- // FORM
214
- export const parseForm: ParseFunction<FormMessageEvent> = args => {
215
- return {
216
- toParse: args.toParse,
217
- parsed: {
218
- ...args.parsed,
219
- form_title: args.toParse.form_title,
220
- form_answers: args.toParse.form_answers,
221
- },
222
- }
223
- }
224
-
225
- // CONTACT
226
- export const parseContact: ParseFunction<ContactMessageEvent> = args => {
227
- return {
228
- toParse: args.toParse,
229
- parsed: {
230
- ...args.parsed,
231
- phone_number: args.toParse.phone_number,
232
- first_name: args.toParse.first_name,
233
- last_name: args.toParse.last_name,
234
- vcard: args.toParse.vcard,
235
- },
236
- }
237
- }