From 88a7851def219c7f26d06dd20cda571de3d642f1 Mon Sep 17 00:00:00 2001 From: Humble Penguin Date: Thu, 4 Apr 2024 21:49:43 +0500 Subject: [PATCH] Implemented basic twitter bot functionality using the tweepy module in the Twitter class, now renamed to TwitterBot. (issue #2) --- README.md | 4 +-- classes/twitter/__init__.py | 62 ++++++++++++++++--------------------- requirements.txt | 2 ++ st_numainda.py | 6 ++-- 4 files changed, 34 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index 1ba30d8..0c6f47b 100644 --- a/README.md +++ b/README.md @@ -39,8 +39,8 @@ Numainda is a Knowledge bot designed to engage and educate on Pakistan's rich le Create a `.env` file in the project root and add your Twitter and OpenAI API keys: ```plaintext - TWITTER_API_KEY=your_twitter_api_key - TWITTER_API_SECRET=your_twitter_api_secret + TWITTER_CONSUMER_KEY=your_twitter_consumer_key + TWITTER_CONSUMER_SECRET=your_twitter_consumer_secret TWITTER_ACCESS_TOKEN=your_access_token TWITTER_ACCESS_TOKEN_SECRET=your_access_token_secret OPENAI_API_KEY=your_openai_api_key diff --git a/classes/twitter/__init__.py b/classes/twitter/__init__.py index 7aac23c..797cfa0 100644 --- a/classes/twitter/__init__.py +++ b/classes/twitter/__init__.py @@ -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 \ No newline at end of file + self.api.update_status(tweet) + return True + + def get_all_mentions(self): + return self.api.mentions_timeline() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 0852232..cd98639 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/st_numainda.py b/st_numainda.py index 76c795f..b9859c5 100644 --- a/st_numainda.py +++ b/st_numainda.py @@ -3,6 +3,9 @@ import time from classes.assistant import Assistant +from dotenv import load_dotenv +load_dotenv() + assistant_id = st.secrets["ASSISTANT_ID"] client = Assistant(openai_api_key=st.secrets["OPEN_AI_API_KEY"]).get_client() @@ -27,7 +30,6 @@ * What happened in the parliament on 15th march 2024? """) - if st.button("Start Chat"): st.session_state.start_chat = True thread = client.beta.threads.create() @@ -86,4 +88,4 @@ st.markdown(message.content[0].text.value) else: - st.write("Click 'Start Chat' to begin.") \ No newline at end of file + st.write("Click 'Start Chat' to begin.")