commonfields:
  id: SendMessageToOnlineUsers
  version: -1
name: SendMessageToOnlineUsers
script: |-
  var commandAvailable = function(command) {
      var ret = false;
      var allModules = getModulesForCommand(command);
      if (allModules) {
          var instanceKeys = Object.keys(allModules);
          instanceKeys.forEach(function (key) {
              if (allModules[key].state === 'active') {
                  ret = true;
              }
          });
      }
      return ret;
  };
  var slackEnabled = commandAvailable('slack-send');
  var emailEnabled = commandAvailable('send-mail');

  var medium = (args.medium) ? args.medium.toLowerCase() : 'email';

  var res = executeCommand("getUsers", {online: true});
  if (isError(res[0])) {
      return res[0];
  }
  var users = dq(res[0].Contents,'email');

  var entries = [];
  var sent = [];
  users.forEach(function(user) {
      if ((medium === 'all' && emailEnabled) || medium === 'email') {
          res = executeCommand("send-mail", {to: user, subject: 'Notification from Demisto server', body: args.message});
          if (isError(res[0])) {
              entries.push(res[0]);
          } else {
              sent.push(user);
          }
      }
      if ((medium === 'all' && slackEnabled) || medium === 'slack') {
          res = executeCommand("slack-send", {to: user, message: args.message});
          if (isError(res[0])) {
              entries.push(res[0]);
          } else {
              sent.push(user);
          }
      }
  });

  if (sent.length > 0) {
      entries.push('Successfuly sent ' + medium + ' message to ' + sent.join(','));
  }

  return entries;
type: javascript
tags: []
comment: Send message to Demisto online users over Email, Slack or both.
enabled: true
args:
- name: medium
  auto: PREDEFINED
  predefined:
  - Slack
  - Email
  - All
  description: Medium to send message over (Slack or Email). Default is email.
- name: message
  required: true
  default: true
  description: Message to send
scripttarget: 0

Send message on online users in Demisto
Send message on online users in Demisto
