Python SDK¶
The Python SDK provides Publisher and Consumer classes for interacting with SlackMQ.
Installation¶
Or install from source:
Publishing¶
import os
from slack_sdk import WebClient
from pyslackmq import Publisher
client = WebClient(os.environ["SLACK_BOT_TOKEN"])
pub = Publisher(client, "order-events")
ts = pub.publish("Order ORD-9042 confirmed — $149.99, shipping to Melbourne")
print(f"Published message: ts={ts}")
Consuming¶
import os
from slack_sdk import WebClient
from pyslackmq import Consumer
client = WebClient(os.environ["SLACK_BOT_TOKEN"])
consumer = Consumer(client, "billing")
def handle(delivery):
try:
print(f"Received: {delivery.text} (attempt {delivery.attempt})")
delivery.ack()
except Exception as e:
print(f"Failed: {e}")
delivery.nack()
consumer.consume(handler=handle)
Key Classes¶
Publisher¶
Publisher(client, channel)— create a publisher for a topic channelpublish(text) -> str— post a message, returns the Slack timestamp
Consumer¶
Consumer(client, channel)— create a consumer for a queue channelconsume(handler)— start consuming messages, callinghandler(delivery)for each
Delivery¶
text— the message contentattempt— current delivery attempt numberack()— acknowledge successful processingnack()— signal processing failure