-
Notifications
You must be signed in to change notification settings - Fork 0
Add Slackronyms app code #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Scotchester
wants to merge
1
commit into
main
Choose a base branch
from
feature/slack-app-code
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| apps.dev.json | ||
| cache/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| { | ||
| "manifest": { | ||
| "source": "local" | ||
| }, | ||
| "project_id": "725579d7-7881-4412-8090-c011a560fa42" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| { | ||
| "hooks": { | ||
| "get-hooks": "python3 -m slack_cli_hooks.hooks.get_hooks" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import logging | ||
| import os | ||
|
|
||
| from listeners import register_listeners | ||
| from slack_bolt import App | ||
| from slack_bolt.adapter.socket_mode import SocketModeHandler | ||
|
|
||
| logging.basicConfig(level=logging.DEBUG) | ||
|
|
||
| # From OAuth & Permissions tab | ||
| bot_user_oauth_token = os.environ.get("SLACK_BOT_TOKEN") | ||
|
|
||
| # From Basic Information tab's App-Level Tokens section | ||
| app_token = os.environ.get("SLACK_APP_TOKEN") | ||
|
|
||
| # Initialization | ||
| app = App(token=bot_user_oauth_token) | ||
|
|
||
| # Register Listeners | ||
| register_listeners(app) | ||
|
|
||
| # Start Bolt app | ||
| if __name__ == "__main__": | ||
| # app.start(port=8080) | ||
| SocketModeHandler(app, app_token).start() |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from listeners import commands | ||
|
|
||
|
|
||
| def register_listeners(app): | ||
| commands.register(app) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from slack_bolt import App | ||
|
|
||
| from .define import define_callback | ||
|
|
||
| def register(app: App): | ||
| app.command("/define")(define_callback) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import json | ||
| from datetime import datetime as dt | ||
| from logging import Logger | ||
|
|
||
| from slack_bolt import Ack, Respond | ||
|
|
||
|
|
||
| def define_callback(command, ack: Ack, respond: Respond, logger: Logger): | ||
| logger.debug(command) | ||
|
|
||
| # TODO: DETERMINE HOW TO HANDLE REMOTE STORAGE OF THIS JSON FILE | ||
|
|
||
| # load the current list of terms into a dict | ||
| with open("data/terms.json", mode="r", encoding="utf-8") as read_file: | ||
| terms = json.load(read_file) | ||
|
|
||
| # split command text at the first colon (if any) and detemine whether we have a term | ||
| text = command["text"] | ||
| text = text.split(":", maxsplit=1) | ||
| term = text[0].strip() | ||
|
|
||
| # check whether the term we ened up with isn't just an empty string | ||
| if term: | ||
| try: | ||
| if len(text) == 2: | ||
| # the split resulted in the list `text` having two items, so this is a request to set a definition | ||
| definition = text[1].strip() | ||
| if definition: | ||
| ack() | ||
| terms[term] = { | ||
| "definition": definition, | ||
| "added_by": command["user_name"], | ||
| "added_on": dt.now().strftime("%Y-%m-%d %H:%M:%S"), | ||
| } | ||
| with open("data/terms.json", mode="w", encoding="utf-8") as write_file: | ||
| json.dump(terms, write_file, indent=2) | ||
| respond(f'*{term}* is now defined as "{definition}"', response_type="ephemeral") | ||
| else: | ||
| ack("Please specify a definition after the colon.") | ||
| else: | ||
| # the split resulted in the list `text` having only one item, so this is a request to output a definition | ||
| ack() | ||
|
|
||
| # TODO: MATCH TERMS CASE INSENSITIVELY | ||
|
|
||
| if term_dict := terms.get(term): | ||
| # definition exists, so spit it out publicly | ||
| respond( | ||
| f"Someone asked what *{term}* means. It means: *{term_dict['definition']}*", response_type="in_channel" | ||
| ) | ||
| else: | ||
| # definition doesn't exist, let the requester know that privately | ||
| respond(f"*{term}* not found. Use `/define {term}: <definition>` to add it.", response_type="ephemeral") | ||
| except Exception as e: | ||
| logger.error(e) | ||
| else: | ||
| ack("You must specify a term.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| { | ||
| "_metadata": { | ||
| "major_version": 1, | ||
| "minor_version": 1 | ||
| }, | ||
| "display_information": { | ||
| "name": "Slackronyms" | ||
| }, | ||
| "features": { | ||
| "app_home": { | ||
| "home_tab_enabled": true, | ||
| "messages_tab_enabled": false, | ||
| "messages_tab_read_only_enabled": true | ||
| }, | ||
| "bot_user": { | ||
| "display_name": "Slackronyms", | ||
| "always_online": false | ||
| }, | ||
| "shortcuts": [ | ||
| { | ||
| "name": "Run sample shortcut", | ||
| "type": "global", | ||
| "callback_id": "sample_shortcut_id", | ||
| "description": "Runs a sample shortcut" | ||
| } | ||
| ], | ||
| "slash_commands": [ | ||
| { | ||
| "command": "/define", | ||
| "description": "Explain the given term. Add a colon and definition to set it.", | ||
| "usage_hint": "term[: definition]", | ||
| "should_escape": false | ||
| }, | ||
| { | ||
| "command": "/sample-command", | ||
| "description": "Runs a sample command", | ||
| "should_escape": false | ||
| } | ||
| ] | ||
| }, | ||
| "oauth_config": { | ||
| "scopes": { | ||
| "bot": ["chat:write", "chat:write.public", "commands"] | ||
| } | ||
| }, | ||
| "settings": { | ||
| "event_subscriptions": { | ||
| "bot_events": ["app_home_opened", "message.channels"] | ||
| }, | ||
| "interactivity": { | ||
| "is_enabled": true | ||
| }, | ||
| "org_deploy_enabled": false, | ||
| "socket_mode_enabled": true, | ||
| "token_rotation_enabled": false, | ||
| "is_mcp_enabled": false | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Flask==3.1.3 | ||
| slack-bolt==1.28.0 | ||
| slack-cli-hooks<1.0.0 | ||
| slack-sdk==3.42.0 |
Empty file.
Empty file.
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import logging | ||
| from unittest.mock import Mock | ||
|
|
||
| from listeners.commands.define import define_callback | ||
| from slack_bolt import Ack, Respond | ||
|
|
||
| test_logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class TestDefine: | ||
| def setup_method(self): | ||
| self.fake_ack = Mock(Ack) | ||
| self.fake_respond = Mock(Respond) | ||
| self.fake_command = {"text": "TEST"} | ||
|
|
||
| def test_define_callback_definition(self): | ||
| command = {"text": "who"} | ||
| define_callback( | ||
| command, | ||
| ack=self.fake_ack, | ||
| respond=self.fake_respond, | ||
| logger=test_logger, | ||
| ) | ||
|
|
||
| self.fake_ack.assert_called_once() | ||
|
|
||
| self.fake_respond.assert_called_once() | ||
| args = self.fake_respond.call_args.args | ||
| assert "he's on first" in args[0] | ||
|
|
||
| def test_ack_exception(self, caplog): | ||
| self.fake_ack.side_effect = Exception("test exception") | ||
| define_callback( | ||
| self.fake_command, | ||
| ack=self.fake_ack, | ||
| respond=self.fake_respond, | ||
| logger=test_logger, | ||
| ) | ||
|
|
||
| self.fake_ack.assert_called_once() | ||
| assert str(self.fake_ack.side_effect) in caplog.text |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file is where all the magic happens.