-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
51 lines (43 loc) · 1.51 KB
/
server.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
/*
* server.js
* This file is the core of your bot
*
* It creates a little server using express
* So, your bot can be triggered throught "/" route
*
* This file was made for locally testing your bot
* You can test it by running this command
* curl -X "POST" "http://localhost:5000" -d '{"text": "YOUR_TEXT"}' -H "Content-Type: application/json; charset=utf-8"
* You might modify the server port ^^^^ depending on your configuration in config.js file
*/
const express = require('express')
const bodyParser = require('body-parser')
// Load configuration
//require('./config')
const bot = require('./bot').bot
// Start Express server
const app = express()
app.set('port', process.env.PORT || 5000)
app.use(bodyParser.json())
// Handle / route
app.use('/', (request, response) => {
// Call bot main function
bot(request.body, response, (error, success) => {
if (error) {
console.log('Error in your bot:', error)
if (!response.headersSent) { response.sendStatus(400) }
} else if (success) {
console.log(success)
if (!response.headersSent) { response.status(200).json(success) }
}
})
})
if (!process.env.REQUEST_TOKEN.length) {
console.log('ERROR: process.env.REQUEST_TOKEN variable in src/config.js file is empty ! You must fill this field with the request_token of your bot before launching your bot locally')
process.exit(0)
} else {
// Run Express server, on right port
app.listen(app.get('port'), () => {
console.log('Our bot is running on port', app.get('port'))
})
}