Hi all. I'm trying to write a simple Slack bot / N...
# suitescript
b
Hi all. I'm trying to write a simple Slack bot / Netsuite integration. But I'm having trouble with how to approach ajax requests in suitescript. Are there any good example scripts regarding this?
z
Might as well use NS’ out of the box N/https module
(or N/http)
m
Do you mean you’re trying to signal from Slack back to NetSuite
b
Other way around. So when a sales order is created, it sends a post request to a slackbot, which then sends it in the appropriate channel
👍 1
z
You are just using a slack webhook, then?
That should be a pretty simple POST request using the N/https module
Webhooks don’t require authentication
oh but you want to route based on information in the SO?
b
Well the filtering I can do on my server. I tried using the modules, but I think the issue was that my script wasn't set as SS 2.0 initially. I'm completely green to 2.0 scripts/modules 😬
z
@jonny
j
HI Blake, I’ve done something a bit like this.
Just as a test so far but it worked. I set something up to post to a Slack channel whenever someone viewed a transaction. Here’s how it worked. I created a SuiteScript (Workflow Action Script) with a custom action called
slackAttack()
. This action sends a request to a PHP script on my server, which handles sending a message to the Slack channel.
Copy code
function slackAttack() { 
    
    // Build the post message.
    var tx = context.newRecord;
    
    var tran = tx.getValue({fieldId: 'tranid'});
    
    // Who?
    var user = runtime.getCurrentUser();
    
    var message = user.name + ' viewed the transaction ' + tran;
    
    log.debug({title: '', details: message});    	
    
    // ID of the Slack channel to send to.
    var channel = 'asdfasdf';
    
    // URL of the PHP script on my server.
    var slack_url = '<https://path.to.my/slackhandler.php>';
    
    var request_data = {
    	url: slack_url,
    	method: 'POST',
    	data: {message: message, channel: channel }
    };
    
    var req = <http://https.post|https.post>({
    	url: slack_url,
    	body: {type: 'slackattack', message: message, channel: channel }
    });
    
    log.debug({title:'Resp', details:req.body});
    
}
Copy code
/* slackhandler.php */

// What's the message?
$message = $_POST['message'];
        
// What channel is it going to?
$channel = $_POST['channel'];

// Your bot's token generated through the Slack Developer Console.
$user_token = SLACK_OATH_TOKEN_BOT;

// Name of your Slackbot.
$slackbot = 'My SlackBot';
        
// Send the message to the channel.
$ch = curl_init('<https://slack.com/api/chat.postMessage>');
        
$data = http_build_query([
    	'token'=>$user_token,
	'channel'=>$channel,
	'text'=>$message,
	'username'=>slackbot
]);

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$result = curl_exec($ch);
curl_close($ch);
b
@jen I think that's basically what i'm after. This is SS2.0 yes? Thanks!
j
yes SS2.0
my solution requires you to have a PHP script somewhere