|
| 1 | +import tweepy |
| 2 | +from typing import Any |
| 3 | +from loguru import logger |
| 4 | +from pydantic import BaseModel, Field |
| 5 | + |
| 6 | +from openagent.core.tool import Tool |
| 7 | + |
| 8 | + |
| 9 | +class TwitterCredentials(BaseModel): |
| 10 | + bearer_token: str = Field(description="Twitter API bearer token") |
| 11 | + api_key: str = Field(description="Twitter API key (consumer key)") |
| 12 | + api_secret: str = Field(description="Twitter API secret (consumer secret)") |
| 13 | + access_token: str = Field(description="Twitter API access token") |
| 14 | + access_token_secret: str = Field(description="Twitter API access token secret") |
| 15 | + |
| 16 | + |
| 17 | +class TwitterRetweetConfig(BaseModel): |
| 18 | + debug: bool = Field(default=False, description="Enable debug mode") |
| 19 | + credentials: TwitterCredentials |
| 20 | + |
| 21 | + |
| 22 | +class TwitterRetweet(Tool[TwitterRetweetConfig]): |
| 23 | + """Tool for retweeting tweets""" |
| 24 | + |
| 25 | + @property |
| 26 | + def name(self) -> str: |
| 27 | + return "twitter_retweet" |
| 28 | + |
| 29 | + @property |
| 30 | + def description(self) -> str: |
| 31 | + return "Retweet a tweet without adding a comment" |
| 32 | + |
| 33 | + def __init__(self): |
| 34 | + super().__init__() |
| 35 | + self.debug = None |
| 36 | + self.client = None |
| 37 | + |
| 38 | + async def setup(self, config: TwitterRetweetConfig) -> None: |
| 39 | + """Setup the Twitter tool with API credentials""" |
| 40 | + try: |
| 41 | + self.debug = config.debug |
| 42 | + self.client = tweepy.Client( |
| 43 | + bearer_token=config.credentials.bearer_token, |
| 44 | + consumer_key=config.credentials.api_key, |
| 45 | + consumer_secret=config.credentials.api_secret, |
| 46 | + access_token=config.credentials.access_token, |
| 47 | + access_token_secret=config.credentials.access_token_secret, |
| 48 | + ) |
| 49 | + logger.info("Twitter retweet tool setup completed") |
| 50 | + except Exception as e: |
| 51 | + logger.error(f"Error setting up Twitter retweet tool: {e}") |
| 52 | + raise |
| 53 | + |
| 54 | + async def __call__(self, tweet_id: str) -> Any: |
| 55 | + """ |
| 56 | + Retweet a tweet |
| 57 | +
|
| 58 | + Args: |
| 59 | + tweet_id: ID of the tweet to retweet |
| 60 | +
|
| 61 | + Returns: |
| 62 | + str: URL of the retweeted tweet or error message |
| 63 | + """ |
| 64 | + if not self.client: |
| 65 | + logger.error("Twitter client not initialized") |
| 66 | + raise ValueError("Twitter client not initialized. Please run setup first.") |
| 67 | + |
| 68 | + logger.info(f"{self.name} tool is called for tweet: {tweet_id}") |
| 69 | + |
| 70 | + if self.debug: |
| 71 | + return f"Debug mode enabled. Skipping retweet: {tweet_id}" |
| 72 | + |
| 73 | + try: |
| 74 | + response = self.client.retweet(tweet_id) |
| 75 | + if response.data: |
| 76 | + logger.info(f"Successfully retweeted tweet with ID: {tweet_id}") |
| 77 | + return f"Successfully retweeted: https://twitter.com/i/web/status/{tweet_id}" |
| 78 | + else: |
| 79 | + logger.error("Failed to retweet: No response data") |
| 80 | + return "Failed to retweet: No response data" |
| 81 | + except Exception as e: |
| 82 | + error_msg = f"Failed to retweet: {str(e)}" |
| 83 | + logger.error(error_msg) |
| 84 | + return error_msg |
0 commit comments