Skip to content
Branch: master
Find file History
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
..
Failed to load latest commit information.
assets
README.md
binaris.yml
function.js
package.json

README.md

Slack π command (NodeJS)

A slack command that returns π to n digits, via a Binaris function.

Table of Contents

  1. Return n digits of π with a Binaris Function
  2. Integrate with Slack

Return n digits of π with a Binaris Function

It is assumed you already have a Binaris account. If you don't have an account yet, worry not, signing up is painless and takes just 2 minutes. Visit Getting Started to find concise instructions for the process.

Note: To complete this tutorial you will need administrator permissions on your Slack workspace.

We'll start by creating the skeleton for our π function.

bn create node8 public_slack_pi_command

It's important the function name begin with public_, this indicates to the Binaris backend that the function should be publicly accessible.

The first change we'll make, is to parse and convert the input payload Slack sends into something usable. Slack sends command payloads as x-url-form-encoded which means we'll need to convert it into a native JavaScript object. Luckily the query-string package from npm can do a lot of our heavy lifting for us.

Note: Binaris will attempt to JSON.parse any incoming payloads, successfully parsed payloads reside in the body field.

+const { parse } = require('query-string');

 exports.handler = async (body, context) => {
+  const parsed = parse(context.body.toString('utf8'));
-  const name = context.request.query.name || body.name || 'World';
-  return `Hello ${name}!`;
 };

Now that we have the payload in a usable form, let's extract the data we need, so we compute π to the number of digits requested from our user.

   const parsed = parse(context.body.toString('utf8'));
+  const numDigits = parseInt(parsed.text, 10);
 };

Now let's add some generic validation

   const numDigits = parseInt(parsed.text, 10);
+
+  // ensure the input is a valid, natural number
+  if (isNaN(numDigits) || numDigits <= 0) {
+    return {
+      text: `Expected non-zero, natural number, received "${parsed.text}"`,
+      response_type: 'ephemeral',
+    };
+  }

+  // enforce a fun limit on the max pi digits
+  if (numDigits > 10) {
+    return {
+      text: "You've already had too much 🥧!",
+      response_type: 'ephemeral',
+    };
+  }
 };

Note: We return the specific fields text & response_type because this is the expected format of an "Error" response in the Slack API.

All that's left to do is return π to the number of digits requested by the user. The npm package pi makes this very convenient.

   }
+  return PI(numDigits - 1);
};

Note: We subtract one from the input value because the first 3 in π is not considered a "digit"

Don't forget to require the package at the top

 const { parse } = require('query-string');
+const PI = require('pi');

 exports.handler = async (body, context) => {

The final state of function.js

const { parse } = require('query-string');
const PI = require('pi');

exports.handler = async (body, context) => {
  // slack unfortunately sends command payloads as x-url-form-encoded
  const parsed = parse(context.body.toString('utf8'));

  // ensure the input is a valid, natural number
  const numDigits = parseInt(parsed.text, 10);
  if (isNaN(numDigits) || numDigits <= 0) {
    return {
      text: `Expected non-zero, natural number, received "${parsed.text}"`,
      response_type: 'ephemeral',
    };
  }

  // enforce a fun limit on the max pi digits
  if (numDigits > 10) {
    return {
      text: "You've already had too much 🥧!",
      response_type: 'ephemeral',
    };
  }
  return PI(numDigits - 1);
};

Now, install the dependencies we used in our function.

$ npm install --save query-string pi

And finally, deploy our function.

$ bn deploy public_slack_pi_command
  Deployed function public_slack_pi_command
  Invoke with one of:
    "bn invoke public_slack_pi_command"
    "curl https://run.binaris.com/v2/run/0123456789/public_slack_pi_command"

Note: Please keep the printed URL, as we will use it during the next step

Integrate with Slack

  1. Create a new app on Slack or have your administrator do it for you.

    https://api.slack.com/apps?new_app=1

    create-app

    I chose to call the app "pi-command" but this is entirely preference

    create-app-confirm

  2. Once your Slack app has been created, navigate to "Slash Commands" on the sidebar

    create-slash-command

    After being clicked, a prompt will open. Feel free to fill in the fields as you see fit. The only field which is non-negotiable is "Request URL". That field needs to contain the URL printed by bn deploy at the end of the last section

    slash-command-confirm

  3. As our final step we need to install our app to the workspace, this allows our command to be called.

    install-app

To test your new command, navigate to slack and try it out

/pi 5

3.1415
You can’t perform that action at this time.