-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
65 lines (59 loc) · 2.67 KB
/
app.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
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
const request = require('superagent');
const util = require('util')
const ngrok = require('ngrok')
const PORT = 4040
const CONVERSATION_API_BASE = 'https://driftapi.com/conversations'
const TOKEN = process.env.AUTH_TOKEN // only necessary if you're sending data back to the APIs
app.use(bodyParser.json())
app.listen(PORT, () => console.log(`Testing app listening on port ${PORT}!`))
// create external connection - I recommend commenting this out and install ngrok globally
// so the endpoint doesn't refresh every time you restart
ngrok.connect(PORT).then(url => console.log(`External Forwarding URL is: ${url}/api`))
// POST request to our /api endpoint
// This needs to have the ASYNC keyword because inside this function is a call to another async function meaning we need to wait for it
app.post('/api', async (req, res) => {
// Put the whole function in a try/catch loop
// That way if something goes wrong, we can understand it
// And not throw a fatal error, crashing the whole service
try {
// Get the convoId, if for some reason that element didn't exist in the request, it would throw an error
const conversationId = req.body.data.data.conversationId
// Use AWAIT to tell this function to hang here until getConversation resolves/rejects a promise to fulfill the conversation data
const conversation = await getConversation(conversationId)
// Check if an error message is included in the response and if not...
if (!conversation.error) {
// Log the success
console.log('It worked!', conversation.data)
// Also resolve the request successfully
return res.status(200).json(conversation.data);
} else {
// Otherwise, log the failure
console.log('Something went wrong:', conversation.error)
// And hangup the request with an error message
return res.status(500).json({error: conversation.error});
}
} catch (e) {
// Catch the generic error and log it
console.log('Something went wrong.')
// Also end the request
return res.status(500).json({error: e});
}
})
// HELPER functions
// look up meta data about this conversation
const getConversation = (conversationId) => {
return request.get(CONVERSATION_API_BASE + `/${conversationId}`)
.set('Content-type', 'application/json')
.set('Authorization', `Bearer ${TOKEN}`)
.then(res => {
return res.body
})
.catch(err => {
return {
error: err.message
}
});
}