-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
54 lines (41 loc) · 1.59 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
52
53
54
import 'dotenv/config'
import express from 'express'
import cors from 'cors'
import ask from './openai.js'
const main = async ({ port }) => {
const app = express()
app.use(express.json())
app.use(express.urlencoded())
app.use(cors())
app.get('/', (_req, res) => res.send('Welcome to the Trade Information Assistant'))
app.post('/ussd', async (req, res) => {
const { text } = req.body
let response = ''
if (text === '') {
response = 'CON Welcome to Trade Information Assistant.\nThis service helps you get answers to your trading questions.\nEnter: \n1. To ask a question\n0. To exit'
} else if (text === '1') {
response = 'CON Please enter your trade-related question.'
} else if (text === '0') {
response = 'END You have exited the Trade Information Assistant. Thank you for using our service. Goodbye!'
} else if (text.split('*').length > 1) {
console.log({ text })
const parts = text.split('*')
const input = parts[parts.length - 1].split('#')[0]
const outcome = await ask(input)
if (outcome.status === 'success') {
response = `END ${outcome.message}`
} else {
response = 'END Sorry, an error occurred. Please try again.'
}
} else {
response = 'END Invalid input. Please try again.'
};
res.set('Content-Type: text/plain')
res.send(response)
})
app.use('*', (_req, res) => res.status(400).send('Invalid route. Please check your URL and try again.'))
app.listen(port, () => console.log(`App running on port ${port}`))
}
main({
port: process.env.APP_PORT || 3000
})