0
# Event Adapter
1
2
Core functionality for creating and managing Slack event adapters. The SlackEventAdapter class provides the main interface for receiving Slack events and integrating with Flask web servers.
3
4
## Capabilities
5
6
### SlackEventAdapter Creation
7
8
Creates a new Slack event adapter instance with signature verification and optional server binding.
9
10
```python { .api }
11
class SlackEventAdapter(BaseEventEmitter):
12
def __init__(self, signing_secret, endpoint="/slack/events", server=None, **kwargs):
13
"""
14
Initialize the Slack event adapter.
15
16
Parameters:
17
- signing_secret: str, required - Slack app's signing secret for request verification
18
- endpoint: str, optional - URL endpoint for receiving events (default: "/slack/events")
19
- server: Flask/Blueprint/LocalProxy, optional - Existing server instance to bind to
20
- **kwargs: Additional arguments passed to the SlackServer constructor (unused when server is provided)
21
22
Raises:
23
- ValueError: If signing_secret is None
24
- TypeError: If server is not a Flask, Blueprint, or LocalProxy instance
25
"""
26
```
27
28
### Built-in Server Management
29
30
Starts and manages the built-in Flask server for standalone usage.
31
32
```python { .api }
33
def start(self, host='127.0.0.1', port=None, debug=False, **kwargs):
34
"""
35
Start the built-in webserver.
36
37
Parameters:
38
- host: str, optional - Host to bind the webserver to (default: '127.0.0.1')
39
- port: int, optional - Port number for the webserver (default: None, which uses Flask's default of 5000)
40
- debug: bool, optional - Enable debug level logging (default: False)
41
- **kwargs: Additional arguments passed to Flask.run()
42
43
Returns:
44
None
45
"""
46
```
47
48
### Public Attributes
49
50
The SlackEventAdapter provides access to key configuration and state through public attributes.
51
52
```python { .api }
53
signing_secret: str
54
"""The signing secret used for HMAC signature verification of incoming requests."""
55
56
server: SlackServer
57
"""The internal SlackServer instance handling HTTP requests and routing."""
58
```
59
60
## Usage Examples
61
62
### Standalone Server
63
64
```python
65
import os
66
from slackeventsapi import SlackEventAdapter
67
68
# Create adapter with standalone server
69
slack_events_adapter = SlackEventAdapter(
70
signing_secret=os.environ["SLACK_SIGNING_SECRET"],
71
endpoint="/slack/events"
72
)
73
74
# Register event listeners
75
@slack_events_adapter.on("message")
76
def handle_message(event_data):
77
print(f"Message received: {event_data}")
78
79
# Start the server
80
slack_events_adapter.start(host="0.0.0.0", port=3000, debug=True)
81
```
82
83
### Integration with Existing Flask App
84
85
```python
86
import os
87
from flask import Flask
88
from slackeventsapi import SlackEventAdapter
89
90
# Your existing Flask app
91
app = Flask(__name__)
92
93
@app.route("/")
94
def hello():
95
return "Hello World!"
96
97
# Bind adapter to existing app
98
slack_events_adapter = SlackEventAdapter(
99
signing_secret=os.environ["SLACK_SIGNING_SECRET"],
100
endpoint="/slack/events",
101
server=app
102
)
103
104
@slack_events_adapter.on("reaction_added")
105
def reaction_added(event_data):
106
print(f"Reaction: {event_data['event']['reaction']}")
107
108
if __name__ == "__main__":
109
app.run(port=3000)
110
```
111
112
### Custom Endpoint
113
114
```python
115
import os
116
from slackeventsapi import SlackEventAdapter
117
118
# Use custom endpoint path
119
slack_events_adapter = SlackEventAdapter(
120
signing_secret=os.environ["SLACK_SIGNING_SECRET"],
121
endpoint="/api/slack/webhook"
122
)
123
124
@slack_events_adapter.on("app_mention")
125
def handle_mention(event_data):
126
event = event_data["event"]
127
print(f"Mentioned in channel: {event['channel']}")
128
129
slack_events_adapter.start(port=8080)
130
```
131
132
### Accessing Adapter Attributes
133
134
```python
135
import os
136
from slackeventsapi import SlackEventAdapter
137
138
# Create adapter
139
slack_events_adapter = SlackEventAdapter(
140
signing_secret=os.environ["SLACK_SIGNING_SECRET"],
141
endpoint="/api/slack/events"
142
)
143
144
# Access public attributes
145
print(f"Signing secret: {slack_events_adapter.signing_secret}")
146
print(f"Server endpoint: {slack_events_adapter.server.endpoint}")
147
print(f"Package info: {slack_events_adapter.server.package_info}")
148
```