MCPcopy
hub / github.com/slackapi/python-slack-sdk / slack_app

Function slack_app

integration_tests/samples/basic_usage/views_2.py:34–91  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

32
33@app.route("/slack/events", methods=["POST"])
34def slack_app():
35 if not signature_verifier.is_valid_request(request.get_data(), request.headers):
36 return make_response("invalid request", 403)
37
38 if "payload" in request.form:
39 payload = json.loads(request.form["payload"])
40 if payload["type"] == "shortcut" and payload["callback_id"] == "test-shortcut":
41 # Open a new modal by a global shortcut
42 try:
43 view = View(
44 type="modal",
45 callback_id="modal-id",
46 title=PlainTextObject(text="Awesome Modal"),
47 submit=PlainTextObject(text="Submit"),
48 close=PlainTextObject(text="Cancel"),
49 blocks=[
50 InputBlock(
51 block_id="b-id",
52 label=PlainTextObject(text="Input label"),
53 element=PlainTextInputElement(action_id="a-id"),
54 )
55 ],
56 )
57 api_response = client.views_open(
58 trigger_id=payload["trigger_id"],
59 view=view,
60 )
61 return make_response("", 200)
62 except SlackApiError as e:
63 code = e.response["error"]
64 return make_response(f"Failed to open a modal due to {code}", 200)
65
66 if payload["type"] == "view_submission" and payload["view"]["callback_id"] == "modal-id":
67 # Handle a data submission request from the modal
68 submitted_data = payload["view"]["state"]["values"]
69 print(submitted_data) # {'b-id': {'a-id': {'type': 'plain_text_input', 'value': 'your input'}}}
70 return make_response(
71 jsonify(
72 {
73 "response_action": "update",
74 "view": View(
75 type="modal",
76 callback_id="modal-id",
77 title=PlainTextObject(text="Accepted"),
78 close=PlainTextObject(text="Close"),
79 blocks=[
80 SectionBlock(
81 block_id="b-id",
82 text=PlainTextObject(text="Thanks for submitting the data!"),
83 )
84 ],
85 ).to_dict(),
86 }
87 ),
88 200,
89 )
90
91 return make_response("", 404)

Callers

nothing calls this directly

Calls 8

ViewClass · 0.90
PlainTextObjectClass · 0.90
InputBlockClass · 0.90
SectionBlockClass · 0.90
is_valid_requestMethod · 0.45
views_openMethod · 0.45
to_dictMethod · 0.45

Tested by

no test coverage detected