Skip to content

Commit

Permalink
wip: first pass implementing mentions monitor start
Browse files Browse the repository at this point in the history
  • Loading branch information
stat committed Nov 13, 2024
1 parent caf3301 commit 7408608
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion cdp-agentkit-core/cdp_agentkit_core/actions/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def create_var(self, name: str, default=None) -> None:
store = self.cvars.get()

if name not in store:
store[name] = contextvars.ContextVar(name, default=default)
store[name] = ContextVar(name, default=default)
self.cvars.set(store)

def get(self, name: str) -> Any:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from cdp_agentkit_core.actions.social.twitter.account_details import AccountDetailsAction
from cdp_agentkit_core.actions.social.twitter.action import Action as TwitterAction
from cdp_agentkit_core.actions.social.twitter.context import Context as TwitterContext

from cdp_agentkit_core.actions.social.twitter.account_details import AccountDetailsAction
from cdp_agentkit_core.actions.social.twitter.mentions_monitor_details import (
MentionsMonitorDetailsAction,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
import contextvars
from contextvars import ContextVar

import tweepy

from cdp_agentkit_core.actions.context import Context


class Context(Context):
client: contextvars.ContextVar[tweepy.Client] | None = None
api: ContextVar[tweepy.API] | None = None
client: ContextVar[tweepy.Client] | None = None

def __init__(self):
super().__init__()
self.client = contextvars.ContextVar("client", default=None)
self.api = ContextVar("api", default=None)
self.client = ContextVar("client", default=None)

def get_api(self) -> tweepy.API:
return self.api.get()

def set_api(self, value: tweepy.API):
self.api.set(value)

def get_client(self) -> tweepy.Client:
return self.client.get()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,57 @@
from collections.abc import Callable

import asyncio
import tweepy
from pydantic import BaseModel

from cdp_agentkit_core.actions.social.twitter import TwitterAction, TwitterContext

MENTIONS_MONITOR_START_PROMPT = """
"""
This tool will monitor mentions for the currently authenticated Twitter (X) user context."""

# TODO: enums

class MentionsMonitorStartInput(BaseModel):
pass


def mentions_monitor_start(context: TwitterContext) -> str:
pass
try:
state = context.get("mentions-state")
if state == "running":
return "already running"
except KeyError as e:
pass

context.set("mentions-state", "running")
task = asyncio.create_task(monitor_mentions(context))
context.set("mentions-task", task)

return "started monitoring"

def monitor_mentions(context: TwitterContext):
last_id = 0


try:
response = context.get_client().get_me()
me = response.data
except tweepy.errors.TweepyException as e:
return f"Error retrieving authenticated user account details: {e}"

while context.get("mentions-state") == "running":
try:
# mentions = context.get_api().mentions_timeline(since_id=last_id)
mentions = context.get_client().get_users_mentions(me.id, since_id=last_id)
for mention in mentions:
print(f"@{mention.user.screen_name}: {mention.text}")
last_id = mention.id

except tweepy.errors.TweepyException as e:
print(f"Error: {e}")
return

asyncio.sleep(60)

class MentionsMonitorStartAction(TwitterAction):
name: str = "mentions_monitor_start"
Expand Down
12 changes: 12 additions & 0 deletions twitter-langchain/twitter_langchain/twitter_api_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Util that calls Twitter API."""

import contextvars
import inspect
from collections.abc import Callable
from typing import Any

Expand Down Expand Up @@ -32,6 +34,14 @@ def validate_environment(cls, values: dict) -> Any:
"Tweepy Twitter SDK is not installed. " "Please install it with `pip install tweepy`"
) from None

api_auth = tweepy.OAuth1UserHandler(
api_key,
api_secret,
access_token,
access_token_secret,
)

api = tweepy.API(api_auth)

client = tweepy.Client(
consumer_key=api_key,
Expand All @@ -41,9 +51,11 @@ def validate_environment(cls, values: dict) -> Any:
)

context = TwitterContext()
context.set_api(api)
context.set_client(client)

values["context"] = context
values["api"] = context.api
values["client"] = context.client
values["api_key"] = api_key
values["api_secret"] = api_secret
Expand Down

0 comments on commit 7408608

Please sign in to comment.