from typing import Any, Optional from smolagents.tools import Tool import os import upload_post class UploadPostTextTool(Tool): name = "upload_post_text" description = "Publish (or schedule) a text-only post on social networks through Upload-Post: X, LinkedIn, Facebook, Threads, Bluesky, Discord, Telegram, Google Business, Slack, Mastodon and more. Returns a dict with the per-platform results, or a job_id when the post is scheduled or queued." inputs = {'text': {'type': 'string', 'description': 'The text of the post.'}, 'platforms': {'type': 'array', 'description': "Platforms to publish to, e.g. ['x', 'linkedin']. Allowed: linkedin, x, facebook, threads, bluesky, discord, telegram, google_business, slack, mastodon, nostr, lemmy, devto, hashnode, wordpress, whop, listmonk."}, 'profile': {'type': 'string', 'description': 'Upload-Post profile (username) to publish from. Omit to use the default profile. Use upload_post_profiles to list profiles.', 'nullable': True}, 'link_url': {'type': 'string', 'description': 'URL shown as a link preview card on LinkedIn, Bluesky and Facebook.', 'nullable': True}, 'scheduled_date': {'type': 'string', 'description': "ISO-8601 date/time to schedule the post instead of publishing now, e.g. '2026-12-31T18:00:00Z'. Must be in the future (max 365 days).", 'nullable': True}, 'timezone': {'type': 'string', 'description': "IANA timezone (e.g. 'Europe/Madrid') for scheduled_date. Defaults to UTC.", 'nullable': True}, 'add_to_queue': {'type': 'boolean', 'description': "Put the post in the profile's next free queue slot. Not combinable with scheduled_date.", 'nullable': True}, 'first_comment': {'type': 'string', 'description': 'First comment posted right after publishing (X, LinkedIn, Facebook, Threads, Bluesky).', 'nullable': True}} output_type = "object" def __init__(self, api_key=None, profile=None, **kwargs): super().__init__() import os self.api_key = api_key or os.environ.get("UPLOAD_POST_API_KEY") self.profile = profile or os.environ.get("UPLOAD_POST_PROFILE") self.client = None def setup(self): from upload_post import UploadPostClient if not self.api_key: raise ValueError( "Missing Upload-Post API key: pass api_key=... or set UPLOAD_POST_API_KEY." ) self.client = UploadPostClient(self.api_key) self.is_initialized = True def forward( self, text: str, platforms: list, profile: Optional[str] = None, link_url: Optional[str] = None, scheduled_date: Optional[str] = None, timezone: Optional[str] = None, add_to_queue: Optional[bool] = None, first_comment: Optional[str] = None, ) -> Any: if not self.is_initialized: self.setup() user = profile or self.profile if not user: raise ValueError( "No Upload-Post profile: pass profile=... or set UPLOAD_POST_PROFILE. " "Call upload_post_profiles to list the available profiles." ) if isinstance(platforms, str): platforms = [p.strip() for p in platforms.split(",") if p.strip()] if not platforms: raise ValueError("At least one platform is required.") if scheduled_date and add_to_queue: raise ValueError("scheduled_date and add_to_queue cannot be used together.") options = { "link_url": link_url, "scheduled_date": scheduled_date, "timezone": timezone, "add_to_queue": add_to_queue, "first_comment": first_comment, } options = {k: v for k, v in options.items() if v is not None and v != ""} return self.client.upload_text( title=text, user=user, platforms=list(platforms), **options )