From 8d74a21a39b56b3c08be40c03d7a603244d6cf99 Mon Sep 17 00:00:00 2001 From: Christopher Gerber Date: Thu, 14 Nov 2024 12:01:38 -0800 Subject: [PATCH] first pass implementing twitter account mentions action --- .../actions/social/twitter/__init__.py | 2 + .../social/twitter/account_mentions.py | 52 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py index c8735ce9a..2d688a066 100644 --- a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/__init__.py @@ -1,4 +1,5 @@ from cdp_agentkit_core.actions.social.twitter.account_details import AccountDetailsAction +from cdp_agentkit_core.actions.social.twitter.account_mentions import AccountMentionsAction from cdp_agentkit_core.actions.social.twitter.action import TwitterAction from cdp_agentkit_core.actions.social.twitter.post_tweet import PostTweetAction @@ -16,6 +17,7 @@ def get_all_twitter_actions() -> list[type[TwitterAction]]: __all__ = [ "TwitterAction", "AccountDetailsAction", + "AccountMentionsAction", "PostTweetAction", "TWITTER_ACTIONS", ] diff --git a/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py new file mode 100644 index 000000000..e26d62216 --- /dev/null +++ b/cdp-agentkit-core/cdp_agentkit_core/actions/social/twitter/account_mentions.py @@ -0,0 +1,52 @@ +from collections.abc import Callable +from json import dumps + +import tweepy +from pydantic import BaseModel, Field + +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.""" + + +class AccountMentionsInput(BaseModel): + """Input argument schema for Twitter account mentions action.""" + + account_id: str = Field( + ..., + description="The account id for the Twitter (X) user to get mentions for", + ) + + +def account_mentions(client: tweepy.Client, account_id: str) -> str: + """Get the authenticated Twitter (X) user account mentions. + + Args: + client (tweepy.Client): The Twitter (X) client used to authenticate with. + account_id (str): The Twitter (X) account id to get mentions for. + + Returns: + str: A message containing account mentions for the authenticated user context. + + """ + message = "" + + try: + response = client.get_users_mentions(account_id) + mentions = response.data + + message = f"Successfully retrieved authenticated user account mentions:{dumps(mentions)}" + except tweepy.errors.TweepyException as e: + message = f"Error retrieving authenticated user account mentions: {e}" + + return message + + +class AccountMentionsAction(TwitterAction): + """Twitter (X) account mentions action.""" + + name: str = "account_mentions" + description: str = ACCOUNT_MENTIONS_PROMPT + args_schema: type[BaseModel] | None = AccountMentionsInput + func: Callable[..., str] = account_mentions