All files / src/messages SetDialogMessage.js

26.92% Statements 7/26
0% Branches 0/12
0% Functions 0/10
26.92% Lines 7/26
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55      1x   1x                 1x 1x   1x             1x                                                           1x
/* jslint es6 */
'use strict';
 
const DEFAULT_ADDRESS = require('./SessionMessage').DEFAULT_ADDRESS;
 
const BaseScriptStep = require('./BaseScriptStep');
 
function SetDialogMessage(step, config, bot, logReporter, prevStepPromise ) {
  BaseScriptStep.call(this,step,config,bot,logReporter,prevStepPromise);
 
  this.stepFinishedPromise = Promise.all([prevStepPromise] ).then(() => {
    return this.send();
  });
}
SetDialogMessage.prototype = Object.create(BaseScriptStep.prototype);
SetDialogMessage.prototype.constructor = SetDialogMessage;
 
SetDialogMessage.prototype.initSession = function () {
  return new Promise((resolve, reject) => {
    this.bot.loadSession(DEFAULT_ADDRESS, (session) => {
      return resolve(session);
    });
  });
}
SetDialogMessage.prototype.send = function () {
  return new Promise((resolve, reject) => {
    this.initSession()
      .then((session) => {
        if (!session) {
          if ( "undefined" != typeof this.config.dialog ) {
            this.bot.settings.defaultDialogId = this.config.dialog;
          }
          if ( "undefined" != typeof this.config.args ) {
            this.bot.settings.defaultDialogArgs = this.config.args ;
          }
 
        } else {
          let dialog = this.config.dialog || this.bot.settings.defaultDialogId || '/';
          let dialogArgs = this.config.arguments || this.bot.settings.defaultDialogArgs || undefined;
 
          session.replaceDialog(dialog,dialogArgs);
        }
      })
    .then(() => {
      this.logReporter.startupDialog(this.step, this.config.dialog, this.config.args);
      resolve();
    })
    .catch((error) => {
      reject(error);
    })
  })
 
}
 
module.exports = SetDialogMessage;