Skip to content

Mock Slack HTTP and WebSocker server for sub-system testing purposes

License

Notifications You must be signed in to change notification settings

ygalblum/slack-server-mock

Repository files navigation

Slack Server Mock

Slack server mock is intended to be used for sub-system testing of Slack applications.

Running the Server

Required files

settings.yaml

Create a settings.yaml file based on the sample file.

Channels file

If you wish to return a list of channels, create a JSON file with an array of channel objects. At minimum, each entry must have a name and an ID.

In the settings.yaml file, set slack_server.channels_path to the location of the file.

Using Poetry

Prerequisites

Install poetry

Installation

  • Download the code and change to the downloaded directory:
    git clone https://github.com/ygalblum/slack-server-mock.git
    cd slack-server-mock
  • Install all the requirements using poetry:
    poetry install --no-root
  • Run the application:
    poetry run python -m slack_server_mock

Using a Podman/Docker

The server is published in a container image on quay.

Running the latest version

podman run --rm -it -d --name slack_server_mock --publish 3001:3001 --publish 8080:8080 --publish 8888:8888 --volume ${PWD}/settings.yaml:/app/settings.yaml:z quay.io/yblum/slack_server_mock:latest

Channels file

If you are using a channels file, make sure to mount it into the container as well.

Configure your Slack application

In order to make your Slack application connect with the mock server you need to override its base_url.

Socket mode

Instead of:

handler = SocketModeHandler(
    App(token=BOT_TOKEN),
    APP_TOKEN
)

Use:

handler = SocketModeHandler(
    app=App(
        client=WebClient(
            token=BOT_TOKEN,
            base_url="http://localhost:8888"
        )
    ),
    app_token=settings.slackbot.app_token
)

Interacting with the application

The mock server provides an endpoint to send a message to the application, wait for the application response and return the accumulated ephemeral messages (if sent) and the response.

Message payload

The request payload is a JSON with the key message whose value is a string

Response payload

The response payload is a JSON with two keys:

  • ephemeral: List of strings. All accumulated ephemeral message
  • response: string. The application's response

Example

Send a message using curl and see the response:

$ curl http://localhost:8080/message -d'{"message": "hello"}'
{"answer": "Hello to you too", "ephemeral": ["I'll be right with you"]}