Search code examples
node.jsibm-cloudslackopenwhiskibm-cloud-functions

IBM Cloud Functions / OpenWhisk Slack package and message attachments


I am trying to use the Slack package available for IBM Cloud Functions and OpenWhisk. I created a Node.js action that produces a JSON object with text and attachments values. The object is passed along in a sequence that uses the post method of the Slack package. The message itself is shown when posted via Incoming Webhook, the attachment is not. Why? What needs to be changed?

return {text : "regular message text", attachments: [
       { fallback: "my fallback message",
         title: "some nice title",
         mrkdwn_in: ["text"],
         text : "Simple text"}
        ]};

The action sequence is created this way, webhook and username are bound following the steps in the documentation:

ibmcloud fn action update mySequence --sequence myAction,mySlack/post

I checked the source code for the post action and it stringifies the attachments array.


Solution

  • I ended up writing it on my own for a Cloud Functions action that posts statistics.

    // now compose the payload and post it to Slack
     var payload= {
        text : resString,
        channel : channel,
        attachments: [
           {
             fallback: "Weekly top 25 repositories",
             title: "Top 25 repositories by unique views ("+workweek+")",
             mrkdwn_in: ["text"],
             text : dataString
            }
            ]
          };
    
     var options = {
      method: 'POST',
      uri: webhook,
      body: payload,
      resolveWithFullResponse: true,
      json: true // Automatically stringifies the body to JSON
    };
    
    // return response to the request, so the status will be logged
    return rp(options).then(function(response) {
      return response;
    });
    

    Straight forward and works well since weeks.