Adding a Connection to Your Nom Nom App

Adding a Connection to Your Nom Nom App

In this tutorial, we will discuss how to add a Connection parameter to the template Nom Nom 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 Nom Nom App from the SDK is available in the Creating Your First Nom Nom 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 Nom Nom App to Our Store

      When you deploy a Nom Nom App that you have created, it is immediately available to be used on all of the Nominodes within your Nom Nom Data organization.  However, if you want to share your Nom Nom App with others outside of your organization, ...
    • Testing Your Nom Nom App

      Local Testing For Nom Nom 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 Nom Nom App

      In this tutorial, we will be examining the contents of the template Nom Nom App generated from the SDK and making it available to Tasks on your Nominode. Prerequisites Python 3.7.4 (or newer) https://www.python.org/downloads/ Docker 19.03 (or newer) ...
    • Using an External Process in Your Nom Nom App

      This article explains how to leverage an external process from your Nom Nom 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 Nom ...
    • Reporting and Maintaining Progress in Your Nom Nom App

      This article explains the best practices for reporting Task progress in your Nom Nom App code and for updating a parameter value in a Task from your Nom Nom App code so that you can maintain progress across multiple Task executions.  Specifically it ...