Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
from slack_bolt import App
from slack_bolt.adapter.socket_mode import SocketModeHandler

from listeners import register_listeners
from listeners import load_listeners

logging.basicConfig(level=logging.DEBUG)

# Initialization
app = App(token=os.environ.get("SLACK_BOT_TOKEN"))

# Register Listeners
register_listeners(app)
# Load Listeners
load_listeners(app)

# Start Bolt app
if __name__ == "__main__":
Expand Down
6 changes: 3 additions & 3 deletions app_oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from slack_sdk.oauth.installation_store import FileInstallationStore
from slack_sdk.oauth.state_store import FileOAuthStateStore

from listeners import register_listeners
from listeners import load_listeners

logging.basicConfig(level=logging.DEBUG)

Expand Down Expand Up @@ -42,8 +42,8 @@ def failure(args: FailureArgs) -> BoltResponse:
),
)

# Register Listeners
register_listeners(app)
# Load Listeners
load_listeners(app)

# Start Bolt app
if __name__ == "__main__":
Expand Down
19 changes: 11 additions & 8 deletions listeners/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
from listeners import actions, commands, events, messages, shortcuts, views
from importlib import import_module
from pkgutil import walk_packages

from slack_bolt import App

def register_listeners(app):
actions.register(app)
commands.register(app)
events.register(app)
messages.register(app)
shortcuts.register(app)
views.register(app)
app: App


def load_listeners(bolt_app: App) -> None:
global app
app = bolt_app
for module_info in walk_packages(__path__, f"{__name__}."):
import_module(module_info.name)
7 changes: 0 additions & 7 deletions listeners/actions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from slack_bolt import App

from .sample_action import sample_action_callback


def register(app: App):
app.action("sample_action_id")(sample_action_callback)
3 changes: 3 additions & 0 deletions listeners/actions/sample_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from slack_bolt import Ack
from slack_sdk import WebClient

from listeners import app


@app.action("sample_action_id")
def sample_action_callback(ack: Ack, client: WebClient, body: dict, logger: Logger):
try:
ack()
Expand Down
7 changes: 0 additions & 7 deletions listeners/commands/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from slack_bolt import App

from .sample_command import sample_command_callback


def register(app: App):
app.command("/sample-command")(sample_command_callback)
3 changes: 3 additions & 0 deletions listeners/commands/sample_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

from slack_bolt import Ack, Respond

from listeners import app


@app.command("/sample-command")
def sample_command_callback(command, ack: Ack, respond: Respond, logger: Logger):
try:
ack()
Expand Down
7 changes: 0 additions & 7 deletions listeners/events/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from slack_bolt import App

from .app_home_opened import app_home_opened_callback


def register(app: App):
app.event("app_home_opened")(app_home_opened_callback)
3 changes: 3 additions & 0 deletions listeners/events/app_home_opened.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

from slack_sdk import WebClient

from listeners import app


@app.event("app_home_opened")
def app_home_opened_callback(client: WebClient, event: dict, logger: Logger):
# ignore the app_home_opened event for anything but the Home tab
if event["tab"] != "home":
Expand Down
10 changes: 0 additions & 10 deletions listeners/messages/__init__.py
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
import re

from slack_bolt import App

from .sample_message import sample_message_callback


# To receive messages from a channel or dm your app must be a member!
def register(app: App):
app.message(re.compile("(hi|hello|hey)"))(sample_message_callback)
5 changes: 5 additions & 0 deletions listeners/messages/sample_message.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import re
from logging import Logger

from slack_bolt import BoltContext, Say

from listeners import app


# To receive messages from a channel or dm your app must be a member!
@app.message(re.compile("(hi|hello|hey)"))
def sample_message_callback(context: BoltContext, say: Say, logger: Logger):
try:
greeting = context["matches"][0]
Expand Down
7 changes: 0 additions & 7 deletions listeners/shortcuts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from slack_bolt import App

from .sample_shortcut import sample_shortcut_callback


def register(app: App):
app.shortcut("sample_shortcut_id")(sample_shortcut_callback)
3 changes: 3 additions & 0 deletions listeners/shortcuts/sample_shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from slack_bolt import Ack
from slack_sdk import WebClient

from listeners import app


@app.shortcut("sample_shortcut_id")
def sample_shortcut_callback(body: dict, ack: Ack, client: WebClient, logger: Logger):
try:
ack()
Expand Down
7 changes: 0 additions & 7 deletions listeners/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +0,0 @@
from slack_bolt import App

from .sample_view import sample_view_callback


def register(app: App):
app.view("sample_view_id")(sample_view_callback)
3 changes: 3 additions & 0 deletions listeners/views/sample_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
from slack_bolt import Ack
from slack_sdk import WebClient

from listeners import app


@app.view("sample_view_id")
def sample_view_callback(view, ack: Ack, body: dict, client: WebClient, logger: Logger):
try:
ack()
Expand Down
9 changes: 9 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from unittest.mock import patch

from slack_bolt import App
from slack_sdk import WebClient

from listeners import load_listeners

with patch.object(WebClient, "auth_test"):
load_listeners(App(token="test-bot-token"))
13 changes: 13 additions & 0 deletions tests/listeners/test_load_listeners.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import sys
from pkgutil import walk_packages

import listeners


class TestLoadListeners:
def test_imports_every_listener_module(self):
for module_info in walk_packages(listeners.__path__, f"{listeners.__name__}."):
assert module_info.name in sys.modules

def test_registers_every_listener_on_the_app(self):
assert len(listeners.app._listeners) == 6
Loading