-
-
Notifications
You must be signed in to change notification settings - Fork 10
feat: add Folo webhook support #19
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
Open
LogicOber
wants to merge
4
commits into
perzeuss:main
Choose a base branch
from
LogicOber:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
41787a6
feat: Integrate Folo webhook and update Dify plugin dependency
LogicOber d1e11b1
refactor: Standardize Folo middleware and improve control flow
LogicOber 5a0adb9
chore: bump version from 0.5.1 to 0.5.2
LogicOber a96e00e
feat: add description field to Dify webhook payload mapping
LogicOber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| version: 0.5.1 | ||
| version: 0.5.2 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a new feature, so new version should be 0.6.0 |
||
| type: plugin | ||
| author: perzeuss | ||
| name: webhook | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import json | ||
| import logging | ||
| from werkzeug import Request, Response | ||
| from typing import Optional | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| class FoloMiddleware: | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please create a test for this middleware like in |
||
| def __init__(self): | ||
| """ | ||
| Initializes the FoloMiddleware. | ||
| Currently, it does not require specific settings from the main config. | ||
| """ | ||
| pass | ||
|
|
||
| def invoke(self, r: Request) -> Optional[Response]: | ||
| """ | ||
| Processes the request if it's a Folo webhook. | ||
| - Parses the Folo payload. | ||
| - Transforms it into the Dify-expected format with query="start" | ||
| and specific fields mapped to inputs. | ||
| - Stores the transformed payload in r.default_middleware_json. | ||
| Returns: | ||
| - A Response object if an error occurs during processing (e.g., invalid payload). | ||
| - None if the transformation is successful, allowing the main handler to proceed | ||
| using r.default_middleware_json. | ||
| """ | ||
| logger.debug("FoloMiddleware: Processing request...") | ||
| try: | ||
| folo_payload = r.get_json() | ||
| if not folo_payload or "entry" not in folo_payload: | ||
| logger.error("FoloMiddleware: Invalid or missing 'entry' in Folo payload.") | ||
| return Response( | ||
| json.dumps({"error": "Folo middleware: Invalid payload, 'entry' field is missing."}), | ||
| status=400, | ||
| content_type="application/json" | ||
| ) | ||
|
|
||
| entry = folo_payload.get("entry", {}) | ||
|
|
||
| # Extract required fields from the Folo 'entry' object | ||
| title = entry.get("title") | ||
| content = entry.get("content") | ||
| author = entry.get("author") | ||
| url = entry.get("url") | ||
| published_at = entry.get("publishedAt") | ||
| description = entry.get("description") | ||
|
|
||
| # Construct the new payload for Dify | ||
| dify_inputs = { | ||
| "title": title, | ||
| "content": content, | ||
| "author": author, | ||
| "url": url, | ||
| "publishedAt": published_at, | ||
| "description": description | ||
| } | ||
|
|
||
| # Add debug logging for extracted inputs | ||
| logger.debug(f"FoloMiddleware: Extracted inputs for Dify: {dify_inputs}") | ||
|
|
||
| dify_payload = { | ||
| "query": "start", | ||
| "inputs": dify_inputs | ||
| } | ||
|
|
||
| # Store the transformed payload in the request object for the main endpoint handler | ||
| r.default_middleware_json = dify_payload | ||
| logger.info("FoloMiddleware: Payload transformed successfully and stored in r.default_middleware_json.") | ||
|
|
||
| return None # Indicate successful transformation | ||
|
|
||
| except json.JSONDecodeError as e: | ||
| logger.error(f"FoloMiddleware: JSONDecodeError while parsing Folo payload - {str(e)}") | ||
| return Response( | ||
| json.dumps({"error": f"Folo middleware: Invalid JSON payload - {str(e)}"}), | ||
| status=400, | ||
| content_type="application/json" | ||
| ) | ||
| except Exception as e: | ||
| logger.error(f"FoloMiddleware: Unexpected error during processing - {str(e)}", exc_info=True) | ||
| return Response( | ||
| json.dumps({"error": f"Folo middleware: Unexpected error - {str(e)}"}), | ||
| status=500, | ||
| content_type="application/json" | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove unnecessary comments like
# Ensure logging is imported