1 | # Mainframe JavaScript SDK for bots
|
2 |
|
3 | Work in progress
|
4 |
|
5 | ## Installation
|
6 |
|
7 | ```sh
|
8 | npm install @mainframe/bot-sdk # npm
|
9 | yarn add @mainframe/bot-sdk # Yarn
|
10 | ```
|
11 |
|
12 | ## Example
|
13 |
|
14 | ```js
|
15 | const { startServer } = require('@mainframe/bot-sdk')
|
16 |
|
17 | startServer(
|
18 | {
|
19 | conversation_added({ conversation_id }, { sendMessage }) {
|
20 | sendMessage({ conversation_id, message: 'Hello world' })
|
21 | },
|
22 | },
|
23 | {
|
24 | mainframe_secret: '<my bot secret>',
|
25 | port: 3000,
|
26 | },
|
27 | )
|
28 | ```
|
29 |
|
30 | ## Types
|
31 |
|
32 | The SDK uses the following [Flow types](https://flow.org/en/docs/types/):
|
33 |
|
34 | ```js
|
35 | type PartialConfig = {
|
36 | +mainframe_secret: string,
|
37 | +mainframe_url: string,
|
38 | +port: number,
|
39 | }
|
40 |
|
41 | type Config = {|
|
42 | +mainframe_secret: string,
|
43 | +mainframe_url: string,
|
44 | +port: number,
|
45 | |}
|
46 |
|
47 | type ClientContext = {|
|
48 | +user: {
|
49 | +id: string,
|
50 | +username: string,
|
51 | +name: string,
|
52 | },
|
53 | +conversation?: {
|
54 | +id: string,
|
55 | +subject: ?string,
|
56 | +type: 'bot' | 'direct' | 'default' | 'space',
|
57 | },
|
58 | +organization?: {
|
59 | +id: string,
|
60 | +username: string,
|
61 | +name: string,
|
62 | },
|
63 | +subscription_token?: string,
|
64 | |}
|
65 |
|
66 | type PostPayload = {|
|
67 | +user_id: string,
|
68 | +data: Object,
|
69 | +context: ClientContext,
|
70 | |}
|
71 |
|
72 | type BotContext = {|
|
73 | +config: Config,
|
74 | +callMainframe: (endpoint: string, data?: Object) => Promise<void | Object>,
|
75 | +sendEvent: (payload: {
|
76 | conversation_id: string,
|
77 | message?: string,
|
78 | data?: Object,
|
79 | }) => Promise<void>,
|
80 | |}
|
81 |
|
82 | type BotResponse = {|
|
83 | success: boolean,
|
84 | message?: string,
|
85 | data?: Object,
|
86 | |}
|
87 |
|
88 | type Handlers = {|
|
89 | enable?: (payload: { user_id: string }) => void,
|
90 | disable?: (payload: { user_id: string }) => void,
|
91 | conversation_added?: (payload: {
|
92 | user_id: string,
|
93 | conversation_id: string,
|
94 | }) => void,
|
95 | conversation_removed?: (payload: {
|
96 | user_id: string,
|
97 | conversation_id: string,
|
98 | }) => void,
|
99 | edit_subscription?: (payload: {
|
100 | user_id: string,
|
101 | conversation_id: string,
|
102 | subscription_id: string,
|
103 | }) => void,
|
104 | delete_subscription?: (payload: { subscription_id: string }) => void,
|
105 | post?: (payload: PostRequest) => BotResponse | Promise<BotResponse>,
|
106 | |}
|
107 | ```
|
108 |
|
109 | ## API
|
110 |
|
111 | ### createConfig
|
112 |
|
113 | `createConfig (parameters?: PartialConfig): Config`
|
114 |
|
115 | Creates the configuration using the provided parameters, environment variables or defaults.
|
116 |
|
117 | ### createContext
|
118 |
|
119 | `createContext (config: Config): BotContext`
|
120 |
|
121 | Creates a bot context provided to the handlers, notably to allow to communicate with Mainframe.
|
122 |
|
123 | ### createRouter
|
124 |
|
125 | `createRouter (handlers: Handlers, context: BotContext): express$Router`
|
126 |
|
127 | Creates an [Express Router](https://expressjs.com/en/4x/api.html#router) implementing HTTP endpoints for the provided handlers.
|
128 |
|
129 | ### createServer
|
130 |
|
131 | `createServer (handlers: Handlers, config: Config): express$Application`
|
132 |
|
133 | Creates an [Express Application](https://expressjs.com/en/4x/api.html#app) implementing HTTP endpoints for the provided handlers.
|
134 |
|
135 | ### log
|
136 |
|
137 | `log (...args: Array<mixed>): void`
|
138 |
|
139 | Logs using [debug](https://github.com/visionmedia/debug) with the `mainframe-bot` namespace.
|
140 |
|
141 | ### startServer
|
142 |
|
143 | `startServer (handlers: Handlers, parameters?: PartialConfig): void`
|
144 |
|
145 | Creates an [Express Application](https://expressjs.com/en/4x/api.html#app) implementing HTTP endpoints for the provided handlers and start listening using the `port` provided in `parameters`, defaulting to `4000`.
|
146 |
|
147 | ## License
|
148 |
|
149 | MIT
|
150 | See [LICENSE file](LICENSE)
|