Skip to content
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

feat: Add support for report_reaction endpoint #128

Merged
Show file tree
Hide file tree
Changes from 2 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 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="[email protected]" },
{ name="Jelle Zijlstra", email="[email protected]" },
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
41 changes: 41 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,35 @@ async def on_feedback_with_context(
"""
await self.on_feedback(feedback_request)

async def on_reaction(self, reaction_request: ReportReactionRequest) -> None:
JelleZijlstra marked this conversation as resolved.
Show resolved Hide resolved
"""

Override this to record reaction from the user.
#### 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.
#### Returns: `None`

"""
pass

async def on_reaction_with_context(
self, reaction_request: ReportReactionRequest, context: RequestContext
) -> None:
"""

A version of `on_reaction` that also includes the request context information. By
default, this will call `on_reaction`.

#### 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`

"""
await self.on_reaction(reaction_request)

async def on_error(self, error_request: ReportErrorRequest) -> None:
"""

Expand Down Expand Up @@ -674,6 +704,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 +888,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
Loading