All files / botbuilder-unit/src/messages UserMessage.js

91.67% Statements 22/24
83.33% Branches 5/6
87.5% Functions 7/8
91.67% Lines 22/24
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 56 57 58 59      1x     69x     69x   69x 69x     1x 1x   1x 69x 69x   69x   69x 2x   67x       69x 1x 1x             1x   68x   69x       69x                   1x
/* jslint es6 */
'use strict';
 
const BaseScriptStep = require('./BaseScriptStep');
 
function UserMessage(step, config, bot, logReporter, prevStepPromise )  {
  BaseScriptStep.call(this,step,config,bot,logReporter,prevStepPromise);
 
 
  this.connector = this.bot.connector('console');
 
  this.stepFinishedPromise = Promise.all([prevStepPromise] ).then(() => {
    return this.send();
  });
}
UserMessage.prototype = Object.create(BaseScriptStep.prototype);
UserMessage.prototype.constructor = UserMessage;
 
UserMessage.prototype.send = function () {
  return new Promise((resolve, reject) => {
    this.logReporter.messageSent(this.step, this.config);
 
    Promise.resolve()
      .then(() => {
        if ("function" === typeof this.config.user) {
          return this.config.user();
        } else {
          return Promise.resolve(this.config.user);
        }
      })
      .then((message) => {
        if ("object" == typeof message) {
          Eif (!message.data.address) {
            message.address({
              channelId: 'console',
              user: {id: 'user', name: 'User1'},
              bot: {id: 'bot', name: 'Bot'},
              conversation: {id: 'Convo1'}
            });
          }
          this.connector.onEventHandler([message.toMessage()]);
        } else {
          this.connector.processMessage(message);
        }
        return true;
      })
 
      .then(() => {
        resolve();
      })
      .catch((err)=> {
        this.logReporter.error(`Error at step ${this.step}`, err);
        reject(err);
      });
  })
};
 
 
module.exports = UserMessage;