Skip to content

Instantly share code, notes, and snippets.

@cjdinger
Created July 22, 2016 20:54

Revisions

  1. cjdinger created this gist Jul 22, 2016.
    147 changes: 147 additions & 0 deletions publishToSlack.sas
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,147 @@
    /* Example code publish to Slack channel */
    /* Author: Chris Hemedinger */
    /* See explanation at: */
    /* http://blogs.sas.com/content/sasdummy/slack-channel-with-sas/ */


    %macro echoResp(fn=);
    data _null_;
    infile &fn;
    input;
    put _infile_;
    run;
    %mend;

    /* Replace with your own Incoming Webhook, which you add from a Slack configuration */
    %let webhookUrl = https://hooks.slack.com/services/<your webhook URL here!>;

    /*
    Simple example with direct text
    */

    filename resp temp;
    proc http
    url="&webhookUrl"
    method="POST"
    /* IN= supports text in SAS 9.4m3. Earlier release? Use fileref with content */
    in='payload={"channel": "#fromsas",
    "username": "sasprogram",
    "text": "Created using PROC HTTP!"}'
    out=resp
    ;
    run;

    /*
    More complex messages, with multiple parts.
    Use the attachments fields that Slack supports
    */
    filename rich temp;

    data _null_;
    file rich;
    infile datalines4;
    input;
    put _infile_;
    datalines4;
    payload=
    {
    "channel": "#fromsas",
    "username": "sasprogram",
    "icon_emoji": ":fax:",
    "attachments":[
    {
    "fallback":"New SAS Dummy post!: <http://blogs.sas.com/content/sasdummy|The SAS Dummy blog>",
    "pretext":"New SAS Dummy post!: <http://blogs.sas.com/content/sasdummy|The SAS Dummy blog>",
    "color":"#3030F0",
    "fields":[
    {
    "title":"Great news!",
    "value":"That Chris...he's done it again!",
    "short":false
    }
    ]
    }
    ]
    }
    ;;;;

    proc http
    url="&webhookUrl"
    method="POST"
    in=rich
    out=resp
    ;
    run;

    %echoResp(fn=resp);

    /*
    And finally an example that publishes values from data!
    */

    /* Calculate some data */
    proc means data=sashelp.class noprint;
    var age;
    output out=stats;
    run;

    /* file to hold the JSON payload */
    filename msg temp;

    /* Create the start of the JSON payload */
    data _null_;
    file msg ;
    infile datalines4;
    input;
    put _infile_;
    datalines4;
    payload=
    {
    "channel": "#fromsas",
    "username": "sasprogram",
    "icon_emoji": ":fax:",
    "attachments":[
    {
    "fallback":"Latest Stats for AGE in SASHELP.CLASS",
    "pretext":"Latest Stats for AGE in SASHELP.CLASS",
    "color":"#D000FF",
    "fields":[
    ;;;;

    /* fill in the data fields in the middle */
    data _null_;
    /* mod option will append to the file */
    file msg mod;
    set stats end=eof;
    put '{ "short":false, "title": "' _stat_ '",';
    put '"value": "' age '" }';
    /* separate values with commas, except the last */
    if not eof then put ",";
    run;

    /*
    And finish with the tail end of the payload
    */

    data _null_;
    /* mod option will append to the file */
    file msg mod;
    infile datalines4;
    input;
    put _infile_;
    datalines4;
    ]
    }
    ]
    }
    ;;;;

    proc http
    url="&webhookUrl"
    method="POST"
    in=msg
    out=resp
    ;
    run;

    %echoResp(fn=resp);