Spaces:
Sleeping
Sleeping
| import requests | |
| from requests.auth import HTTPBasicAuth | |
| from typing import Dict, Optional | |
| from smolagents.tools import Tool | |
| class WordPressPostTool(Tool): | |
| name = "wordpress_post" | |
| description = "Publishes a blog post to WordPress using the REST API" | |
| inputs = { | |
| 'title': {'type': 'string', 'description': 'The title of the blog post'}, | |
| 'content': {'type': 'string', 'description': 'The content of the blog post in HTML format'}, | |
| 'status': { | |
| 'type': 'string', | |
| 'description': 'Post status: publish, draft, or private. Default is publish', | |
| 'nullable': True | |
| } | |
| } | |
| output_type = "object" | |
| def __init__(self, wordpress_credentials): | |
| super().__init__() | |
| self.credentials = wordpress_credentials | |
| def forward(self, title: str, content: str, status: str = "publish") -> Dict: | |
| """Publishes a post to WordPress | |
| Args: | |
| title: The title of the blog post | |
| content: The content in HTML format | |
| status: Post status (publish, draft, private) | |
| Returns: | |
| Dict containing the response from WordPress | |
| """ | |
| if not self.credentials: | |
| return {"error": "WordPress credentials not configured"} | |
| api_url = f"{self.credentials['site_url']}/wp-json/wp/v2/posts" | |
| auth = HTTPBasicAuth( | |
| self.credentials['username'], self.credentials['app_password']) | |
| data = { | |
| "title": title, | |
| "content": content, | |
| "status": status | |
| } | |
| print(f"api_url: {api_url}") | |
| print(f"auth: {auth}") | |
| print(f"data: {data}") | |
| try: | |
| response = requests.post(api_url, json=data, auth=auth) | |
| response.raise_for_status() | |
| response_data = response.json() | |
| response_returned = { | |
| "status": "success", | |
| "post_id": response_data.get("id"), | |
| "post_url": response_data.get("link") | |
| } | |
| print(f"Post published successfully: {response_returned}") | |
| return response_returned | |
| except requests.exceptions.RequestException as e: | |
| print(f"Failed to publish post: {str(e)}") | |
| return {"error": f"Failed to publish post: {str(e)}"} | |