Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ async def async_process(
) -> BoltResponse:
text = req.body.get("event", {}).get("text", "")
if text:
m = re.search(self.keyword, text)
if m is not None:
req.context["matches"] = m.groups() # tuple
m = re.findall(self.keyword, text)
if m is not None and m != []:
if type(m[0]) is not tuple:
m = tuple(m)
else:
m = m[0]
req.context["matches"] = m # tuple or list
return await next()

# As the text doesn't match, skip running the listener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ def process(
) -> BoltResponse:
text = req.body.get("event", {}).get("text", "")
if text:
m = re.search(self.keyword, text)
if m is not None:
req.context["matches"] = m.groups() # tuple
m = re.findall(self.keyword, text)
if m is not None and m != []:
if type(m[0]) is not tuple:
m = tuple(m)
else:
m = m[0]
req.context["matches"] = m # tuple or list
return next()

# As the text doesn't match, skip running the listener
Expand Down
48 changes: 48 additions & 0 deletions tests/scenario_tests/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ def build_request(self) -> BoltRequest:
def build_request2(self) -> BoltRequest:
return self.build_request_from_body(message_body2)

def build_request3(self) -> BoltRequest:
return self.build_request_from_body(message_body3)

def test_string_keyword(self):
app = App(
client=self.web_client,
Expand Down Expand Up @@ -101,6 +104,20 @@ def test_string_keyword_capturing2(self):
time.sleep(1) # wait a bit after auto ack()
assert self.mock_received_requests["/chat.postMessage"] == 1

def test_string_keyword_capturing_multi_capture(self):
app = App(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.message(re.compile("([a-z|A-Z]{3,}-[0-9]+)"))(verify_matches_multi)

request = self.build_request3()
response = app.dispatch(request)
assert response.status == 200
assert_auth_test_count(self, 1)
time.sleep(1) # wait a bit after auto ack()
assert self.mock_received_requests["/chat.postMessage"] == 1

def test_string_keyword_unmatched(self):
app = App(
client=self.web_client,
Expand Down Expand Up @@ -253,9 +270,40 @@ def whats_up(body, payload, message, say):
}


message_body3 = {
"token": "verification_token",
"team_id": "T111",
"enterprise_id": "E111",
"api_app_id": "A111",
"event": {
"client_msg_id": "a8744611-0210-4f85-9f15-5faf7fb225c8",
"type": "message",
"text": "Please fix JIRA-1234, SCM-567 and BUG-169 as soon as you can!",
"user": "W111",
"ts": "1596183880.004200",
"team": "T111",
"channel": "C111",
"event_ts": "1596183880.004200",
"channel_type": "channel",
},
"type": "event_callback",
"event_id": "Ev111",
"event_time": 1596183880,
"authed_users": ["W111"],
}


def verify_matches(context, say, body, payload, message):
assert context["matches"] == ("103", "you")
assert context.matches == ("103", "you")
assert body["event"] == message
assert payload == message
say("Thanks!")


def verify_matches_multi(context, say, body, payload, message):
assert context["matches"] == ("JIRA-1234", "SCM-567", "BUG-169")
assert context.matches == ("JIRA-1234", "SCM-567", "BUG-169")
assert body["event"] == message
assert payload == message
say("Thanks!")
47 changes: 47 additions & 0 deletions tests/scenario_tests_async/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ def build_request(self) -> AsyncBoltRequest:
def build_request2(self) -> AsyncBoltRequest:
return self.build_request_from_body(message_body2)

def build_request3(self) -> AsyncBoltRequest:
return self.build_request_from_body(message_body3)

@pytest.mark.asyncio
async def test_string_keyword(self):
app = AsyncApp(
Expand Down Expand Up @@ -110,6 +113,21 @@ async def test_string_keyword_capturing2(self):
await asyncio.sleep(1) # wait a bit after auto ack()
assert self.mock_received_requests["/chat.postMessage"] == 1

@pytest.mark.asyncio
async def test_string_keyword_capturing_multi_capture(self):
app = AsyncApp(
client=self.web_client,
signing_secret=self.signing_secret,
)
app.message(re.compile("([a-z|A-Z]{3,}-[0-9]+)"))(verify_matches_multi)

request = self.build_request3()
response = await app.async_dispatch(request)
assert response.status == 200
await assert_auth_test_count_async(self, 1)
await asyncio.sleep(1) # wait a bit after auto ack()
assert self.mock_received_requests["/chat.postMessage"] == 1

@pytest.mark.asyncio
async def test_string_keyword_unmatched(self):
app = AsyncApp(
Expand Down Expand Up @@ -268,7 +286,36 @@ async def whats_up(body, say):
}


message_body3 = {
"token": "verification_token",
"team_id": "T111",
"enterprise_id": "E111",
"api_app_id": "A111",
"event": {
"client_msg_id": "a8744611-0210-4f85-9f15-5faf7fb225c8",
"type": "message",
"text": "Please fix JIRA-1234, SCM-567 and BUG-169 as soon as you can!",
"user": "W111",
"ts": "1596183880.004200",
"team": "T111",
"channel": "C111",
"event_ts": "1596183880.004200",
"channel_type": "channel",
},
"type": "event_callback",
"event_id": "Ev111",
"event_time": 1596183880,
"authed_users": ["W111"],
}


async def verify_matches(context, say):
assert context["matches"] == ("103", "you")
assert context.matches == ("103", "you")
await say("Thanks!")


async def verify_matches_multi(context, say, body, payload, message):
assert context["matches"] == ("JIRA-1234", "SCM-567", "BUG-169")
assert context.matches == ("JIRA-1234", "SCM-567", "BUG-169")
await say("Thanks!")