Skip to content

Commit

Permalink
Implemented basic twitter bot functionality using the tweepy module i…
Browse files Browse the repository at this point in the history
…n the Twitter class, now renamed to TwitterBot. (issue #2)
  • Loading branch information
humblepenguinn committed Apr 4, 2024
1 parent 80446b7 commit 88a7851
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 40 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
62 changes: 26 additions & 36 deletions classes/twitter/__init__.py
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()
2 changes: 2 additions & 0 deletions requirements.txt
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
6 changes: 4 additions & 2 deletions st_numainda.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -86,4 +88,4 @@
st.markdown(message.content[0].text.value)

else:
st.write("Click 'Start Chat' to begin.")
st.write("Click 'Start Chat' to begin.")

0 comments on commit 88a7851

Please sign in to comment.