|
| 1 | +const path = require('path') |
| 2 | +const { nanoid } = require('nanoid') |
| 3 | +const cors = require('cors') |
| 4 | +const axios = require('axios') |
| 5 | +const app = require('express')() |
| 6 | +const http = require('http').Server(app) |
| 7 | +const io = require('socket.io')(http) |
| 8 | + |
| 9 | +const SAPCAI_TOKEN = '5ee1b84709db76f5bbff8ea14dc9ad85' |
| 10 | + |
| 11 | +app.use(cors()) |
| 12 | + |
| 13 | +app.get('/', (req, res) => { |
| 14 | + res.sendFile(path.join(__dirname, 'index.html')) |
| 15 | +}) |
| 16 | + |
| 17 | +io.on('connection', (socket) => { |
| 18 | + console.log('user connected') |
| 19 | + socket.on('disconnect', () => { |
| 20 | + console.log('user disconnected') |
| 21 | + }) |
| 22 | + socket.on('session_request', (msg) => { |
| 23 | + console.log('session_request', msg) |
| 24 | + if (msg && msg.session_id) { |
| 25 | + socket.emit('session_confirm', { session_id: msg.session_id }) |
| 26 | + } else { |
| 27 | + socket.emit('session_confirm', { session_id: nanoid() }) |
| 28 | + } |
| 29 | + }) |
| 30 | + socket.on('user_uttered', async (msg) => { |
| 31 | + if (msg && msg.message) { |
| 32 | + let textInput = msg.message |
| 33 | + |
| 34 | + if (msg.message.startsWith('data:')) { |
| 35 | + console.log('user_uttered audio') |
| 36 | + |
| 37 | + const base64Data = msg.message.substring(msg.message.indexOf(',') + 1) |
| 38 | + const audioData = Buffer.from(base64Data, 'base64') |
| 39 | + console.log(`Received data length ${audioData.length}`) |
| 40 | + |
| 41 | + const wavToMonoWavRequestOptions = { |
| 42 | + method: 'POST', |
| 43 | + url: 'https://speech.botiumbox.com/api/convert/WAVTOMONOWAV', |
| 44 | + data: audioData, |
| 45 | + headers: { |
| 46 | + 'content-type': 'audio/wav' |
| 47 | + }, |
| 48 | + responseType: 'arraybuffer' |
| 49 | + } |
| 50 | + const wavToMonoWavResponse = await axios(wavToMonoWavRequestOptions) |
| 51 | + console.log(`Converted to mono wav length ${wavToMonoWavResponse.data.length}`) |
| 52 | + |
| 53 | + const sttRequestOptions = { |
| 54 | + method: 'POST', |
| 55 | + url: 'https://speech.botiumbox.com/api/stt/en', |
| 56 | + data: wavToMonoWavResponse.data, |
| 57 | + headers: { |
| 58 | + 'content-type': 'audio/wav' |
| 59 | + }, |
| 60 | + responseType: 'json' |
| 61 | + } |
| 62 | + const sttResponse = await axios(sttRequestOptions) |
| 63 | + console.log('sttResponse', JSON.stringify(sttResponse.data, null, 2)) |
| 64 | + |
| 65 | + textInput = sttResponse.data.text |
| 66 | + } else { |
| 67 | + console.log('user_uttered text', msg) |
| 68 | + } |
| 69 | + |
| 70 | + const requestOptions = { |
| 71 | + method: 'POST', |
| 72 | + url: 'https://api.cai.tools.sap/build/v1/dialog', |
| 73 | + headers: { |
| 74 | + Authorization: `Token ${SAPCAI_TOKEN}` |
| 75 | + }, |
| 76 | + data: { |
| 77 | + message: { |
| 78 | + type: 'text', |
| 79 | + content: textInput |
| 80 | + }, |
| 81 | + conversation_id: msg.session_id || nanoid() |
| 82 | + } |
| 83 | + } |
| 84 | + try { |
| 85 | + const response = await axios(requestOptions) |
| 86 | + console.log('sap response', JSON.stringify(response.data, null, 2)) |
| 87 | + |
| 88 | + for (const message of response.data.results.messages.filter(t => t.type === 'text')) { |
| 89 | + const botUttered = { |
| 90 | + text: message.content.replace(/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g, '') |
| 91 | + } |
| 92 | + |
| 93 | + const ttsRequestOptions = { |
| 94 | + method: 'GET', |
| 95 | + url: 'https://speech.botiumbox.com/api/tts/en', |
| 96 | + params: { |
| 97 | + text: message.content |
| 98 | + }, |
| 99 | + responseType: 'arraybuffer' |
| 100 | + } |
| 101 | + const ttsResponse = await axios(ttsRequestOptions) |
| 102 | + botUttered.link = 'data:audio/wav;base64,' + Buffer.from(ttsResponse.data, 'binary').toString('base64') |
| 103 | + |
| 104 | + socket.emit('bot_uttered', botUttered) |
| 105 | + } |
| 106 | + } catch (err) { |
| 107 | + console.log(err.message) |
| 108 | + } |
| 109 | + } |
| 110 | + }) |
| 111 | +}) |
| 112 | + |
| 113 | +const port = process.env.PORT || 5005 |
| 114 | +http.listen(port, () => { |
| 115 | + console.log('listening on *:' + port) |
| 116 | +}) |
0 commit comments