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
2 changes: 1 addition & 1 deletion src/infrastructure/giphy/gif_search.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""GIF search through Teams, with the account's own Microsoft sign-in and no key of our own.

The Teams clients ask their middle tier for Giphy results (see `TeamsChatClient.gif_command`). The answer is a list of
`{composeType, composeValue, fieldValues: [{fieldName, fieldValue}]}`. The route is undocumented and the names in it were
`{composeType, composeValue, fieldValues: [{fieldName, fieldValue}]}`, nested as production showed. The route is undocumented and the names in it were
read from Teams' client code, not from a live answer, so the GIF is found by its Giphy URL rather than by a field name.
A GIF sends as a plain `<img itemtype="http://schema.skype.com/Giphy" src="https://media*.giphy.com/...">` in the message body.
"""
Expand Down
5 changes: 3 additions & 2 deletions src/infrastructure/teams/teams_chat_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ def gif_command(self, broker, query: str, limit: int, rating: str) -> list:
if r.status_code == 401:
self._drop_session(broker)
raise ChatServiceError(r.status_code, "GIF command refused")
result = r.json().get("result")
return result if isinstance(result, list) else []
# The answer nests: {result: [{templateType, result: [the GIFs]}]}.
groups = r.json().get("result")
return [item for group in groups if isinstance(group, dict) and isinstance(group.get("result"), list) for item in group["result"]] if isinstance(groups, list) else []

def _media_host(self, value) -> str | None:
"""The media service base URL when it is an https Microsoft media host, else None."""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_teams_extras.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ def post(self, url, headers=None, content=None, timeout=None):
if url == teams_chat.AUTHZ_URL:
return FakeResponse(200, {"tokens": {"skypeToken": "skype", "expiresIn": 4000}, "regionGtms": {"chatService": "https://ch.ng.msg.teams.microsoft.com", "middleTier": middle_tier}})
calls.append({"url": url, "headers": headers, "body": json.loads(content)})
return FakeResponse(status, {"result": result or []})
return FakeResponse(status, {"result": [{"templateType": "grid", "result": result or []}]})

monkeypatch.setattr(teams_chat, "http_client", lambda: Http())
return calls
Expand Down
Loading