Skip to content

Commit

Permalink
fix: twitter skills error with username
Browse files Browse the repository at this point in the history
  • Loading branch information
hyacinthus committed Jan 26, 2025
1 parent 7ac1ebe commit 34075d1
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 27 deletions.
14 changes: 14 additions & 0 deletions skills/twitter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down
10 changes: 6 additions & 4 deletions skills/twitter/follow_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
7 changes: 4 additions & 3 deletions skills/twitter/get_mentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions skills/twitter/get_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions skills/twitter/like_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions skills/twitter/post_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,18 @@ 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)

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.
Expand Down
27 changes: 21 additions & 6 deletions skills/twitter/reply_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand Down
8 changes: 5 additions & 3 deletions skills/twitter/retweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions skills/twitter/search_tweets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 34075d1

Please sign in to comment.