Skip to content

Commit

Permalink
swaping tweepy to dict responses and adding success/failure response …
Browse files Browse the repository at this point in the history
…samples
  • Loading branch information
stat committed Nov 15, 2024
1 parent dc1c1cc commit 7714189
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
from collections.abc import Callable
from json import dumps

import tweepy
from pydantic import BaseModel

from cdp_agentkit_core.actions.social.twitter.action import TwitterAction

ACCOUNT_DETAILS_PROMPT = """
This tool will return account details for the currently authenticated Twitter (X) user context."""
This tool will return account details for the currently authenticated Twitter (X) user context.
A successful response will return a message with the api response as a json payload:
{"data": {"id": "1853889445319331840", "name": "CDP AgentKit", "username": "CDPAgentKit"}}
A failure response will return a message with the tweepy client api request error:
Error retrieving authenticated user account: 429 Too Many Requests
"""


class AccountDetailsInput(BaseModel):
Expand All @@ -27,13 +37,7 @@ def account_details(client: tweepy.Client) -> str:

try:
response = client.get_me()
user = response.data

message = f"""Successfully retrieved authenticated user account details. Please present the following as json and not markdown:
id: {user.id}
name: {user.name}
username: {user.username}
link: https://x.com/{user.username}"""
message = f"""Successfully retrieved authenticated user account details:\n{dumps(response)}"""
except tweepy.errors.TweepyException as e:
message = f"Error retrieving authenticated user account details: {e}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
from cdp_agentkit_core.actions.social.twitter.action import TwitterAction

ACCOUNT_MENTIONS_PROMPT = """
This tool will return account mentions for the currently authenticated Twitter (X) user context."""
This tool will return account mentions for the currently authenticated Twitter (X) user context.
A successful response will return a message with the api response as a json payload:
A failure response will return a message with the tweepy client api request error:
Error retrieving authenticated user account mentions: 429 Too Many Requests
"""


class AccountMentionsInput(BaseModel):
Expand Down Expand Up @@ -36,11 +44,9 @@ def account_mentions(client: tweepy.Client, account_id: str) -> str:

try:
response = client.get_users_mentions(account_id)
mentions = response.data

message = f"Successfully retrieved authenticated user account mentions:{dumps(mentions)}"
message = f"Successfully retrieved authenticated user account mentions:\n{dumps(response)}"
except tweepy.errors.TweepyException as e:
message = f"Error retrieving authenticated user account mentions: {e}"
message = f"Error retrieving authenticated user account mentions:\n{e}"

return message

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
from collections.abc import Callable
from json import dumps

import tweepy
from pydantic import BaseModel, Field

from cdp_agentkit_core.actions.social.twitter import TwitterAction

POST_TWEET_PROMPT = """
This tool will post a tweet on Twitter. The tool takes the text of the tweet as input. Tweets can be maximum 280 characters."""
This tool will post a tweet on Twitter. The tool takes the text of the tweet as input. Tweets can be maximum 280 characters.
A successful response will return a message with the api response as a json payload:
{"data": {"text": "hello, world!", "id": "0123456789012345678", "edit_history_tweet_ids": ["0123456789012345678"]}}
A failure response will return a message with the tweepy client api request error:
You are not allowed to create a Tweet with duplicate content.
"""


class PostTweetInput(BaseModel):
Expand All @@ -32,10 +41,10 @@ def post_tweet(client: tweepy.Client, tweet: str) -> str:
message = ""

try:
client.create_tweet(text=tweet)
message = f"Successfully posted to Twitter:\n{tweet}"
response = client.create_tweet(text=tweet)
message = f"Successfully posted to Twitter:\n{dumps(response)}"
except tweepy.errors.TweepyException as e:
message = f"Error posting to Twitter:\n{tweet}\n{e}"
message = f"Error posting to Twitter:\n{e}"

return message

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
from cdp_agentkit_core.actions.social.twitter import TwitterAction

POST_TWEET_REPLY_PROMPT = """
This tool will post a reply to a tweet on Twitter. The tool takes the text of the reply and the tweet id to reply to as input. Tweets can be maximum 280 characters."""
This tool will post a reply to a tweet on Twitter. The tool takes the text of the reply and the tweet id to reply to as input. Tweets can be maximum 280 characters.
A successful response will return a message with the api response as a json payload:
{"data": {"id": "0123456789012345678", "text": "hellllloooo!", "edit_history_tweet_ids": ["1234567890123456789"]}}
A failure response will return a message with the tweepy client api request error:
You are not allowed to create a Tweet with duplicate content.
"""


class PostTweetReplyInput(BaseModel):
Expand Down Expand Up @@ -40,11 +48,9 @@ def post_tweet_reply(client: tweepy.Client, tweet_id: str, tweet_reply: str) ->

try:
response = client.create_tweet(text=tweet_reply, in_reply_to_tweet_id=tweet_id)
data = response.data

message = f"Successfully posted reply to Twitter:\n{dumps(data)}"
message = f"Successfully posted reply to Twitter:\n{dumps(response)}"
except tweepy.errors.TweepyException as e:
message = f"Error posting reply to Twitter: {e}"
message = f"Error posting reply to Twitter:\n{e}"

return message

Expand Down
1 change: 1 addition & 0 deletions twitter-langchain/twitter_langchain/twitter_api_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def validate_environment(cls, values: dict) -> Any:
access_token=access_token,
access_token_secret=access_token_secret,
bearer_token=bearer_token,
return_type=dict,
)

values["client"] = client
Expand Down

0 comments on commit 7714189

Please sign in to comment.