You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by mr...@apache.org on 2017/09/13 22:21:36 UTC

[incubator-openwhisk-wskdeploy] branch master updated: Adding a use case on slack (#479)

This is an automated email from the ASF dual-hosted git repository.

mrutkowski pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-openwhisk-wskdeploy.git


The following commit(s) were added to refs/heads/master by this push:
     new a460917  Adding a use case on slack (#479)
a460917 is described below

commit a4609178bfa8b186f02ccf905aaced259ed208a1
Author: Priti Desai <pd...@us.ibm.com>
AuthorDate: Wed Sep 13 15:21:34 2017 -0700

    Adding a use case on slack (#479)
    
    * Adding a use case on slack
    
    * retabing action file
---
 tests/usecases/slack/README.md                | 49 ++++++++++++++++++++++
 tests/usecases/slack/actions/post-to-slack.js | 59 +++++++++++++++++++++++++++
 tests/usecases/slack/deployment.yaml          | 13 ++++++
 tests/usecases/slack/manifest.yaml            | 28 +++++++++++++
 4 files changed, 149 insertions(+)

diff --git a/tests/usecases/slack/README.md b/tests/usecases/slack/README.md
new file mode 100644
index 0000000..9cf3047
--- /dev/null
+++ b/tests/usecases/slack/README.md
@@ -0,0 +1,49 @@
+# Using Slack Package with `wskdeploy`
+
+The [Slack usecase](https://github.com/pritidesai/incubator-openwhisk-wskdeploy/tree/master/tests/usecases/slack) demonstrates how to build an OpenWhisk app to post a sample message to slack every hour using `wskdeploy`.
+
+OpenWhisk comes with a [Slack package](https://github.com/apache/incubator-openwhisk-catalog/blob/master/packages/slack/README.md) which can be used to post messages to slack. For our app to post hourly messages, we need:
+ 
+- [manifest.yaml](https://github.com/apache/incubator-openwhisk-wskdeploy/blob/master/tests/usecases/slack/manifest.yaml)
+- [deployment.yaml](https://github.com/apache/incubator-openwhisk-wskdeploy/blob/master/tests/usecases/slack/deployment.yaml)
+- [Action File](https://github.com/apache/incubator-openwhisk-wskdeploy/blob/master/tests/usecases/slack/src/post-to-slack.js)
+
+All you have to do is export few environment variables with your slack webhook settings in `deployment.yaml` to deploy this app. You can create a new incoming webhook by following step by step instructions from [here](https://github.com/apache/incubator-openwhisk-GitHubSlackBot/blob/master/docs/add-webhook-to-slack.md).
+
+```yaml
+    dependencies:
+        slack-package-to-post-messages:
+            location: /whisk.system/slack
+            inputs:
+                username: $SLACK_USERNAME 
+                url: $SLACK_URL
+                channel: $SLACK_CHANNEL
+```
+
+### Step 1: Deploy
+
+Deploy it using `wskdeploy`:
+
+```
+wskdeploy -m tests/usecases/slack/manifest.yaml -d tests/usecases/slack/deployment.yaml
+```
+
+### Step 2: Verify
+
+```
+$ wsk package get SlackPackage
+$ wsk package get slack-package-to-post-messages
+$ wsk trigger get everyhour 
+$ wsk rule get post-to-slack-every-hour 
+```
+### Step 3: Run
+
+Fire the `everyhour` trigger and notice a new message on your slack channel:
+
+```
+Activation: post-to-slack (9909dd5229e84526bff9902a2cd860df)
+[
+    "2017-09-12T23:05:17.17872899Z  stdout: Hello from WskDeploy!",
+    "2017-09-12T23:05:17.549177677Z stdout: Posted message to slack"
+]
+```
diff --git a/tests/usecases/slack/actions/post-to-slack.js b/tests/usecases/slack/actions/post-to-slack.js
new file mode 100644
index 0000000..787da64
--- /dev/null
+++ b/tests/usecases/slack/actions/post-to-slack.js
@@ -0,0 +1,59 @@
+/**
+  *
+  * main() will be invoked when you Run This Action.
+  *
+  * @param Whisk actions accept a single parameter,
+  *        which must be a JSON object.
+  *
+  * In this case, the params variable will look like:
+  *     {
+  *         "message": "xxxx",
+  *         "slack_package": "xxxx",
+  *     }
+  *
+  * @return which must be a JSON object.
+  *         It will be the output of this action.
+  *
+  */
+
+
+function main(params) {
+    // require the OpenWhisk npm package
+    var openwhisk = require("openwhisk");
+
+    // instantiate the openwhisk instance before you can use it
+    wsk = openwhisk();
+
+    //read Params
+    var message = params.message;
+    var slackPackage = params.slack_package;
+
+    console.log(message);
+
+    // access namespace as environment variables
+    var namespace = process.env["__OW_NAMESPACE"];
+
+    // Slack package can be accessed using /namespace/package
+    packageName = "/" + namespace + "/" + slackPackage;
+
+    return wsk.actions.invoke({
+        actionName: packageName + "/post",
+        params: {
+            "text": message,
+        },
+        blocking: true
+    })
+    .then(activation => {
+        console.log("Posted message to slack");
+        return {
+            message: activation
+        };
+    })
+    .catch(function (err) {
+        console.log("Error posting message to slack")
+        return {
+            error: err
+        };
+    });
+}
+
diff --git a/tests/usecases/slack/deployment.yaml b/tests/usecases/slack/deployment.yaml
new file mode 100644
index 0000000..d8fb8cd
--- /dev/null
+++ b/tests/usecases/slack/deployment.yaml
@@ -0,0 +1,13 @@
+application:
+    name: AppToPostToSlack
+    packages:
+        SlackPackage:
+            actions:
+                post-to-slack:
+                    inputs:
+                        message: "Hello from WskDeploy!"
+                        slack_package: slack-package-to-post-messages
+            triggers:
+                everyhour:
+                    inputs:
+                        cron: "0 */1 * * *"
diff --git a/tests/usecases/slack/manifest.yaml b/tests/usecases/slack/manifest.yaml
new file mode 100644
index 0000000..63ab31e
--- /dev/null
+++ b/tests/usecases/slack/manifest.yaml
@@ -0,0 +1,28 @@
+package:
+    name: SlackPackage
+    dependencies:
+        slack-package-to-post-messages:
+            location: /whisk.system/slack
+            inputs:
+                username: $SLACK_USERNAME
+                url: $SLACK_URL
+                channel: $SLACK_CHANNEL
+    actions:
+        post-to-slack:
+            function: actions/post-to-slack.js
+            runtime: nodejs:6
+            inputs:
+                message:
+                    type: string
+                    description: message to post on slack
+                slack_package:
+                    type: string
+                    description: slack package name
+    triggers:
+        everyhour:
+            feed: /whisk.system/alarms/alarm
+    rules:
+        post-to-slack-every-hour:
+            action: post-to-slack
+            trigger: everyhour
+

-- 
To stop receiving notification emails like this one, please contact
['"commits@openwhisk.apache.org" <co...@openwhisk.apache.org>'].