Skip to content

Commit

Permalink
Merge pull request #54 from crestalnetwork/fix/twitter-limit
Browse files Browse the repository at this point in the history
Fix: twitter limit
  • Loading branch information
taiyangc authored Jan 18, 2025
2 parents 07a1f6a + 56687af commit c1309c1
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 50 deletions.
34 changes: 24 additions & 10 deletions app/admin/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def create_agent(agent: Agent, db: Session = Depends(get_db)) -> Agent:
# Send Slack notification
total_agents = db.exec(select(func.count()).select_from(Agent)).one()
send_slack_message(
"New agent created ",
"Agent created or updated:",
attachments=[
{
"color": "good",
Expand All @@ -59,30 +59,44 @@ def create_agent(agent: Agent, db: Session = Depends(get_db)) -> Agent:
{"title": "ID", "short": True, "value": latest_agent.id},
{"title": "Name", "short": True, "value": latest_agent.name},
{"title": "Model", "short": True, "value": latest_agent.model},
{
"title": "Enso Enabled",
"short": True,
"value": str(latest_agent.enso_enabled),
},
{
"title": "CDP Enabled",
"short": True,
"value": str(latest_agent.cdp_enabled),
},
{
"title": "CDP Network",
"short": True,
"value": latest_agent.cdp_network_id or "Default",
},
{
"title": "Autonomous",
"short": True,
"value": str(latest_agent.autonomous_enabled),
},
{
"title": "Twitter",
"title": "Autonomous Interval",
"short": True,
"value": str(latest_agent.twitter_enabled),
"value": str(latest_agent.autonomous_minutes),
},
{
"title": "Telegram",
"title": "Twitter Entrypoint",
"short": True,
"value": str(latest_agent.telegram_enabled),
"value": str(latest_agent.twitter_entrypoint_enabled),
},
{
"title": "CDP Enabled",
"title": "Telegram Entrypoint",
"short": True,
"value": str(latest_agent.cdp_enabled),
"value": str(latest_agent.telegram_entrypoint_enabled),
},
{
"title": "CDP Network",
"short": True,
"value": latest_agent.cdp_network_id or "Default",
"title": "Twitter Skills",
"value": str(latest_agent.twitter_skills),
},
],
}
Expand Down
4 changes: 2 additions & 2 deletions skills/twitter/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ def check_rate_limit(
rate_limit = self.store.get_agent_skill_data(
self.agent_id, self.name, "rate_limit"
)

current_time = datetime.now(tz=timezone.utc)

if (
rate_limit
and rate_limit.get("reset_time")
Expand Down
13 changes: 8 additions & 5 deletions skills/twitter/follow_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ def _run(self, user_id: str) -> TwitterFollowUserOutput:
Exception: If there's an error accessing the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1, interval=15)
if is_rate_limited:
return TwitterFollowUserOutput(
success=False, message=f"Error following user: {error}"
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(
max_requests=5, interval=15
)
if is_rate_limited:
return TwitterFollowUserOutput(
success=False, message=f"Error following user: {error}"
)

client = self.twitter.get_client()
if not client:
Expand Down
15 changes: 8 additions & 7 deletions skills/twitter/get_mentions.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ def _run(self) -> TwitterGetMentionsOutput:
Exception: If there's an error accessing the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1)
if is_rate_limited:
return TwitterGetMentionsOutput(
mentions=[],
error=error,
)
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(max_requests=1)
if is_rate_limited:
return TwitterGetMentionsOutput(
mentions=[],
error=error,
)

# get since id from store
last = self.store.get_agent_skill_data(self.agent_id, self.name, "last")
Expand Down
11 changes: 7 additions & 4 deletions skills/twitter/get_timeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,13 @@ def _run(self, max_results: int = 10) -> TwitterGetTimelineOutput:
Exception: If there's an error accessing the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1, interval=15)
if is_rate_limited:
return TwitterGetTimelineOutput(tweets=[], error=error)
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(
max_requests=5, interval=15
)
if is_rate_limited:
return TwitterGetTimelineOutput(tweets=[], error=error)

# get since id from store
last = self.store.get_agent_skill_data(self.agent_id, self.name, "last")
Expand Down
13 changes: 8 additions & 5 deletions skills/twitter/like_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ def _run(self, tweet_id: str) -> TwitterLikeTweetOutput:
Exception: If there's an error accessing the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1, interval=15)
if is_rate_limited:
return TwitterLikeTweetOutput(
success=False, message=f"Error liking tweet: {error}"
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(
max_requests=100, interval=1440
)
if is_rate_limited:
return TwitterLikeTweetOutput(
success=False, message=f"Error liking tweet: {error}"
)

client = self.twitter.get_client()
if not client:
Expand Down
11 changes: 7 additions & 4 deletions skills/twitter/post_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ def _run(self, text: str) -> str:
Exception: If there's an error posting to the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1, interval=15)
if is_rate_limited:
return f"Error posting tweet: {error}"
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(
max_requests=24, interval=1440
)
if is_rate_limited:
return f"Error posting tweet: {error}"

client = self.twitter.get_client()
if not client:
Expand Down
11 changes: 7 additions & 4 deletions skills/twitter/reply_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ def _run(self, tweet_id: str, text: str) -> str:
Exception: If there's an error replying via the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1, interval=15)
if is_rate_limited:
return f"Error replying to tweet: {error}"
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(
max_requests=48, interval=1440
)
if is_rate_limited:
return f"Error replying to tweet: {error}"

client = self.twitter.get_client()
if not client:
Expand Down
13 changes: 8 additions & 5 deletions skills/twitter/retweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ def _run(self, tweet_id: str) -> TwitterRetweetOutput:
Exception: If there's an error accessing the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1, interval=15)
if is_rate_limited:
return TwitterRetweetOutput(
success=False, message=f"Error retweeting: {error}"
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(
max_requests=5, interval=15
)
if is_rate_limited:
return TwitterRetweetOutput(
success=False, message=f"Error retweeting: {error}"
)

client = self.twitter.get_client()
if not client:
Expand Down
11 changes: 7 additions & 4 deletions skills/twitter/search_tweets.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ def _run(
Exception: If there's an error searching via the Twitter API.
"""
try:
# Check rate limit
is_rate_limited, error = self.check_rate_limit(max_requests=1, interval=15)
if is_rate_limited:
return TwitterSearchTweetsOutput(tweets=[], error=error)
# Check rate limit only when not using OAuth
if not self.twitter.use_key:
is_rate_limited, error = self.check_rate_limit(
max_requests=3, interval=15
)
if is_rate_limited:
return TwitterSearchTweetsOutput(tweets=[], error=error)

client = self.twitter.get_client()
if not client:
Expand Down

0 comments on commit c1309c1

Please sign in to comment.