readme.md
1# Using the Slack object classes
2
3## Composing Messages
4
5Messages are built up out of blocks and legacy attachments. Blocks are composed of the base Block classes in `blocks.py`, which themselves are composed of elements (`elements.py`) which are either atomic or contain common sub-objects (`objects.py`).
6
7For example: A simple block template, containing a header, some fields, and an actions block at the bottom would be built up as follows:
8
9```python
10from slack.web.client import WebClient
11from slack.web.classes import messages, blocks, elements
12
13client = WebClient(token="abc")
14
15fields = blocks.SectionBlock(fields=["*Type:*\nComputer", "*Reason:*\nAll vowel keys aren't working"])
16
17approve_button = elements.ButtonElement(text="Approve", action_id="approval", value="order_123", style="primary")
18deny_button = elements.ButtonElement(text="Deny", action_id="denial", value="order_123", style="danger")
19
20buttons = [approve_button, deny_button]
21
22actions = blocks.ActionsBlock(elements=buttons)
23
24work_order_message = messages.Message(text="You have a new request", blocks=[fields, actions])
25
26client.chat_postMessage(channel="C12345", **work_order_message.to_dict())
27```
28
29## Composing Dialogs
30Dialogs can be built using a helper 'builder' class, to simplify keeping track of required fields.
31
32```python
33from slack.web.client import WebClient
34from slack.web.classes import dialogs
35
36builder = (
37 dialogs.DialogBuilder()
38 .title("My Cool Dialog")
39 .callback_id("myCoolDialog")
40 .state({'value': 123, 'key': "something"})
41 .conversation_selector(name="target", label="Choose Target")
42 .text_area(name="message", label="Message", hint="Enter a message", max_length=500)
43 .text_field(name="signature", label="Signature", optional=True, max_length=50)
44)
45
46client = WebClient(token="abc")
47
48client.dialog_open(dialog=builder.to_dict(), trigger_id="123458.12355")
49```
50