---
# System prepended metadata

title: Untitled

---

const Alexa = require('ask-sdk-core');
const http = require('http');
const querystring = require('querystring');
var fs = require('fs');


function film_annee(annee) {
    return new Promise(((resolve, reject) => {
        
    var post_data = querystring.stringify({
      'primary_release_year' : annee
    });
  
    var options = {
        host: '195.83.128.21',
        port: 80,
        path: '/~dev1908/apialexa/alexa.php',
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': Buffer.byteLength(post_data)
        }
    };
   
    const request = http.request(options, (response) => {
      response.setEncoding('utf8');
      let returnData = '';
      console.log(response);

      response.on('data', (chunk) => {
        returnData += chunk;
      });

      response.on('end', () => {
        resolve(returnData);
      });

      response.on('error', (error) => {
        reject(error);
      });
    });
    request.write(post_data);
    request.end();
  }));
}


const LaunchRequestHandler = {
    canHandle(handlerInput) {
        return handlerInput.requestEnvelope.request.type === 'LaunchRequest';
    },
    handle(handlerInput) {
        const speechText = 'Bonjour, quelle est votre demande ?';
return handlerInput.responseBuilder
            .speak(speechText)
            .reprompt(speechText)
            .withSimpleCard('Hello World', speechText)
            .getResponse();
    }
};

const filmannee = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'film_annee';
    },
    async handle(handlerInput) {
        var annee = Alexa.getSlotValue(handlerInput.requestEnvelope, 'annee');
        const lareponse = await film_annee(annee);
        return handlerInput.responseBuilder
          .speak(lareponse)
          .reprompt(lareponse)
          .withSimpleCard('Hello World', lareponse)
          .getResponse();
    }
};

function film_infos(infos) {
    return new Promise(((resolve, reject) => {
        
    var post_data = querystring.stringify({
      'query' : infos
    });
  
    var options = {
        host: '195.83.128.21',
        port: 80,
        path: '/~dev1908/apialexa/alexa.php',
        method: 'POST',
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          'Content-Length': Buffer.byteLength(post_data)
        }
    };
   
    const request = http.request(options, (response) => {
      response.setEncoding('utf8');
      let returnData = '';
      console.log(response);

      response.on('data', (chunk) => {
        returnData += chunk;
      });

      response.on('end', () => {
        resolve(returnData);
      });

      response.on('error', (error) => {
        reject(error);
      });
    });
    request.write(post_data);
    request.end();
  }));
}

const filminfos = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'film_infos';
    },
    async handle(handlerInput) {
        var infos = Alexa.getSlotValue(handlerInput.requestEnvelope, 'infos');
        const lareponse = await film_infos(infos);
        return handlerInput.responseBuilder
          .speak(lareponse)
          .reprompt(lareponse)
          .withSimpleCard('Hello World', lareponse)
          .getResponse();
    }
};


exports.handler = Alexa.SkillBuilders.custom().addRequestHandlers(  LaunchRequestHandler, filmannee, filminfos ).lambda();