Skip to content

Commit 8ac5c2e

Browse files
authored
Merge pull request #1 from obvonage/main
upload files
2 parents 671cd7a + de6bc23 commit 8ac5c2e

File tree

6 files changed

+2121
-0
lines changed

6 files changed

+2121
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.env
2+
node_modules/

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
## Integrate Phone Calls and SMS with OpenAI
2+
3+
We've already developed a starter Vonage Voice application to receive a call, catch your response, and send it to a 3rd party (OpenAI).
4+
Users can deploy the App using Github Codespaces.
5+
Fork [this repository](https://github.com/Vonage-Community/tutorial-voice-messages-node-openai-integration). Open it in Codespaces by clicking "Create codespace on main"
6+
7+
![Create Codespace interface](codespaces.png)
8+

codespaces.png

333 KB
Loading

index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict'
2+
3+
require("dotenv").config();
4+
const express = require('express')
5+
const bodyParser = require('body-parser')
6+
const app = express()
7+
const http = require('http')
8+
const unirest = require('unirest')
9+
10+
const apiKey = process.env.API_KEY;
11+
const apiSecret = process.env.API_SECRET;
12+
const openaiApiKey = process.env.OPENAI_API_KEY;
13+
const eventUrl = process.env.EVENT_URL
14+
15+
app.use(bodyParser.json())
16+
17+
//const privateKey = require('fs').readFileSync('private.key');
18+
19+
app.get('/webhooks/answer', (request, response) => {
20+
21+
const ncco = [{
22+
action: 'talk',
23+
text: 'Hi, describe an image that you want to generate'
24+
},
25+
{
26+
eventMethod: 'POST',
27+
action: 'input',
28+
eventUrl: [
29+
eventUrl],
30+
type: [ "speech" ],
31+
speech: {
32+
language: 'en-gb',
33+
endOnSilence: 0.5
34+
}
35+
},
36+
{
37+
action: 'talk',
38+
text: 'Thank you'
39+
}
40+
]
41+
console.log('/webhooks/answer')
42+
response.json(ncco)
43+
44+
})
45+
46+
app.post('/webhooks/asr', (request, response) => {
47+
48+
console.log(request.body)
49+
console.log(request.body.speech.results[0].text)
50+
console.log(request.body.from)
51+
let phoneNumber = request.body.from
52+
console.log(request.body.timestamp)
53+
let promptText = request.body.speech.results[0].text
54+
55+
var req = unirest('POST', 'https://api.openai.com/v1/images/generations')
56+
.headers({
57+
'Content-Type': 'application/json',
58+
'Authorization': 'Bearer ' + openaiApiKey
59+
})
60+
.send(JSON.stringify({
61+
"prompt": promptText,
62+
"n": 1,
63+
"size": "1024x1024"
64+
}))
65+
.end(function (res) {
66+
if (res.error) throw new Error(res.error);
67+
console.log(res.raw_body);
68+
console.log(res.body.data[0].url)
69+
let imgUrl = res.body.data[0].url
70+
sentMsg(phoneNumber, imgUrl)
71+
72+
});
73+
74+
const ncco = [{
75+
action: 'talk',
76+
text: `Got it, I'll sent link to generated image in WhatsApp`
77+
}]
78+
response.json(ncco)
79+
80+
})
81+
82+
function sentMsg(phoneNumber, imgUrl) {
83+
console.log('sentMsg');
84+
console.log(phoneNumber, imgUrl);
85+
86+
var req = unirest('POST', 'https://messages-sandbox.nexmo.com/v1/messages')
87+
.headers({
88+
'Authorization': 'Basic ' + Buffer.from(apiKey + ':' + apiSecret).toString('base64'),
89+
'Content-Type': 'application/json'
90+
})
91+
.send(JSON.stringify({
92+
"from": "14157386102",
93+
"to": phoneNumber,
94+
"message_type": "text",
95+
"text": imgUrl,
96+
"channel": "whatsapp"
97+
}))
98+
.end(function (res) {
99+
if (res.error) throw new Error(res.error);
100+
console.log(res.raw_body);
101+
});
102+
}
103+
104+
105+
app.post('/webhooks/events', (request, response) => {
106+
107+
console.log('/webhooks/events')
108+
console.log(request.body)
109+
response.sendStatus(200);
110+
})
111+
112+
const port = 3000
113+
app.listen(port, () => console.log(`Listening on port ${port}`))

0 commit comments

Comments
 (0)