slackbot/handler.js
// Helper function to make the actual slack request
Helper function to make the actual slack request
Helper function to make the actual slack request
function processEvent(event, callback) {
  const obj = JSON.parse(event.Records[0].Sns.Message);
  console.log('in processevent, obj is: ' + JSON.stringify(obj));
  if (typeof obj.slackMessage !== 'undefined') {
    var slackMessage = obj.slackMessage;
    console.log('sending following slackMessage: ' + JSON.stringify(slackMessage));
    postMessage(slackMessage, (response) => {
      if (response.statusCode < 400) {
        console.info('Message posted successfully');
        callback(null);
      } else if (response.statusCode < 500) {
        console.error(`Error posting message to Slack API: ${response.statusCode} - ${response.statusMessage}`);
        callback(null); 
      } else {
        callback(`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`);
      }
    });
  } else {
    obtainAccount(obj.client_order_id, function(account) {
      var trade = obj;
      console.log('got trade: ' + JSON.stringify(trade));
      var color, fallback, title;
      var fields = [];
      if (trade.hedge) {
        color = "danger";
        fallback = "HedgieBot " + trade.client_order_id + " hedged @ " + trade.avg_execution_price;
        title = "HedgieBot " + trade.client_order_id + " has hedged @" + trade.avg_execution_price;
        fields.push({
          "title": "Fund Value",
          "value": '$' + Number(account.accountBalanceUSD).toFixed(2) + ' / ' + '$' + Number(account.targetAmountUsd).toFixed(2),
          "short": false
        });
        fields.push({
          "title": "Siphoned Profit",
          "value": '$' + Number(account.profitUSD).toFixed(2),
          "short": true
        });
      } else if (trade.side === 'sell') {
        color = "good";
        fallback = "HedgieBot " + trade.client_order_id + " sold for a profit @ " + trade.avg_execution_price;
        title = "HedgieBot " + trade.client_order_id + " has profited @ " + trade.avg_execution_price + "!! Congrats!";
        fields.push({
          "title": "Fund Value",
          "value": '$' + Number(account.accountBalanceUSD).toFixed(2) + ' / ' + '$' + Number(account.targetAmountUsd).toFixed(2),
          "short": false
        });
        fields.push({
          "title": "Siphoned Profit",
          "value": '$' + Number(account.profitUSD).toFixed(2) + '',
          "short": true
        });
      } else {
        color = "#439FE0";
        fallback = "HedgieBot " + trade.client_order_id + " bought @ " + trade.avg_execution_price;
        title = "HedgieBot " + trade.client_order_id + " has bought @ " + trade.avg_execution_price;
        if (typeof(account.hoursToUpdate) !== 'undefined') {
          fields.push({
            "title": "Activity Setting",
            "value": account.hoursToUpdate + '',
            "short": true
          });
        }
      }
      if (typeof(account.riskFactor) !== 'undefined') {
        fields.push({
          "title": "Risk Factor",
          "value": account.riskFactor + '',
          "short": true
        });
      }
      const slackMessage = {
        channel: slackChannel,
        attachments: [{
          "fallback": fallback,
          "color": color,
          "title": title,
          "fields": fields,
          "ts": Math.round((new Date()).getTime() / 1000)
        }]
      };
      console.log('sending following slackMessage: ' + JSON.stringify(slackMessage));
      postMessage(slackMessage, (response) => {
        if (response.statusCode < 400) {
          console.info('Message posted successfully');
          callback(null);
        } else if (response.statusCode < 500) {
          console.error(`Error posting message to Slack API: ${response.statusCode} - ${response.statusMessage}`);
          callback(null); 
        } else {
          callback(`Server error when processing message: ${response.statusCode} - ${response.statusMessage}`);
        }
      });
    });
  }
}
function processEvent ( event , callback ) { const obj = JSON . parse ( event . Records [ 0 ] . Sns . Message ) ; console . log ( 'in processevent, obj is: ' + JSON . stringify ( obj ) ) ; if ( typeof obj . slackMessage !== 'undefined' ) { var slackMessage = obj . slackMessage ; console . log ( 'sending following slackMessage: ' + JSON . stringify ( slackMessage ) ) ; postMessage ( slackMessage , ( response ) => { if ( response . statusCode < 400 ) { console . info ( 'Message posted successfully' ) ; callback ( null ) ; } else if ( response . statusCode < 500 ) { console . error ( ` ${ response . statusCode } ${ response . statusMessage } ` ) ; callback ( null ) ; } else { callback ( ` ${ response . statusCode } ${ response . statusMessage } ` ) ; } } ) ; } else { obtainAccount ( obj . client_order_id , function ( account ) { var trade = obj ; console . log ( 'got trade: ' + JSON . stringify ( trade ) ) ; var color , fallback , title ; var fields = [ ] ; if ( trade . hedge ) { color = "danger" ; fallback = "HedgieBot " + trade . client_order_id + " hedged @ " + trade . avg_execution_price ; title = "HedgieBot " + trade . client_order_id + " has hedged @" + trade . avg_execution_price ; fields . push ( { "title" : "Fund Value" , "value" : '$' + Number ( account . accountBalanceUSD ) . toFixed ( 2 ) + ' / ' + '$' + Number ( account . targetAmountUsd ) . toFixed ( 2 ) , "short" : false } ) ; fields . push ( { "title" : "Siphoned Profit" , "value" : '$' + Number ( account . profitUSD ) . toFixed ( 2 ) , "short" : true } ) ; } else if ( trade . side === 'sell' ) { color = "good" ; fallback = "HedgieBot " + trade . client_order_id + " sold for a profit @ " + trade . avg_execution_price ; title = "HedgieBot " + trade . client_order_id + " has profited @ " + trade . avg_execution_price + "!! Congrats!" ; fields . push ( { "title" : "Fund Value" , "value" : '$' + Number ( account . accountBalanceUSD ) . toFixed ( 2 ) + ' / ' + '$' + Number ( account . targetAmountUsd ) . toFixed ( 2 ) , "short" : false } ) ; fields . push ( { "title" : "Siphoned Profit" , "value" : '$' + Number ( account . profitUSD ) . toFixed ( 2 ) + '' , "short" : true } ) ; } else { color = "#439FE0" ; fallback = "HedgieBot " + trade . client_order_id + " bought @ " + trade . avg_execution_price ; title = "HedgieBot " + trade . client_order_id + " has bought @ " + trade . avg_execution_price ; if ( typeof ( account . hoursToUpdate ) !== 'undefined' ) { fields . push ( { "title" : "Activity Setting" , "value" : account . hoursToUpdate + '' , "short" : true } ) ; } } if ( typeof ( account . riskFactor ) !== 'undefined' ) { fields . push ( { "title" : "Risk Factor" , "value" : account . riskFactor + '' , "short" : true } ) ; } const slackMessage = { channel : slackChannel , attachments : [ { "fallback" : fallback , "color" : color , "title" : title , "fields" : fields , "ts" : Math . round ( ( new Date ( ) ) . getTime ( ) / 1000 ) } ] } ; console . log ( 'sending following slackMessage: ' + JSON . stringify ( slackMessage ) ) ; postMessage ( slackMessage , ( response ) => { if ( response . statusCode < 400 ) { console . info ( 'Message posted successfully' ) ; callback ( null ) ; } else if ( response . statusCode < 500 ) { console . error ( ` ${ response . statusCode } ${ response . statusMessage } ` ) ; callback ( null ) ; } else { callback ( ` ${ response . statusCode } ${ response . statusMessage } ` ) ; } } ) ; } ) ; } }
Helper function to make the actual slack request
Helper function to make the actual slack request
//provide a way to directly send a message // Don't retry because the error is due to a problem with the request // Let Lambda retry //loss //profit //buy // Don't retry because the error is due to a problem with the request // Let Lambda retry