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