-
Composing a Message
Messages in PyBlock Builder are naturally constructed using the Message class from the surfaces module.
The following is an example of how to compose a barebones message:
from pyblock_builder.surfaces import Message
(Message()
.set_text("I'm posting this message with PyBlock Builder!") # main body text of the message
.set_channel("C12345") # ID of the channel for posting or user ID for DM
)
*Note that the () wrapping the code constructing the instance of the Message object is required by PyCharm and other IDEs to ensure proper indentation for method chaining and serves no functional purpose in PyBlock Builder.
A more complex message may, of course, include—you guessed it—blocks!
from pyblock_builder.surfaces import Message
from pyblock_builder.blocks import Section, Divider, Actions
from pyblock_builder.elements import Button
from pyblock_builder import mrkdwn as md
(Message()
.add_blocks(
Section()
.set_text(f"Johnny, what can you make out of this? {md.emoji('airplane')}"),
Divider(),
Actions()
.add_elements(
Button()
.set_label(f"{md.emoji('tophat')} Hat")
.set_value("hat-button")
.set_action_id("hat_button_pressed")
.set_style("primary"), # colors a button green
Button()
.set_label(f"{md.emoji('gem')} Broach")
.set_value("broach-button")
.set_action_id("broach_button_pressed")
.danger(), # colors a button red; alternative to using set_style("danger")
Button()
.set_label(f"{md.emoji('lizard')} Pterodactyl")
.set_value("pterodactyl-button")
.set_action_id("pterodactyl_button_pressed"),
)
)
.set_text("Johnny, what can you make of this?") # when blocks are used this becomes a fallback string for display in notifcations
.set_channel("C12345") # ID of the channel for posting or user ID for DM
)
-
Posting and Scheduling Messages
While the above examples show how simple and intuitive it is to construct a message using PyBlock Builder,
Message objects are also equipped with some handy features that allow for easier posting and scheduling when
using Slack's Bolt for Python SDK.
# Simple demonstration of how to post a message with Slack's Bolt for Python SDK and PyBlock Builder
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from pyblock_builder.surfaces import Message
# Initializes your app with your bot token and socket mode handler
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
(Message()
.set_text("I'm posting this message using PyBlock Builder!") # main body text of the message
.set_channel("C12345") # ID of the channel for posting or user ID for DM
.post(app.client)
)
# Start your app
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
Output:
As demonstrated above, posting our first messsage example from above with PyBlock Builder is as simple as passing the WebClient provided to
your Bolt app as app.client to the post() method of the Message object. PyBlock Builder takes care of the
rest for you! Now, let's try scheduling our more complex message example to be sent at a certain time:
# Simple demonstration of how to schedule a message with Slack's Bolt for Python SDK and PyBlock Builder
import os
from datetime import datetime
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from pyblock_builder.surfaces import Message
from pyblock_builder.blocks import Section, Divider, Actions
from pyblock_builder.elements import Button
from pyblock_builder import mrkdwn as md
# Initializes your app with your bot token and socket mode handler
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
# Create a datetime object representing the date and time for posting
when_to_post = datetime(2023, 11, 29, 15, 59)
(Message()
.add_blocks(
Section()
.set_text(f"Johnny, what can you make out of this? {md.emoji('airplane')}"),
Divider(),
Actions()
.add_elements(
Button()
.set_label(f"{md.emoji('tophat')} Hat")
.set_value("hat-button")
.set_action_id("hat_button_pressed")
.set_style("primary"), # colors a button green
Button()
.set_label(f"{md.emoji('gem')} Broach")
.set_value("broach-button")
.set_action_id("broach_button_pressed")
.danger(), # colors a button red; alternative to using set_style("danger")
Button()
.set_label(f"{md.emoji('lizard')} Pterodactyl")
.set_value("pterodactyl-button")
.set_action_id("pterodactyl_button_pressed"),
)
)
.set_text("Johnny, what can you make of this?") # when blocks are used this becomes a fallback string for display in notifcations
.set_channel("C12345") # ID of the channel for posting or user ID for DM
.post_at(when_to_post)
.post(app.client)
)
# Start your app
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
Output:
That's it! Scheduling messages is as easy as using the post_at() method to set the date and time you wish your app to
send the message before calling the post() method. For even more convenience, post_at() accepts values in the form
of a UNIX timestamp as a string or as a Python datetime object as shown above.
*Note that it is still possible to use PyBlock Builder without taking advantage of the methods optimized for use with Bolt for Python SDK by simply assigning an instance of a Message (or any other surfaces) object to a variable and passing its view payload using the .view attribute. The JSON payloads of blocks, block elements, or composition objects can similarly be accessed using the .block attribute (for the former) or the .json attribute (for the latter two) as needed.
-
Updating and Deleting Messages
Posting and scheduling messages is great, but what if you want to want to update or delete a message that your app has
already sent? Fear not! PyBlock Builder has you covered!
"""
Simple demonstration of how to post and then update a message
with Slack's Bolt for Python SDK and PyBlock Builder
"""
import os
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler
from pyblock_builder.surfaces import Message
# Initializes your app with your bot token and socket mode handler
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))
# Post a message and catch the response from the Slack API to reference its ID for updating later
response = (Message()
.set_text("Hey, that's a nice message...")
.set_channel("C12345")
.post(app.client))
update_message_ts = response['message']['ts'] # also accessible via response['ts']
# Start your app
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
Output:
Now, all we have to do to update our message is pass in its ts ID which we've parsed from the Slack API response and
stored in update_message_ts using the set_ts() method when constructing a new Message object and then call the
update() method instead of the post() method.
# ...
update_message_ts = response['message']['ts'] # also accessible via response['ts']
(Message()
.set_channel("C12345")
.set_ts(update_message_ts)
.set_text("It'd be a shame if someone updated it!")
.update(app.client))
# Start your app
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()
Output:

While updating an existing message requires the use of either set_text() or add_blocks() to define the new content
for display in the message, deleting a message requires only the use of set_channel() and set_ts():
# ...
delete_message_ts = response['message']['ts'] # also accessible via response['ts']
(Message()
.set_channel("C12345")
.set_ts(delete_message_ts)
.delete(app.client))
# Start your app
if __name__ == "__main__":
SocketModeHandler(app, os.environ["SLACK_APP_TOKEN"]).start()