Adding a Connection to Your NND App

Adding a Connection to Your NND App

In this tutorial, we will discuss how to add a Connection parameter to the template NND App generated from the SDK and how to use it to connect to Slack.  More information about creating Connections is available in the Managing Connections on a Nominode support document.  More information about generating the template NND App from the SDK is available in the Creating Your First NND App support document.

Add a Connection Parameter

1.  Locate and edit the General Parameters group in the executable.py file:

      @engine.parameter_group(
            name="general_parameters",
            display_name="General Parameters",
            description="Parameters for Hello World",
            parameter_group=ParameterGroup(
                  Parameter(
                        type=Int(),
                        name="repeat",
                        display_name="Repeat",
                        description="Specify how many times to print.",
                        default=1,
                        required=True,
                  ),
                  Parameter(
                        type=String(),
                        name="name",
                        display_name="Name",
                        description="Specify a name to print instead of World.",
                        required=False,
                  ),
            ),
      )
2.  Add a new Parameter entry with the SlackAPIConnection type and a new string Parameter entry:
      @engine.parameter_group(
            name="general_parameters",
            display_name="General Parameters",
            description="Parameters for Hello World",
            parameter_group=ParameterGroup(
                  Parameter(
                        type=Int(),
                        name="repeat",
                        display_name="Repeat",
                        description="Specify how many times to print.",
                        default=1,
                        required=True,
                  ),
                  Parameter(
                        type=String(),
                        name="name",
                        display_name="Name",
                        description="Specify a name to print instead of World.",
                        required=False,
                  ),
                  Parameter(
                        type=SlackAPIConnection,
                        name="slack_connection",
                        display_name="Slack Connection",
                        description="Select the Slack workspace to use.",
                        required=True,
                  ),
                  Parameter(
                        type=String(),
                        name="channel",
                        display_name="Channel",
                        description="Specify the Slack channel name to print in.",
                        required=True,
                  ),
            ),
      )
3.  The new parameters will render like this in the Nominode:

4.  All of the available Connection types and the parameters contained in each are documented in this section of the develper.nomnomdata.com website.
5.  In this tutorial, the SlackAPIConnection type that we are using has a single parameter named 'token' which we will use later in the app code: 
      SlackAPIConnection = Connection(
            alias="Slack:API:Token",
            description="Slack API token.",
            connection_type_uuid="SLKTK-O2B8D",
            categories=["API", "Slack"],
            parameter_groups=[
                  ParameterGroup(
                        Parameter(
                              name="token",
                              display_name="Token",
                              description="Token string used to connect to Slack REST API's.",
                              type=String(),
                              required=True,
                        ),
                        name="slack_parameters",
                        display_name="Slack Parameters",
                  ),
            ],
      )

Add Code to Use the Connection

6.  Locate and edit the hello_world function in the executable.py file:
      def hello_world(parameters):
            i = 0
            x = f"Hello {parameters.get('name', 'World')}"
            while i < parameters["repeat"]:
                  logger.info(x)
                  i += 1
            return i, x
7.  Add code to send the message to a Slack channel:
      def hello_world(parameters):
            i = 0
            x = f"Hello {parameters.get('name', 'World')}"
            client = slack.WebClient(token=parameters["slack_connection"]["token"])
            while i < parameters["repeat"]:
                  logger.info(x)
                  client.chat_postMessage(channel=parameters["channel"], text=x)
                  i += 1
            return i, x

Add to Imports and Requirements

8.  Locate and edit the import statements at the top of the executable.py file:
      import logging
      from nomnomdata.engine.components import Engine, Parameter, ParameterGroup
      from nomnomdata.engine.parameters import Int, String
9.  Add lines to import the slack package and the Connection classes:
      import logging
      import slack
      from nomnomdata.engine.components import Engine, Parameter, ParameterGroup, Connection
      from nomnomdata.engine.connections import SlackAPIConnection
      from nomnomdata.engine.parameters import Int, String
10.  Add the slackclient package to the requirements.txt file:
      slackclient==2.5.0

    • Related Articles

    • Adding Your NND App to Our Store

      When you deploy an NND App that you have created, it is immediately available to be used on all of the Nominodes within your NND organization.  However, if you want to share your NND App with others outside of your organization, either for free or ...
    • Testing Your NND App

      Local Testing For NND Apps created in Python, we recommend using pytest to locally test the code that you have written.  Follow this link for a general overview on how to install and use pytest.  In this article, we will discuss a few suggestions ...
    • Creating Your First NND App

      In this tutorial, we will be examining the contents of the template NND App generated from the SDK and making it available to Tasks on your Nominode. Prerequisites Miniconda 4.8.3 (or newer, Python 3.7 version*) (*Python 3.7.4 or newer is required.  ...
    • Separating UI Code in Your NND App

      This article explains a best practice for moving different portions of your NND App code into different files.  Specifically it focuses on moving the portion of the code that controls the user interface to a different file, but the techniques ...
    • Using an External Process in Your NND App

      This article explains how to leverage an external process from your NND App.  Specifically it discusses how to use the Scrapy python package.  It is assumed that the reader is already familiar with the information in the Creating Your First NND App ...