-
Notifications
You must be signed in to change notification settings - Fork 4
/
express.js
123 lines (111 loc) · 4.34 KB
/
express.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* @author David Menger
*/
'use strict';
const mongoose = require('mongoose');
const { Router } = require('express');
const bodyParser = require('body-parser');
const {
postMiddlewares,
getVerifierMiddleware,
createValidator,
createUpdater
} = require('./src/expressHooks');
const Processor = require('./src/Processor');
const { MongoBotToken, MongoState, MongoChatLog } = require('./src/mongodb');
/**
* Create a chat event processor
*
* @param {function|Router} reducer - Root router object or processor function
* @param {Object} processorOptions - settings for message processing
* @param {string} [processorOptions.pageToken] - page token
* @param {string} [processorOptions.appSecret] - bot application secret
* @param {string} [processorOptions.appUrl] - where the bot application is deployed
* @param {number} [processorOptions.timeout] - how long the state will be locked for single event
* @param {Object} [processorOptions.log] - console.log/error/warn like object
* @param {Object} [processorOptions.defaultState] - default conversation state
* @param {MongoChatLog} [processorOptions.chatLog] - discussion logger
* @param {MongoBotToken} [processorOptions.tokenStorage] - storage for chabot tokens
* @param {Function} [processorOptions.senderFnFactory] - override default sender function
* @param {Function} [processorOptions.securityMiddleware] - override webview calls authorizer
* @param {string} [processorOptions.cookieName] - webview cookie (for default securityMiddleware)
* @param {boolean} [processorOptions.loadUsers] - set false to not load user profiles
* @param {Object} [processorOptions.userLoader] - override default user loader
* @param {Function} [processorOptions.onSenderError] - override default sender error reporter
* @param {Object|boolean} [processorOptions.autoTyping] - enable auto typing
* @param {number} [processorOptions.autoTyping.time] - default typing time
* @param {number} [processorOptions.autoTyping.perCharacters] - typing time per character
* @param {number} [processorOptions.autoTyping.minTime] - auto typing lower threshold
* @param {number} [processorOptions.autoTyping.maxTime] - auto typing upper threshold
* @param {MongoState} [stateStorage] - storage for states
* @example
* const express = require('express');
* const bodyParser = require('body-parser');
* const mongoose = require('mongoose');
* const { createRouter, createProcessor } = require('botnaut/express');
*
* const handler = (req, res, postBack) => {
* res.typingOn()
* .wait();
*
* switch (req.action()) {
* case 'hello':
* res.text('Hello world');
* return;
* default:
* // send one quick reply
* res.text('What you want?', {
* hello: 'Say hello world'
* })
* }
* };
*
* const processor = createProcessor(handler, {
* pageToken: 'stringhere',
* appSecret: 'botappsecret'
* });
*
* app = express();
*
* app.use('/bot', createRouter(processor));
*
* mongoose.connect('mongodb://localhost/myapp')
* .then(() => app.listen(3000));
*/
function createProcessor (reducer, processorOptions, stateStorage = null) {
let state = stateStorage;
if (state === null) {
state = mongoose.model('State', MongoState);
}
if (!processorOptions.tokenStorage) {
const tokenStorage = mongoose.model('BotToken', MongoBotToken);
Object.assign(processorOptions, { tokenStorage });
}
if (!processorOptions.chatLog) {
const chatLog = mongoose.model('ChatLog', MongoChatLog);
Object.assign(processorOptions, { chatLog });
}
return new Processor(reducer, processorOptions, state);
}
/**
* Create an express route for accepting messenger events
*
* @param {function|Router} reducer - Root router object or processor function
* @param {string} verifyToken - chatbot application token
* @param {Object} [log] - console.* like logger object
*/
function createRouter (processor, verifyToken, log = console) {
const app = new Router();
app.post('/', ...postMiddlewares(bodyParser, processor, log));
app.get('/', getVerifierMiddleware(verifyToken));
return app;
}
module.exports = {
createProcessor,
createRouter,
State: MongoState,
BotToken: MongoBotToken,
ChatLog: MongoChatLog,
createValidator,
createUpdater
};