UNPKG

3.35 kBMarkdownView Raw
1# Mainframe JavaScript SDK for bots
2
3Work in progress
4
5## Installation
6
7```sh
8npm install @mainframe/bot-sdk # npm
9yarn add @mainframe/bot-sdk # Yarn
10```
11
12## Example
13
14```js
15const { startServer } = require('@mainframe/bot-sdk')
16
17startServer(
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
32The SDK uses the following [Flow types](https://flow.org/en/docs/types/):
33
34```js
35type PartialConfig = {
36 +mainframe_secret: string,
37 +mainframe_url: string,
38 +port: number,
39}
40
41type Config = {|
42 +mainframe_secret: string,
43 +mainframe_url: string,
44 +port: number,
45|}
46
47type 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
66type PostPayload = {|
67 +user_id: string,
68 +data: Object,
69 +context: ClientContext,
70|}
71
72type 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
82type BotResponse = {|
83 success: boolean,
84 message?: string,
85 data?: Object,
86|}
87
88type 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
115Creates the configuration using the provided parameters, environment variables or defaults.
116
117### createContext
118
119`createContext (config: Config): BotContext`
120
121Creates 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
127Creates 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
133Creates 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
139Logs using [debug](https://github.com/visionmedia/debug) with the `mainframe-bot` namespace.
140
141### startServer
142
143`startServer (handlers: Handlers, parameters?: PartialConfig): void`
144
145Creates 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
149MIT
150See [LICENSE file](LICENSE)