Speed up development with full-stack environments for every branch.

Learn More

Leads [Node.js and Hello World]

Forked from node project.

3 Runs 4 Views 0 Copies

Hello world in Node.js

Saved

Saved

arunraj_cr 1

arunraj_cr
published 11 days ago

    'use strict';
    
    const express = require('express');
    const bodyParser = require('body-parser');
    const https = require('https');
    
    const restService = express();
    
    restService.use(bodyParser.urlencoded({
        extended: true
    }));
    
    restService.use(bodyParser.json());
    
    restService.post('/hook', function(req, res) {
        //var speech = req.body.result && req.body.result.parameters && req.body.result.parameters.echoText ? req.body.result.parameters.echoText : "Seems like some problem. Speak again."
        var speech= req.body.result && req.body.result.parameters && req.body.result.parameters.leads ? "talking about the leads boss?" : "Seems like some problem. Speak again."
        getSalesData(leadsCallBack, speech, res);
    });
    
    function leadsCallBack(leads, speech, res){
      console.log(leads);
      return res.json({
            speech: speech + leads,
            displayText: speech + getSalesData(),
            source: 'emma-sales'
        });
    }
    
    //get things from the API
        function getSalesData(callBack, speech, res) {
        var https = require('https');
        var url = 'https://demo6357459.mockable.io/googlehome/action/';
        
        https.get(url, function(res){
        var body = '';
        
        res.on('data', function(chunk){
        body += chunk;
        });
        
        res.on('end', function(){
        var apiResponse = JSON.parse(body);
        var leads = apiResponse.leads;
        callBack(leads, speech, res)
        });
        }).on('error', function(e){
        console.log("Got an error: ", e);
        });
        }
    
    restService.listen((process.env.PORT || 8000), function() {
        console.log("Server up and listening");
    });
    
    Please login/signup to get access to the terminal.

    Your session has timed out.

    Dismiss (the page may not function properly).