|
|
|
"use strict";
|
|
|
|
|
|
|
|
const express = require("express");
|
|
|
|
const fs = require("fs");
|
|
|
|
const { tokenGenerate } = require("@vonage/jwt");
|
|
|
|
const privateKey = fs.readFileSync("private20250417.key");
|
|
|
|
const aclPaths = {
|
|
|
|
paths: {
|
|
|
|
"/*/rtc/**": {},
|
|
|
|
"/*/users/**": {},
|
|
|
|
"/*/conversations/**": {},
|
|
|
|
"/*/sessions/**": {},
|
|
|
|
"/*/devices/**": {},
|
|
|
|
"/*/image/**": {},
|
|
|
|
"/*/media/**": {},
|
|
|
|
"/*/knocking/**": {},
|
|
|
|
"/*/legs/**": {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const app = express();
|
|
|
|
app.use(express.json());
|
|
|
|
|
|
|
|
const app_id = '503c548f-cb61-4a3a-8599-cd815680b390';
|
|
|
|
const api_key = '197c68c9';
|
|
|
|
const api_secret = 'Aa11be17f298dfd4118bdf23';
|
|
|
|
const vonageNumber = "12019751815"; //用于通话的号码,今后根据前端提交来做号码切换
|
|
|
|
const vonageUser = "Highlights"; // user name
|
|
|
|
|
|
|
|
const response = (data) => {
|
|
|
|
return {
|
|
|
|
errcode: 0,
|
|
|
|
errmsg: '',
|
|
|
|
result: data,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//设置跨域访问
|
|
|
|
app.all("*", function (req, res, next) {
|
|
|
|
res.header("Access-Control-Allow-Origin", "*");
|
|
|
|
res.header("Access-Control-Allow-Headers", "*");
|
|
|
|
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
|
|
|
|
res.header("Content-Type", "application/json;charset=utf-8");
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/voice/answer", (req, res) => {
|
|
|
|
console.log("NCCO request:");
|
|
|
|
console.log(` - callee: ${req.query.to}`);
|
|
|
|
console.log("---");
|
|
|
|
const result = [
|
|
|
|
{
|
|
|
|
action: "talk",
|
|
|
|
text: "Please wait while we connect you.",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "record",
|
|
|
|
eventUrl: [`${req.protocol}://${req.get("host")}/vonage-server/voice/recordings`],
|
|
|
|
// eventUrl: [`${req.protocol}://${req.get("host")}/voice/recordings`], // test: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
action: "connect",
|
|
|
|
from: vonageNumber,
|
|
|
|
endpoint: [{ type: "phone", number: req.query.to }],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
res.json(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/jwt", (req, res) => {
|
|
|
|
console.log("jwt Generate:");
|
|
|
|
const exp_time = new Date(Date.now() + 3 * 24 * 3600 * 1000);
|
|
|
|
const token = tokenGenerate(app_id, privateKey, {
|
|
|
|
exp: Math.round(exp_time.getTime() / 1000),
|
|
|
|
sub: vonageUser,
|
|
|
|
acl: aclPaths,
|
|
|
|
});
|
|
|
|
console.log(Math.round(exp_time.getTime() / 1000));
|
|
|
|
// console.log(token);
|
|
|
|
console.log('token ✅');
|
|
|
|
console.log("---");
|
|
|
|
const result = response({ token, status: 200});
|
|
|
|
res.json(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.all("/voice/event", (req, res) => {
|
|
|
|
console.log("EVENT:");
|
|
|
|
console.dir(req.body);
|
|
|
|
console.log("---");
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.all("/voice/fallback", (req, res) => {
|
|
|
|
console.log("fallback:");
|
|
|
|
console.dir(req.body);
|
|
|
|
console.dir(res);
|
|
|
|
console.log("---");
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.all("/voice/recordings", (req, res) => {
|
|
|
|
console.log("recordings:");
|
|
|
|
console.dir(req.body);
|
|
|
|
res.sendStatus(200);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.listen(3009);
|