Skip to content

Commit 2e7800d

Browse files
committed
Initial version
0 parents  commit 2e7800d

81 files changed

Lines changed: 2638 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# general things to ignore
2+
build/
3+
dist/
4+
docs/_sources/
5+
docs/.doctrees
6+
.eggs/
7+
*.egg-info/
8+
*.egg
9+
*.py[cod]
10+
__pycache__/
11+
*.so
12+
*~
13+
14+
# virtualenv
15+
env*/
16+
venv/
17+
18+
# codecov / coverage
19+
.coverage
20+
cov_*
21+
coverage.xml
22+
23+
# due to using tox and pytest
24+
.tox
25+
.cache
26+
.pytest_cache/
27+
.python-version
28+
pip
29+
.mypy_cache/
30+
31+
# JetBrains PyCharm settings
32+
.idea/
33+
34+
tmp.txt
35+
.DS_Store
36+
logs/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020- Slack Technologies, Inc
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# Bolt for Python
2+
3+
A Python framework to build Slack apps in a flash with the latest platform features.
4+
5+
## Setup
6+
7+
```bash
8+
python -m venv env
9+
source env/bin/activate
10+
pip install -U pip
11+
pip install slackclient==2.6.2
12+
pip install -i https://test.pypi.org/simple/ slack_bolt==0.1.0
13+
```
14+
15+
## First Bolt App
16+
17+
Create an app by calling a constructor, which is a top-level export.
18+
19+
```python
20+
# app.py
21+
import logging
22+
logging.basicConfig(level=logging.DEBUG)
23+
24+
import os
25+
from slack_bolt import App
26+
27+
app = App(
28+
signing_secret = os.environ["SLACK_SIGNING_SECRET"],
29+
token= os.environ["SLACK_BOT_TOKEN"],
30+
)
31+
32+
@app.shortcut("callback-id-here")
33+
def handle_global_shortcut(ack, client, logger, payload):
34+
ack()
35+
logger.info(payload)
36+
api_response = client.views_open(
37+
trigger_id=payload["trigger_id"],
38+
view={
39+
"type": "modal",
40+
"callback_id": "view-id",
41+
"title": {
42+
"type": "plain_text",
43+
"text": "My App",
44+
},
45+
"submit": {
46+
"type": "plain_text",
47+
"text": "Submit",
48+
},
49+
"blocks": [
50+
{
51+
"type": "input",
52+
"element": {
53+
"type": "plain_text_input"
54+
},
55+
"label": {
56+
"type": "plain_text",
57+
"text": "Label",
58+
}
59+
}
60+
]
61+
})
62+
63+
64+
@app.view("view-id")
65+
def view_submission(ack, payload, logger):
66+
ack()
67+
logger.info(payload)
68+
69+
70+
@app.event("app_mention")
71+
def event_test(ack, say):
72+
ack()
73+
say("What's up?")
74+
75+
76+
if __name__ == '__main__':
77+
app.start(3000) # POST http://localhost:3000/slack/events
78+
```
79+
80+
## Run the Bolt App
81+
82+
```bash
83+
export SLACK_SIGNING_SECRET=***
84+
export SLACK_BOT_TOKEN=xoxb-***
85+
python app.py
86+
87+
ngrok http 3000
88+
```

maintainers_guide.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Deploy to test repository
2+
3+
```
4+
# https://packaging.python.org/guides/using-testpypi/
5+
python -m venv env
6+
source env/bin/activate
7+
pip install --upgrade pip
8+
pip install twine wheel
9+
rm -rf dist/
10+
python setup.py sdist bdist_wheel
11+
twine check dist/*
12+
13+
# Deploy to test repository
14+
twine upload --repository testpypi dist/*
15+
pip install --index-url https://test.pypi.org/simple/ slack_bolt
16+
```

samples/aiohttp/.gitkeep

Whitespace-only changes.

samples/app.py

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt==0.1.0 in requirements.txt
3+
import sys
4+
5+
sys.path.insert(1, "../src")
6+
# ------------------------------------------------
7+
8+
import logging
9+
from slack_bolt import App
10+
from slack_bolt.kwargs_injection import Args
11+
from slack_sdk import WebClient
12+
13+
logging.basicConfig(level=logging.DEBUG)
14+
app = App(process_before_response=True)
15+
16+
17+
@app.middleware # or app.use(log_request)
18+
def log_request(logger, payload, next):
19+
logger.debug(payload)
20+
return next()
21+
22+
23+
@app.command("/bolt-py-proto-2") # or app.command(re.compile(r"/bolt-.+"))(test_command)
24+
def test_command(args: Args):
25+
args.logger.info(args.payload)
26+
respond, ack = args.respond, args.ack
27+
28+
respond(blocks=[
29+
{
30+
"type": "section",
31+
"block_id": "b",
32+
"text": {
33+
"type": "mrkdwn",
34+
"text": "You can add a button alongside text in your message. "
35+
},
36+
"accessory": {
37+
"type": "button",
38+
"action_id": "a",
39+
"text": {
40+
"type": "plain_text",
41+
"text": "Button"
42+
},
43+
"value": "click_me_123"
44+
}
45+
}
46+
])
47+
ack("thanks!")
48+
49+
50+
@app.shortcut("test-shortcut")
51+
def test_shortcut(ack, client: WebClient, logger, payload):
52+
logger.info(payload)
53+
ack()
54+
res = client.views_open(
55+
trigger_id=payload["trigger_id"],
56+
view={
57+
"type": "modal",
58+
"callback_id": "view-id",
59+
"title": {
60+
"type": "plain_text",
61+
"text": "My App",
62+
},
63+
"submit": {
64+
"type": "plain_text",
65+
"text": "Submit",
66+
},
67+
"close": {
68+
"type": "plain_text",
69+
"text": "Cancel",
70+
},
71+
"blocks": [
72+
{
73+
"type": "input",
74+
"element": {
75+
"type": "plain_text_input"
76+
},
77+
"label": {
78+
"type": "plain_text",
79+
"text": "Label",
80+
}
81+
}
82+
]
83+
})
84+
logger.info(res)
85+
86+
87+
@app.view("view-id")
88+
def view_submission(ack, payload, logger):
89+
logger.info(payload)
90+
return ack()
91+
92+
93+
@app.action("a")
94+
def button_click(logger, payload, ack, respond):
95+
logger.info(payload)
96+
respond("respond!")
97+
# say(text="say!")
98+
ack()
99+
100+
101+
@app.event("app_mention")
102+
def event_test(ack, payload, say, logger):
103+
logger.info(payload)
104+
say("What's up?")
105+
return ack()
106+
107+
108+
@app.event({"type": "message", "subtype": "message_deleted"})
109+
def deleted(ack, payload, say):
110+
message = payload["event"]["previous_message"]["text"]
111+
say(f"I've noticed you deleted: {message}")
112+
return ack()
113+
114+
115+
@app.event({"type": "message"})
116+
def new_message(ack, logger, payload, say):
117+
logger.info(payload)
118+
# say(f"I've noticed you deleted {payload}")
119+
return ack()
120+
121+
122+
if __name__ == '__main__':
123+
app.start(3000)
124+
125+
# pip install -r requirements.txt
126+
# export SLACK_SIGNING_SECRET=***
127+
# export SLACK_BOT_TOKEN=xoxb-***
128+
# python app.python

samples/aws_lambda/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
src/

samples/aws_lambda/aws_lambda.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# ------------------------------------------------
2+
# instead of slack_bolt==0.1.0 in requirements.txt
3+
import sys
4+
5+
sys.path.insert(1, "src")
6+
# ------------------------------------------------
7+
8+
import logging
9+
from slack_bolt import App
10+
from slack_bolt.adapter.aws_lambda import SlackRequestHandler
11+
12+
logging.basicConfig(format='%(asctime)s %(message)s', level=logging.DEBUG)
13+
app = App(process_before_response=True)
14+
15+
16+
@app.command("/bolt-py-proto")
17+
def command_handler(logger, payload, client, ack):
18+
res = client.views_open(
19+
trigger_id=payload["trigger_id"],
20+
view={
21+
"type": "modal",
22+
"callback_id": "view-id",
23+
"title": {
24+
"type": "plain_text",
25+
"text": "My App",
26+
},
27+
"submit": {
28+
"type": "plain_text",
29+
"text": "Submit",
30+
},
31+
"close": {
32+
"type": "plain_text",
33+
"text": "Cancel",
34+
},
35+
"blocks": [
36+
{
37+
"type": "input",
38+
"element": {
39+
"type": "plain_text_input"
40+
},
41+
"label": {
42+
"type": "plain_text",
43+
"text": "Label",
44+
}
45+
}
46+
]
47+
})
48+
logger.info(res)
49+
return ack("Thanks!")
50+
51+
52+
@app.view("view-id")
53+
def view_submission(ack, payload, logger):
54+
logger.info(payload)
55+
return ack()
56+
57+
58+
def handler(event, context):
59+
return SlackRequestHandler(app).handle(event, context)
60+
61+
# export SLACK_SIGNING_SECRET=***
62+
# export SLACK_BOT_TOKEN=xoxb-***
63+
# cp -pr ../../src src
64+
# pip install python-lambda
65+
# lambda deploy --config-file aws_lambda_config.yaml --requirements requirements.txt

0 commit comments

Comments
 (0)