Skip to content

Commit 1c62f67

Browse files
committed
Add python project which can be used as a slash command.
1 parent 4ddf858 commit 1c62f67

File tree

5 files changed

+82
-0
lines changed

5 files changed

+82
-0
lines changed

jokes/README.md

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
## Using Python to implement a slash command
2+
3+
The most convenient way to create a slash command to use with [Nimbella Commander](https://nimbella.com/integrations/commander) is to run `/nc command_create` and implementing the command in the provided editor. However this is not convenient for commands that require third party libraries, or when you want to use source control to manage your code.
4+
5+
An alternate approach is to use the [Nimbella CLI called `nim`](https://docs.nimbella.com/install). With `nim` you can develop and test your commands locally, commit the code to GitHub (or your favorite source control), and then deploy the project either by running `nim project deploy` yourself, or using a CI/CD process (e.g., GitHub actions).
6+
7+
This `jokes` project is an example to illustrate how to create a slash command using Python as the programming language. The command generates jokes using the [`pyjokes`](https://pypi.org/project/pyjokes/) library.
8+
9+
## Project organization
10+
11+
There is only one command in this project. It is implemented by the `joke` API in [./packages/default/joke/__main__.py](./packages/default/joke/__main__.py). The code imports `pyjokes` (installed via [`requirements.txt`](./packages/default/joke/requirements.txt)), generates a new joke and formats the response for Commander.
12+
13+
## Installing the project with `nim`
14+
15+
Before creating the slash command, we need to deploy the project. This is usually done with `/nc csm_install` from your messaging environment (e.g., Slack or Microsoft Teams). However `csm_install` currently does not support Python implementations of commands. So to accomplish the same goals, we will create the API ourselves:
16+
17+
```bash
18+
nim project deploy /path/to/demo-projects/jokes
19+
```
20+
21+
The output from `nim project deploy` will resemble the following:
22+
```
23+
> nim project deploy jokes
24+
Warning: found commands.yaml but no project.yml
25+
Deploying project '/path/to/projects/my-command-sets/jokes'
26+
to namespace 'your-namespace'
27+
on host 'https://your-host.nimbella.io'
28+
Started running ./build.sh in /path/to/demo-projects/jokes/default/joke
29+
Finished running ./build.sh in /path/to/demo-projects/jokes/default/joke
30+
Deployment status recorded in 'jokes/.nimbella'
31+
32+
Deployed actions ('nim action get <actionName> --url' for URL):
33+
- joke
34+
```
35+
36+
The API is now live. We will need the URL for the API to use with [Commander as a webhook](https://nimbella.com/docs/commander/slack/reference#command_webhook). Running the command shown at the end of the project deploy command will produce the API endpoint you need.
37+
38+
```
39+
nim action get joke --url
40+
```
41+
42+
**Pro tip:** You can run `nim project deploy github:nimbella/demo-projects/jokes` to install the project directly from GitHub without cloning this project. The CLI may prompt you to login with GitHub to work around GitHub API rate limits.
43+
44+
## Creating the slash command
45+
46+
Now you can create the command in your messaging environment. In Slack or Microsoft teams you will issue the following commands:
47+
```
48+
/nc command_create joke
49+
/nc command_webhook joke <joke-url-from-previous-step>
50+
```
51+
52+
Note the second command which binds the slash command `joke` to the URL of your API. This invokes your API every time your command runs.
53+
54+
## Run your new command
55+
56+
You are ready to use the slash command now. You can run it, and administer it like any other slash command.
57+
```
58+
/nc joke
59+
ASCII stupid question, get a stupid ANSI.
60+
```

jokes/commands.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
commands:
2+
joke:
3+
description: joke generator
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pyjokes
2+
3+
def main(args):
4+
joke = pyjokes.get_joke()
5+
return {
6+
'body': {
7+
'response_type': 'in_channel',
8+
'text': joke
9+
}
10+
}

jokes/packages/default/joke/build.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
set -e
4+
5+
virtualenv virtualenv
6+
source virtualenv/bin/activate
7+
pip install -r requirements.txt
8+
deactivate
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pyjokes==0.6.0

0 commit comments

Comments
 (0)