forked from Onurrr/liteIM-1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
128 lines (97 loc) · 3.92 KB
/
Copy pathserver.js
File metadata and controls
128 lines (97 loc) · 3.92 KB
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
124
125
126
127
128
require('dotenv').config()
const Express = require('express')
const express = Express()
const helmet = require('helmet')
const bodyParser = require('body-parser')
const cors = require('cors')
express.use(cors())
express.use(helmet())
express.use(bodyParser.json())
express.use(bodyParser.urlencoded({ extended: false }))
express.get(process.env.BASE_PATH + '/:service/webhook/', function(req, res) {
if (req.query['hub.verify_token'] === process.env.FACEBOOK_MESSENGER_VERIFY) {
return res.send(req.query['hub.challenge'])
}
res.send('wrong token')
})
express.post(process.env.BASE_PATH + '/:service/webhook', async (req, res, next) => {
res.locals.service = req.params.service
const handleService = require(`./handlers/services/${req.params.service}`)
.middleware
const handler = await handleService(req, res)
res.status(200)
if (handler.error) return res.send(handler.error)
if (!handler.success) return res.send('unknown error')
let handleCommands = await require('./handlers/parsers/commands')(req, res)
if (!handleCommands.continue) return res.send({ success: true })
let handleConversations = await require('./handlers/parsers/conversations')(
req,
res
)
if (!handleConversations.continue) return res.send({ success: true })
await require('./handlers/parsers/uncaught')(req, res)
return res.send({ success: true })
})
express.post('/liteIM/password', async (req, res, next) => {
const ActionHandler = require('./handlers/action_handler')
const { service, serviceID, email, isUser, password, newPassword } = req.body
const { serviceOptions, middleware } = require(`./handlers/services/${service}`)
res.locals.service = service
res.locals.serviceOptions = serviceOptions
res.locals.serviceID = serviceID
res.locals.message = newPassword ? `${password} ${newPassword}` : password
res.status(200)
if (isUser === 'true') {
try {
await new ActionHandler(service, serviceOptions).getToken(email, password)
} catch (err) {
//invalid password
return res.send({ success: false, error: 'You have entered an incorrect password. Please try again.' })
}
}
let redirect
if (service === 'messenger') {
redirect = 'https://www.messenger.com/closeWindow/?image_url=https%3A%2F%2Fwww.lite.im%2Fstatic%2Ficons%2FIcon-1024.png&display_text=Redirecting%20you%20to%20Messenger'
}
else if (service === 'telegram') {
redirect = 'https://t.me/liteIM_bot'
}
let response = redirect ? { success: true, redirect } : { success: true}
const handler = await middleware(req, res)
if (handler.error) return res.send(handler.error)
if (!handler.success) return res.send('unknown error')
let handleCommands = await require('./handlers/parsers/commands')(req, res)
if (!handleCommands.continue) return res.send(response)
let handleConversations = await require('./handlers/parsers/conversations')(
req,
res
)
if (!handleConversations.continue) return res.send(response)
await require('./handlers/parsers/uncaught')(req, res)
return res.send(response)
})
express.post(process.env.BASE_PATH + '/notifier', async (req, res) => {
const notifier = require('./handlers/notifier')
let notifierResult = false
try {
notifierResult = await notifier(req.body)
} catch (e) {
console.log(e)
}
res.send(notifierResult)
})
express.post(process.env.BASE_PATH + '/broadcast', async (req, res) => {
const broadcast = require('./handlers/broadcast')
let broadcastResult = false
try {
broadcastResult = await broadcast(req.body)
} catch (e) {
console.log(e)
}
res.send(broadcastResult)
})
let port = process.env.port || 3001
express.listen(port, err => {
if (err) return console.error('ERROR:', err)
console.log('Server is listening on port ', port)
})