Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use twitter v2 API instead of legacy. #1806

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 13 additions & 13 deletions autogpt/commands/twitter.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import tweepy
import os
from dotenv import load_dotenv
from autogpt.config import Config

load_dotenv()
cfg = Config()


def send_tweet(tweet_text):
consumer_key = os.environ.get("TW_CONSUMER_KEY")
consumer_secret = os.environ.get("TW_CONSUMER_SECRET")
access_token = os.environ.get("TW_ACCESS_TOKEN")
access_token_secret = os.environ.get("TW_ACCESS_TOKEN_SECRET")
# Authenticate to Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
"""Sends a tweet with the Twitter v2 API

# Create API object
api = tweepy.API(auth)
Args:
tweet_text (str): Content of the tweet.

"""

client = tweepy.Client(consumer_key=cfg.twitter_consumer_key,
consumer_secret=cfg.twitter_consumer_secret,
access_token=cfg.twitter_access_token,
access_token_secret=cfg.twitter_access_token_secret)

# Send tweet
try:
api.update_status(tweet_text)
client.create_tweet(text=tweet_text)
print("Tweet sent successfully!")
except tweepy.TweepyException as e:
print("Error sending tweet: {}".format(e.reason))
6 changes: 6 additions & 0 deletions autogpt/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ def __init__(self) -> None:
# Note that indexes must be created on db 0 in redis, this is not configurable.

self.memory_backend = os.getenv("MEMORY_BACKEND", "local")

self.twitter_consumer_key = os.environ.get("TW_CONSUMER_KEY")
self.twitter_consumer_secret= os.environ.get("TW_CONSUMER_SECRET")
self.twitter_access_token= os.environ.get("TW_ACCESS_TOKEN")
self.twitter_access_token_secret= os.environ.get("TW_ACCESS_TOKEN_SECRET")

# Initialize the OpenAI API client
openai.api_key = self.openai_api_key

Expand Down