|
| 1 | +import { EntityResolutionContext |
| 2 | + , EntityEventHandler |
| 3 | + , EntityEventHandlers |
| 4 | + , EntityEventHandlerMetadata |
| 5 | + , EntityBaseEvent |
| 6 | + , EntityPublishMessageEvent |
| 7 | +} from '@oracle/bots-node-sdk/lib'; |
| 8 | + |
| 9 | +// You can use your favorite http client package to make REST calls, however, the node fetch API is pre-installed with the bots-node-sdk. |
| 10 | +// Documentation can be found at https://www.npmjs.com/package/node-fetch |
| 11 | +// Un-comment the next line if you want to make REST calls using node-fetch. |
| 12 | +// import fetch from 'node-fetch'; |
| 13 | + |
| 14 | +export class {{className}} implements EntityEventHandler { |
| 15 | + |
| 16 | + public metadata(): EntityEventHandlerMetadata { |
| 17 | + return { |
| 18 | + name: '{{name}}', |
| 19 | + eventHandlerType: '{{eventHandlerType}}', |
| 20 | + supportedActions: [] // string array of transition actions that might be set by the event handler |
| 21 | + }; |
| 22 | + } |
| 23 | + |
| 24 | + public handlers(): EntityEventHandlers { |
| 25 | + return { |
| 26 | + |
| 27 | + entity: { |
| 28 | + /** |
| 29 | + * Default message handler that includes acknowledgements when a bag item is updated |
| 30 | + * or a bag item value is provided while the user was prompted for another item |
| 31 | + */ |
| 32 | + publishMessage: async (event: EntityPublishMessageEvent, context: EntityResolutionContext) => { |
| 33 | + updatedItemsMessage(context); |
| 34 | + outOfOrderItemsMessage(context); |
| 35 | + context.addCandidateMessages(); |
| 36 | + }, |
| 37 | + |
| 38 | + /** |
| 39 | + * This handler is called when the composite bag entity is resolved |
| 40 | + */ |
| 41 | + resolved: async (event: EntityBaseEvent, context: EntityResolutionContext) => { // eslint-disable-line no-unused-vars |
| 42 | + // add your back-end REST API call here |
| 43 | + } |
| 44 | + // add more entity level event handlers here |
| 45 | + }, |
| 46 | + |
| 47 | + items: { |
| 48 | + SomeBagItemName: { // TODO change to a valid bag item name |
| 49 | + // add item level event handlers here |
| 50 | + } |
| 51 | + // add more bag items and their handlers here |
| 52 | + }, |
| 53 | + |
| 54 | + custom: { |
| 55 | + // add custom event handlers here |
| 56 | + } |
| 57 | + |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Helper function to show acknowledgement message when a bag item value is updated. |
| 65 | + */ |
| 66 | +function updatedItemsMessage(context: EntityResolutionContext) { |
| 67 | + if (context.getItemsUpdated().length > 0) { |
| 68 | + let message = "I have updated" + context.getItemsUpdated().map((item, i) => (i !== 0 ? " and the " : " the ") + item.toLowerCase() + " to " + context.getDisplayValue(item)); |
| 69 | + context.addMessage(message); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Helper function to show acknowledgement message when a bag item value is provided when user was prompted for anther bag item. |
| 75 | + */ |
| 76 | +function outOfOrderItemsMessage(context: EntityResolutionContext) { |
| 77 | + if (context.getItemsMatchedOutOfOrder().length > 0) { |
| 78 | + let message = "I got" + context.getItemsMatchedOutOfOrder().map((item, i) => (i !== 0 ? " and the " : " the ") + item.toLowerCase() + " " + context.getDisplayValue(item)); |
| 79 | + context.addMessage(message); |
| 80 | + } |
| 81 | +} |
0 commit comments