|
| 1 | +# Slack Server Mock |
| 2 | +Slack server mock is intended to be used for sub-system testing of Slack applications. |
| 3 | + |
| 4 | +## Running the Server |
| 5 | + |
| 6 | +### Required files |
| 7 | + |
| 8 | +#### settings.yaml |
| 9 | +Create a settings.yaml file based on the [sample file](./settings.yaml). |
| 10 | + |
| 11 | +#### Channels file |
| 12 | +If you wish to return a list of channels, create a JSON file with an array of channel objects. |
| 13 | +At minimum, each entry must have a name and an ID. |
| 14 | + |
| 15 | +In the `settings.yaml` file, set `slack_server.channels_path` to the location of the file. |
| 16 | + |
| 17 | +### Using Poetry |
| 18 | + |
| 19 | +#### Prerequisites |
| 20 | +Install [poetry](https://python-poetry.org/docs/) |
| 21 | + |
| 22 | +#### Installation |
| 23 | +- Download the code and change to the downloaded directory: |
| 24 | + ```bash |
| 25 | + git clone https://github.com/ygalblum/slack-server-mock.git |
| 26 | + cd slack-server-mock |
| 27 | + ``` |
| 28 | +- Install all the requirements using poetry: |
| 29 | + ```bash |
| 30 | + poetry install --no-root |
| 31 | + ``` |
| 32 | +- Run the application: |
| 33 | + ```bash |
| 34 | + poetry run python -m slack_server_mock |
| 35 | + ``` |
| 36 | + |
| 37 | +### Using a Podman/Docker |
| 38 | + |
| 39 | +The server is published in a container image on [quay](https://quay.io/repository/yblum/slack_server_mock). |
| 40 | + |
| 41 | +#### Running the latest version |
| 42 | +```bash |
| 43 | +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 |
| 44 | +``` |
| 45 | + |
| 46 | +#### Channels file |
| 47 | +If you are using a channels file, make sure to mount it into the container as well. |
| 48 | + |
| 49 | +## Configure your Slack application |
| 50 | +In order to make your Slack application connect with the mock server you need to override its `base_url`. |
| 51 | + |
| 52 | +### Socket mode |
| 53 | +Instead of: |
| 54 | +```python |
| 55 | +handler = SocketModeHandler( |
| 56 | + App(token=BOT_TOKEN), |
| 57 | + APP_TOKEN |
| 58 | +) |
| 59 | +``` |
| 60 | + |
| 61 | +Use: |
| 62 | +```python |
| 63 | +handler = SocketModeHandler( |
| 64 | + app=App( |
| 65 | + client=WebClient( |
| 66 | + token=BOT_TOKEN, |
| 67 | + base_url="http://localhost:8888" |
| 68 | + ) |
| 69 | + ), |
| 70 | + app_token=settings.slackbot.app_token |
| 71 | +) |
| 72 | +``` |
| 73 | + |
| 74 | +## Interacting with the application |
| 75 | +The mock server provides an endpoint to send a message to the application, |
| 76 | +wait for the application response and return the accumulated ephemeral messages (if sent) and the response. |
| 77 | + |
| 78 | +### Message payload |
| 79 | +The request payload is a JSON with the key `message` whose value is a string |
| 80 | + |
| 81 | +### Response payload |
| 82 | +The response payload is a JSON with two keys: |
| 83 | + |
| 84 | +- `ephemeral`: List of strings. All accumulated ephemeral message |
| 85 | +- `response`: string. The application's response |
| 86 | + |
| 87 | +### Example |
| 88 | +Send a message using `curl` and see the response: |
| 89 | +```bash |
| 90 | +$ curl http://localhost:8080/message -d'{"message": "hello"}' |
| 91 | +{"answer": "Hello to you too", "ephemeral": ["I'll be right with you"]} |
| 92 | +``` |
0 commit comments