File size: 2,309 Bytes
9dee4c2
 
 
 
 
 
 
 
 
 
 
 
22f8de6
 
 
 
 
9dee4c2
c7a3e0f
9dee4c2
c92a951
9dee4c2
c92a951
9dee4c2
 
 
 
 
 
 
 
c92a951
9dee4c2
c92a951
 
9dee4c2
c92a951
 
 
9dee4c2
 
 
 
 
 
 
7f54d7e
 
 
9dee4c2
c92a951
9dee4c2
71c15e2
7f54d7e
71c15e2
 
 
 
7f54d7e
 
9dee4c2
7f54d7e
c92a951
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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)}"}