Skip to content

Commit

Permalink
feat: Add support for report_reaction endpoint (#128)
Browse files Browse the repository at this point in the history
* feat: Add support for report_reaction endpoint

- This is to support message reactions launch. This will replace report_feedback which will be marked deprecated but still functional. Will update documentation around launch time
- Tested end-to-end with server bot running locally and trying different reactions and verifying endpoint is called
- Some formatting changes are in here since I ran all the recommended steps in the documentation
- Updated version to 0.0.49

* Fix formatting errors by running pre-commit

* Remove on_reaction and only keep on_reaction_with_context
  • Loading branch information
ddharman-quora authored Dec 4, 2024
1 parent 88817f0 commit d6b0de8
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "fastapi_poe"
version = "0.0.48"
version = "0.0.49"
authors = [
{ name="Lida Li", email="lli@quora.com" },
{ name="Jelle Zijlstra", email="jelle@quora.com" },
Expand Down
2 changes: 2 additions & 0 deletions src/fastapi_poe/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"QueryRequest",
"SettingsRequest",
"ReportFeedbackRequest",
"ReportReactionRequest",
"ReportErrorRequest",
"SettingsResponse",
"PartialResponse",
Expand Down Expand Up @@ -44,6 +45,7 @@
QueryRequest,
ReportErrorRequest,
ReportFeedbackRequest,
ReportReactionRequest,
RequestContext,
SettingsRequest,
SettingsResponse,
Expand Down
29 changes: 29 additions & 0 deletions src/fastapi_poe/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
QueryRequest,
ReportErrorRequest,
ReportFeedbackRequest,
ReportReactionRequest,
RequestContext,
SettingsRequest,
SettingsResponse,
Expand Down Expand Up @@ -246,6 +247,23 @@ async def on_feedback_with_context(
"""
await self.on_feedback(feedback_request)

async def on_reaction_with_context(
self, reaction_request: ReportReactionRequest, context: RequestContext
) -> None:
"""
Override this to record reaction from the user. This also includes the request context
information.
#### Parameters:
- `reaction_request` (`ReportReactionRequest`): An object representing a reaction request
from Poe. This is sent out when a user provides reaction on a response on your bot.
- `context` (`RequestContext`): an object representing the current HTTP request.
#### Returns: `None`
"""
pass

async def on_error(self, error_request: ReportErrorRequest) -> None:
"""
Expand Down Expand Up @@ -674,6 +692,12 @@ async def handle_report_feedback(
await self.on_feedback_with_context(feedback_request, context)
return JSONResponse({})

async def handle_report_reaction(
self, reaction_request: ReportReactionRequest, context: RequestContext
) -> JSONResponse:
await self.on_reaction_with_context(reaction_request, context)
return JSONResponse({})

async def handle_report_error(
self, error_request: ReportErrorRequest, context: RequestContext
) -> JSONResponse:
Expand Down Expand Up @@ -852,6 +876,11 @@ async def poe_post(request: Request, dict: object = Depends(auth_user)) -> Respo
ReportFeedbackRequest.parse_obj(request_body),
RequestContext(http_request=request),
)
elif request_body["type"] == "report_reaction":
return await bot.handle_report_reaction(
ReportReactionRequest.parse_obj(request_body),
RequestContext(http_request=request),
)
elif request_body["type"] == "report_error":
return await bot.handle_report_error(
ReportErrorRequest.parse_obj(request_body),
Expand Down
21 changes: 21 additions & 0 deletions src/fastapi_poe/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,27 @@ async def report_feedback(
},
)

async def report_reaction(
self,
message_id: Identifier,
user_id: Identifier,
conversation_id: Identifier,
reaction: str,
) -> None:
"""Report message reaction to the bot server."""
await self.session.post(
self.endpoint,
headers=self.headers,
json={
"version": PROTOCOL_VERSION,
"type": "report_reaction",
"message_id": message_id,
"user_id": user_id,
"conversation_id": conversation_id,
"reaction": reaction,
},
)

async def fetch_settings(self) -> SettingsResponse:
"""Fetches settings from a Poe server bot endpoint."""
resp = await self.session.post(
Expand Down
22 changes: 21 additions & 1 deletion src/fastapi_poe/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ class BaseRequest(BaseModel):
"""Common data for all requests."""

version: str
type: Literal["query", "settings", "report_feedback", "report_error"]
type: Literal[
"query", "settings", "report_feedback", "report_reaction", "report_error"
]


class QueryRequest(BaseRequest):
Expand Down Expand Up @@ -146,6 +148,24 @@ class ReportFeedbackRequest(BaseRequest):
feedback_type: FeedbackType


class ReportReactionRequest(BaseRequest):
"""
Request parameters for a report_reaction request.
#### Fields:
- `message_id` (`Identifier`)
- `user_id` (`Identifier`)
- `conversation_id` (`Identifier`)
- `reaction` (`str`)
"""

message_id: Identifier
user_id: Identifier
conversation_id: Identifier
reaction: str


class ReportErrorRequest(BaseRequest):
"""
Expand Down

0 comments on commit d6b0de8

Please sign in to comment.