From 34075d1b6ff1fd38a8a5095f30cc5a9f9f9051e7 Mon Sep 17 00:00:00 2001 From: Muninn Date: Mon, 27 Jan 2025 01:58:19 +0800 Subject: [PATCH] fix: twitter skills error with username --- skills/twitter/base.py | 14 ++++++++++++++ skills/twitter/follow_user.py | 10 ++++++---- skills/twitter/get_mentions.py | 7 ++++--- skills/twitter/get_timeline.py | 4 ++-- skills/twitter/like_tweet.py | 8 +++++--- skills/twitter/post_tweet.py | 6 +++--- skills/twitter/reply_tweet.py | 27 +++++++++++++++++++++------ skills/twitter/retweet.py | 8 +++++--- skills/twitter/search_tweets.py | 6 +++--- 9 files changed, 63 insertions(+), 27 deletions(-) diff --git a/skills/twitter/base.py b/skills/twitter/base.py index d196b83..0fbe053 100644 --- a/skills/twitter/base.py +++ b/skills/twitter/base.py @@ -21,6 +21,20 @@ class TwitterBaseTool(IntentKitSkill): ) store: SkillStoreABC = Field(description="The skill store for persisting data") + def _get_error_with_username(self, error_msg: str) -> str: + """Get error message with username if available. + + Args: + error_msg: The original error message. + + Returns: + Error message with username if available. + """ + username = self.twitter.get_username() + if username: + return f"Error for Twitter user @{username}: {error_msg}" + return error_msg + def check_rate_limit( self, max_requests: int = 1, interval: int = 15 ) -> tuple[bool, str | None]: diff --git a/skills/twitter/follow_user.py b/skills/twitter/follow_user.py index a0863ab..893b732 100644 --- a/skills/twitter/follow_user.py +++ b/skills/twitter/follow_user.py @@ -53,14 +53,14 @@ def _run(self, user_id: str) -> TwitterFollowUserOutput: ) if is_rate_limited: return TwitterFollowUserOutput( - success=False, message=f"Error following user: {error}" + success=False, message=self._get_error_with_username(f"Error following user: {error}") ) client = self.twitter.get_client() if not client: return TwitterFollowUserOutput( success=False, - message="Failed to get Twitter client. Please check your authentication.", + message=self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") ) # Follow the user using tweepy client @@ -73,12 +73,14 @@ def _run(self, user_id: str) -> TwitterFollowUserOutput: success=True, message=f"Successfully followed user {user_id}" ) return TwitterFollowUserOutput( - success=False, message="Failed to follow user." + success=False, + message=self._get_error_with_username("Failed to follow user.") ) except Exception as e: return TwitterFollowUserOutput( - success=False, message=f"Error following user: {str(e)}" + success=False, + message=self._get_error_with_username(str(e)) ) async def _arun(self, user_id: str) -> TwitterFollowUserOutput: diff --git a/skills/twitter/get_mentions.py b/skills/twitter/get_mentions.py index e87cc23..089e44d 100644 --- a/skills/twitter/get_mentions.py +++ b/skills/twitter/get_mentions.py @@ -68,13 +68,14 @@ def _run(self) -> TwitterGetMentionsOutput: if not client: return TwitterGetMentionsOutput( mentions=[], - error="Failed to get Twitter client. Please check your authentication.", + error=self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") ) user_id = self.twitter.get_id() if not user_id: return TwitterGetMentionsOutput( - mentions=[], error="Failed to get Twitter user ID." + mentions=[], + error=self._get_error_with_username("Failed to get Twitter user ID.") ) mentions = client.get_users_mentions( @@ -121,7 +122,7 @@ def _run(self) -> TwitterGetMentionsOutput: except Exception as e: logger.error("Error getting mentions: %s", str(e)) - return TwitterGetMentionsOutput(mentions=[], error=str(e)) + return TwitterGetMentionsOutput(mentions=[], error=self._get_error_with_username(str(e))) async def _arun(self) -> TwitterGetMentionsOutput: """Async implementation of the tool. diff --git a/skills/twitter/get_timeline.py b/skills/twitter/get_timeline.py index 9373781..88173a9 100644 --- a/skills/twitter/get_timeline.py +++ b/skills/twitter/get_timeline.py @@ -72,7 +72,7 @@ def _run(self, max_results: int = 10) -> TwitterGetTimelineOutput: if not client: return TwitterGetTimelineOutput( tweets=[], - error="Failed to get Twitter client. Please check your authentication.", + error=self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") ) timeline = client.get_home_timeline( @@ -120,7 +120,7 @@ def _run(self, max_results: int = 10) -> TwitterGetTimelineOutput: except Exception as e: logger.error("Error getting timeline: %s", str(e)) - return TwitterGetTimelineOutput(tweets=[], error=str(e)) + return TwitterGetTimelineOutput(tweets=[], error=self._get_error_with_username(str(e))) async def _arun(self) -> TwitterGetTimelineOutput: """Async implementation of the tool. diff --git a/skills/twitter/like_tweet.py b/skills/twitter/like_tweet.py index ceb8e4f..54bfc22 100644 --- a/skills/twitter/like_tweet.py +++ b/skills/twitter/like_tweet.py @@ -60,7 +60,7 @@ def _run(self, tweet_id: str) -> TwitterLikeTweetOutput: if not client: return TwitterLikeTweetOutput( success=False, - message="Failed to get Twitter client. Please check your authentication.", + message=self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") ) # Like the tweet using tweepy client @@ -71,12 +71,14 @@ def _run(self, tweet_id: str) -> TwitterLikeTweetOutput: success=True, message=f"Successfully liked tweet {tweet_id}" ) return TwitterLikeTweetOutput( - success=False, message="Failed to like tweet." + success=False, + message=self._get_error_with_username("Failed to like tweet.") ) except Exception as e: return TwitterLikeTweetOutput( - success=False, message=f"Error liking tweet: {str(e)}" + success=False, + message=self._get_error_with_username(str(e)) ) async def _arun(self, tweet_id: str) -> TwitterLikeTweetOutput: diff --git a/skills/twitter/post_tweet.py b/skills/twitter/post_tweet.py index 56f9330..58a30c9 100644 --- a/skills/twitter/post_tweet.py +++ b/skills/twitter/post_tweet.py @@ -51,7 +51,7 @@ def _run(self, text: str) -> str: client = self.twitter.get_client() if not client: - return "Failed to get Twitter client. Please check your authentication." + return self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") # Post tweet using tweepy client response = client.create_tweet(text=text, user_auth=self.twitter.use_key) @@ -59,10 +59,10 @@ def _run(self, text: str) -> str: if "data" in response and "id" in response["data"]: tweet_id = response["data"]["id"] return f"Tweet posted successfully! Tweet ID: {tweet_id}" - return "Failed to post tweet." + return self._get_error_with_username("Failed to post tweet.") except Exception as e: - return f"Error posting tweet: {str(e)}" + return self._get_error_with_username(str(e)) async def _arun(self, text: str) -> str: """Async implementation of the tool. diff --git a/skills/twitter/reply_tweet.py b/skills/twitter/reply_tweet.py index edfb49a..f3a0d28 100644 --- a/skills/twitter/reply_tweet.py +++ b/skills/twitter/reply_tweet.py @@ -2,7 +2,7 @@ from pydantic import BaseModel, Field -from skills.twitter.base import TwitterBaseTool +from skills.twitter.base import TwitterBaseTool, TwitterReplyTweetOutput class TwitterReplyTweetInput(BaseModel): @@ -47,11 +47,17 @@ def _run(self, tweet_id: str, text: str) -> str: max_requests=48, interval=1440 ) if is_rate_limited: - return f"Error replying to tweet: {error}" + return TwitterReplyTweetOutput( + success=False, + message=self._get_error_with_username(error) + ) client = self.twitter.get_client() if not client: - return "Failed to get Twitter client. Please check your authentication." + return TwitterReplyTweetOutput( + success=False, + message=self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") + ) # Post reply tweet using tweepy client response = client.create_tweet( @@ -60,11 +66,20 @@ def _run(self, tweet_id: str, text: str) -> str: if "data" in response and "id" in response["data"]: reply_id = response["data"]["id"] - return f"Reply posted successfully! Reply Tweet ID: {reply_id}" - return "Failed to post reply tweet." + return TwitterReplyTweetOutput( + success=True, + message=f"Reply posted successfully! Reply Tweet ID: {reply_id}" + ) + return TwitterReplyTweetOutput( + success=False, + message=self._get_error_with_username("Failed to reply to tweet.") + ) except Exception as e: - return f"Error posting reply tweet: {str(e)}" + return TwitterReplyTweetOutput( + success=False, + message=self._get_error_with_username(str(e)) + ) async def _arun(self, tweet_id: str, text: str) -> str: """Async implementation of the tool. diff --git a/skills/twitter/retweet.py b/skills/twitter/retweet.py index 06e19f8..2c7a375 100644 --- a/skills/twitter/retweet.py +++ b/skills/twitter/retweet.py @@ -60,7 +60,7 @@ def _run(self, tweet_id: str) -> TwitterRetweetOutput: if not client: return TwitterRetweetOutput( success=False, - message="Failed to get Twitter client. Please check your authentication.", + message=self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") ) # Get authenticated user's ID @@ -82,12 +82,14 @@ def _run(self, tweet_id: str) -> TwitterRetweetOutput: success=True, message=f"Successfully retweeted tweet {tweet_id}" ) return TwitterRetweetOutput( - success=False, message="Failed to retweet tweet." + success=False, + message=self._get_error_with_username("Failed to retweet.") ) except Exception as e: return TwitterRetweetOutput( - success=False, message=f"Error retweeting tweet: {str(e)}" + success=False, + message=self._get_error_with_username(str(e)) ) async def _arun(self, tweet_id: str) -> TwitterRetweetOutput: diff --git a/skills/twitter/search_tweets.py b/skills/twitter/search_tweets.py index 25385b4..8472aae 100644 --- a/skills/twitter/search_tweets.py +++ b/skills/twitter/search_tweets.py @@ -59,13 +59,13 @@ def _run( max_requests=3, interval=15 ) if is_rate_limited: - return TwitterSearchTweetsOutput(tweets=[], error=error) + return TwitterSearchTweetsOutput(tweets=[], error=self._get_error_with_username(error)) client = self.twitter.get_client() if not client: return TwitterSearchTweetsOutput( tweets=[], - error="Failed to get Twitter client. Please check your authentication.", + error=self._get_error_with_username("Failed to get Twitter client. Please check your authentication.") ) # Get since_id from store to avoid duplicate results @@ -116,7 +116,7 @@ def _run( except Exception as e: logger.error("Error searching tweets: %s", str(e)) - return TwitterSearchTweetsOutput(tweets=[], error=str(e)) + return TwitterSearchTweetsOutput(tweets=[], error=self._get_error_with_username(str(e))) async def _arun(self, query: str) -> TwitterSearchTweetsOutput: """Async implementation of the tool.