Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/pip/pip-21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasbarra authored Feb 13, 2023
2 parents 33940a8 + 3abc741 commit e27333f
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 3 deletions.
3 changes: 3 additions & 0 deletions data/mock/interactive/view_submission_synchronous_test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"payload": "{\"type\":\"view_submission\",\"token\":\"ABCDEFGHIJKLMNOPQRSTUVWX\",\"team\":{\"id\":\"TEST_TEAM_ID\",\"domain\":\"test-team-name\"},\"user\":{\"id\":\"TEST_USER_ID\",\"name\":\"testusername\"},\"view\":{\"id\":\"VNHU13V36\",\"type\":\"modal\",\"title\":{ \"a\":\"b\" },\"submit\":{ \"a\":\"b\" },\"blocks\":[],\"private_metadata\":\"shhh-its-secret\",\"callback_id\":\"modal-with-inputs\",\"state\":{\"values\":{\"multiline\":{\"mlvalue\":{\"type\":\"plain_text_input\",\"value\":\"This is my example inputted value\"}},\"target_channel\":{\"target_select\":{\"type\":\"conversations_select\",\"selected_conversation\":\"C123B12DE\"}}}},\"hash\":\"156663117.cd33ad1f\",\"response_urls\":[{\"block_id\":\"target_channel\",\"action_id\":\"target_select\",\"channel_id\":\"C123B12DE\",\"response_url\":\"https:\\/\\/hooks.slack.com\\/app\\/ABC12312\\/1234567890\\/A100B100C100d100\"}]}}"
}
10 changes: 10 additions & 0 deletions data/test_omnibot_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ handlers:
- "echobot"
callbacks:
- module: "omnibot.handlers.interactive_component_handlers:echo_dialog_submission_handler"
- callback_id: "modal-with-inputs"
response_type: "in_channel"
bots:
"test-team-name":
- "TEST_OMNIBOT_NAME"
"testteam":
- "echobot"
callbacks:
- module: "tests.data:test_handler"
synchronous: true
slash_command_handlers:
- command: "/echo"
response_type: "in_channel"
Expand Down
2 changes: 2 additions & 0 deletions omnibot/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,8 @@ def _handle_interactive_component_callback(component, callback, response_type):
extra={**component.event_trace, 'callback': callback},
)
response = _handle_callback(component, callback)
if response_type == 'raw':
return response
for component_response in response.get('responses', []):
logger.debug(
'Handling response for callback (pre-parse): {}'.format(
Expand Down
28 changes: 26 additions & 2 deletions omnibot/routes/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
from omnibot.services import sqs
from omnibot.services import stats
from omnibot.services import slack
from omnibot.services.slack.interactive_component import InteractiveComponent
from omnibot.services.slack.team import Team
from omnibot.services.slack.team import TeamInitializationError
from omnibot.services.slack.bot import Bot
from omnibot.services.slack.bot import BotInitializationError
from omnibot.utils import get_callback_id, merge_logging_context
from omnibot.processor import _handle_interactive_component_callback

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -262,6 +264,7 @@ def slack_interactive_component():
'message_action',
'dialog_submission',
'block_actions',
'view_submission',
]
):
msg = ('Unsupported type={} in interactive'
Expand Down Expand Up @@ -321,7 +324,10 @@ def slack_interactive_component():
' associated with it.')
logger.error(
msg,
extra=bot.logging_context,
extra=merge_logging_context(
{'callback_id': get_callback_id(component)},
bot.logging_context,
)
)
return jsonify({'response_type': 'ephemeral', 'text': msg}), 200
# To avoid needing to look the bot up from its token when the dequeue this
Expand All @@ -332,6 +338,19 @@ def slack_interactive_component():
# If there's no callbacks defined for this interactive component, we
# can skip enqueuing it, since the workers will just discard it.
if handler_found.get('callbacks'):
callbacks = handler_found.get('callbacks')
for c in callbacks:
if c.get('synchronous'):
interactive_component = InteractiveComponent(bot, component, {})
resp = _handle_interactive_component_callback(interactive_component, c, 'raw')
logger.info(
'Synchronous callback response',
extra=merge_logging_context(
{'response': resp},
bot.logging_context,
)
)
return resp, 200
queue_event(bot, component, 'interactive_component')
except Exception:
msg = 'Could not queue interactive component.'
Expand Down Expand Up @@ -711,7 +730,12 @@ def _perform_action(bot, data):
)
logger.debug(ret)
if not ret['ok']:
if ret.get('error') in ['missing_scope', 'not_allowed_token_type']:
if ret.get('error') in {
'missing_scope',
'not_allowed_token_type',
'channel_not_found',
'not_in_channel',
}:
logger.warning(
'action failed in post_slack, attempting as user.',
extra=merge_logging_context(
Expand Down
3 changes: 2 additions & 1 deletion omnibot/services/slack/interactive_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def __init__(self, bot, component, event_trace):
self._payload['action_ts'] = component.get('action_ts')
self._payload['message_ts'] = component.get('message_ts')
self._payload['trigger_id'] = component.get('trigger_id')
self._payload['response_url'] = component['response_url']
self._payload['response_url'] = component.get('response_url')
self._payload['original_message'] = component.get('original_message')
self._payload['view'] = component.get('view')
self._payload['state'] = component.get('state')
self._payload['user'] = component.get('user')
if self.user:
Expand Down
3 changes: 3 additions & 0 deletions omnibot/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ def get_callback_id(component):
actions = component.get('actions', [])
action = next(iter(actions))
return action.get('block_id')
elif component.get('type') == 'view_submission':
view = component.get('view', {})
return view.get('callback_id')
else:
return component.get('callback_id')

Expand Down
4 changes: 4 additions & 0 deletions tests/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@

def get_mock_data(filepath: str):
return open(path.join(MOCK_DIR, filepath))


def test_handler(container):
return "{\"foo\": \"bar\"}"
18 changes: 18 additions & 0 deletions tests/integration/routes/test_interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,21 @@ def test_invalid_callback_id(
)
queue.assert_not_called()
slack_api_call.assert_not_called()


def test_view_submission_synchronous(
client: Client, queue: MagicMock, slack_api_call: MagicMock, mocker
):
mocker.patch("omnibot.services.slack.get_user", return_value={"id": "TEST_USER_ID"})
with get_mock_data("interactive/view_submission_synchronous_test.json") as json_data:
event: Dict[str, Any] = json.loads(json_data.read())
resp: Response = client.post(
_ENDPOINT,
data=event,
content_type="application/x-www-form-urlencoded",
)
component = json.loads(event["payload"])
component["omnibot_bot_id"] = "TEST_OMNIBOT_ID"
queue.assert_not_called()
assert resp.status_code == 200
assert resp.data == b"{\"foo\": \"bar\"}"

0 comments on commit e27333f

Please sign in to comment.