使用节点js进行语音呼叫

时间:2017-11-08 10:28:18

标签: node.js

我正在使用Java Web服务器和节点js(用于聊天)

现在我想使用Twilio`

进行语音通话

我写了这样的代码

var fs = require('fs');
var sslOptions = {};
var path = require('path');
var express = require('express');
var app = express();

var server = require('https').createServer(sslOptions, app);
const EventEmitter = require('events');
const myEE = new EventEmitter();
server.on("request", function (req, res) {
    res.end("this is the response");
});
server.listen('8090', function(){
    console.log("Secure Express server listening on port 8090");
});

var accountSid = 'AC***************************';
var authToken = "******************************";
var client = require('twilio')(accountSid, authToken);
var morgan = require('morgan');
var bodyParser = require('body-parser');
var twilio = require('twilio');
var VoiceResponse = twilio.twiml.VoiceResponse;

module.exports = server;

app.use(express.static(path.join(process.cwd(), 'public')));
app.use(bodyParser.json());
    app.use(bodyParser.urlencoded({
        extended: true,
    }));

    app.use(morgan('combined'));


    app.get('/', function(request, response) {
        response.render('index');
    });


    app.post('/call', function(request, response) {
        var salesNumber = request.body.salesNumber;
        var url = 'http://' + request.headers.host + '/outbound/' + encodeURIComponent(salesNumber);

        var options = {
            to: '+91*******',
            from: '+17******',
            url: url,
        };


        client.calls.create(options)
          .then((message) => {

            response.send({
                message: 'Thank you! We will be calling you shortly.',
            });
          })
          .catch((error) => {
              console.log('errot');
        //    console.log(error);
            response.status(500).send('error');
          });
    });


    app.post('/outbound/:salesNumber', function(request, response) {
        var salesNumber = request.params.salesNumber;
        var twimlResponse = new VoiceResponse();

        twimlResponse.say('Thanks for contacting our sales department. Our ' +
                          'next available representative will take your call. ',
                          { voice: 'alice' });

        twimlResponse.dial(salesNumber);

        response.send(twimlResponse.toString());
    });

我正在尝试从我的一个javascript文件到快速路径进行ajax调用

$.ajax({ url: '/call',
            method: 'POST',
            dataType: 'application/json',
            processData: false,
            data: {
                phoneNumber: '+91*******',
                salesNumber:'+17******** '
            }
        }).done(function(data) {
            // The JSON sent back from the server will contain a success message
            alert(data.message);
        }).fail(function(error) {
            //alert('errot');
            alert(JSON.stringify(error));
        });

当我执行这个ajax调用时

它正在寻找Java服务器并返回404错误

如何解决此问题

任何人,请帮我解决这个问题

0 个答案:

没有答案