Skip to content

Commit

Permalink
use library to tweet
Browse files Browse the repository at this point in the history
  • Loading branch information
glaucocustodio committed Jun 14, 2023
1 parent e9192ed commit 6148609
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 27 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ requests==2.28.2
requests-oauthlib==1.3.1
beautifulsoup4==4.11.1
python-dotenv==1.0.0
python-twitter-v2==0.8.1
42 changes: 15 additions & 27 deletions twitter.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,20 @@
import requests
from requests_oauthlib import OAuth1
import json, os
import pytwitter
import os
from dotenv import load_dotenv

load_dotenv()

def tweet(message):
url = "https://api.twitter.com/2/tweets"

payload = json.dumps({"text": message})

consumer_key = os.environ['TWITTER_CONSUMER_KEY']
consumer_secret = os.environ['TWITTER_CONSUMER_SECRET']
access_token = os.environ['TWITTER_ACCESS_TOKEN']
access_token_secret = os.environ['TWITTER_ACCESS_TOKEN_SECRET']

oauth = OAuth1(
consumer_key,
consumer_secret,
access_token,
access_token_secret
)

headers = {
'Content-Type': 'application/json',
}

response = requests.post(url, headers=headers, data=payload, auth=oauth)
response.raise_for_status()

return response
consumer_key = os.environ['TWITTER_CONSUMER_KEY']
consumer_secret = os.environ['TWITTER_CONSUMER_SECRET']
access_token = os.environ['TWITTER_ACCESS_TOKEN']
access_token_secret = os.environ['TWITTER_ACCESS_TOKEN_SECRET']

twitter_api = pytwitter.Api(
consumer_key=consumer_key,
consumer_secret=consumer_secret,
access_token=access_token,
access_secret=access_token_secret
)

return twitter_api.create_tweet(text=message)

0 comments on commit 6148609

Please sign in to comment.