-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from humblepenguinn/twitter_bot
Implemented basic twitter bot functionality (issue #2)
- Loading branch information
Showing
4 changed files
with
34 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,32 @@ | ||
import os | ||
from requests_oauthlib import OAuth1Session | ||
import json | ||
import tweepy | ||
|
||
|
||
class Twitter: | ||
|
||
class TwitterBot: | ||
def __init__(self): | ||
self.api_key = os.environ.get('CONSUMER_KEY') | ||
self.api_secret = os.environ.get('CONSUMER_SECRET') | ||
self.access_token = os.environ.get('ACCESS_TOKEN') | ||
self.access_token_secret = os.environ.get('ACCESS_TOKEN_SECRET') | ||
self.api = TwitterBot.authenticate() | ||
|
||
# Entry point for the Twitter bot | ||
def start(self): | ||
pass | ||
|
||
def authenticate() -> tweepy.API: | ||
auth = tweepy.OAuthHandler(os.getenv('TWITTER_CONSUMER_KEY'), os.getenv('TWITTER_CONSUMER_SECRET')) | ||
auth.set_access_token(os.getenv('TWITTER_ACCESS_TOKEN'), os.getenv('TWITTER_ACCESS_TOKEN')) | ||
|
||
self.oauth = OAuth1Session( | ||
self.api_key, | ||
client_secret=self.api_secret, | ||
resource_owner_key=self.access_token, | ||
resource_owner_secret=self.access_token_secret | ||
) | ||
|
||
api = tweepy.API(auth) | ||
|
||
try: | ||
api.verify_credentials() | ||
print("Authentication was successful!") | ||
except Exception as e: | ||
print(f"Authentication failed: {e}") | ||
|
||
return api | ||
|
||
def post_tweet(self, tweet): | ||
# Twitter API v2 endpoint for posting tweets | ||
url = 'https://api.twitter.com/2/tweets' | ||
|
||
|
||
payload = { | ||
'text': tweet | ||
} | ||
|
||
headers = { | ||
'Content-Type': 'application/json' | ||
} | ||
|
||
print(json.dumps(payload)) # Optional: For debugging | ||
|
||
response = self.oauth.post(url, headers=headers, data=json.dumps(payload)) | ||
|
||
if response.status_code == 201: # Check for successful tweet creation | ||
return response.json() | ||
else: | ||
print("Tweet failed:", response.text) | ||
return None | ||
self.api.update_status(tweet) | ||
return True | ||
|
||
def get_all_mentions(self): | ||
return self.api.mentions_timeline() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
requests-oauthlib==1.3.1 | ||
openai==1.12.0 | ||
streamlit==1.32.2 | ||
tweepy==4.14.0 | ||
python-dotenv===1.0.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters