-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (121 loc) · 5.22 KB
/
index.js
File metadata and controls
161 lines (121 loc) · 5.22 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
what's in this file:
In this file you specify a JS module with some callbacks. Basically those callbacks get calls when you receive an event from the vonage backend. There's also a
special route function that is called on your conversation function start up allowing your to expose new local http endpoint
the event you can interract here are the same you can specify in your application: https://developer.nexmo.com/application/overview
event callbacks for rtc:
- rtcEvent (event, context)
event callbacks for anything else (those one are just standard express middleware access req.nexmo to get the context):
voice callbacks
- voiceEvent (req, res, next)
- voiceAnswer (req, res, next)
messages callbacks (if you specifiy one of thise, you need to declare both of them, those one are just standard express middleware access req.nexmo ):
- messagesInbound (req, res, next)
- messagesStatus (req, res, next)
route(app) // app is an express app
nexmo context:
you can find this as the second parameter of rtcEvent funciton or as part or the request in req.nexmo in every request received by the handler
you specify in the route function.
it contains the following:
const {
generateBEToken,
generateUserToken,
logger,
csClient,
storageClient
} = nexmo;
- generateBEToken, generateUserToken,// those methods can generate a valid token for application
- csClient: this is just a wrapper on https://github.com/axios/axios who is already authenticated as a nexmo application and
is gonna already log any request/response you do on conversation api.
Here is the api spec: https://jurgob.github.io/conversation-service-docs/#/openapiuiv3
- logger: this is an integrated logger, basically a bunyan instance
- storageClient: this is a simple key/value inmemory-storage client based on redis
*/
/**
*
* This function is meant to handle all the asyncronus event you are gonna receive from conversation api
*
* it has 2 parameters, event and nexmo context
* @param {object} event - this is a conversation api event. Find the list of the event here: https://jurgob.github.io/conversation-service-docs/#/customv3
* @param {object} nexmo - see the context section above
* */
const DATACENTER = `https://api.nexmo.com`
const rtcEvent = async (event, { logger, csClient }) => {
try {
const type = event.type
if (type === 'app:knocking') { /* I m receiving a knocker, it means someone is trying to enstiblish a call */
const knocking_id = event.from
/* create a conversation */
const channel = event.body.channel
const convRes = await csClient({
url: `${DATACENTER}/v0.3/conversations`,
method: "post",
data: {},
})
const conversation_id = convRes.data.id
const user_id = event.body.user.id
/* join the user created by the knocker in the conversation aka we join the caller to the conversation we have just created */
const memberRes = await csClient({
url: `${DATACENTER}/v0.3/conversations/${conversation_id}/members`,
method: "post",
data: {
user: {
id: user_id
} ,
knocking_id: knocking_id,
state: "joined",
channel: {
type: channel.type,
id: channel.id,
to: channel.to,
from: channel.from,
"preanswer": false
},
"media": {
"audio": true
}
}
})
} else if (type === 'member:media' && (event.body.media && event.body.media.audio === true)) { /* the member as the audio enabled */
const legId = event.body.channel.id
/* we send a text to speech action to the conversation */
await csClient({
url: `${DATACENTER}/v0.3/legs/${legId}/talk`,
method: "post",
data: { "loop": 1, "text": "Hello, have a nice day! ", "level": 0, "voice_name": "Kimberly" },
})
} else if (type == 'audio:say:done'){ /* the text to speech is finished */
/* we hangup the call */
const legId = event.body.channel.id
await csClient({
url: `${DATACENTER}/v0.1/legs/${legId}`,
method: "put",
data: { "action": "hangup", "uuid": legId }
})
}
} catch (err) {
logger.error("Error on rtcEvent function")
}
}
/**
*
* @param {object} app - this is an express app
* you can register and handler same way you would do in express.
* the only difference is that in every req, you will have a req.nexmo variable containning a nexmo context
*
*/
const route = (app) => {
app.get('/hello', async (req, res) => {
const {
logger,
} = req.nexmo;
logger.info(`Hello Request HTTP `)
res.json({
text: "Hello Request!"
})
})
}
module.exports = {
rtcEvent,
route
}