Skip to content

Instantly share code, notes, and snippets.

@hokie-sam
Last active March 6, 2018 16:59
Show Gist options
  • Save hokie-sam/a53fd0219b6787f502fadf34319551e4 to your computer and use it in GitHub Desktop.
Save hokie-sam/a53fd0219b6787f502fadf34319551e4 to your computer and use it in GitHub Desktop.
Simple Pyton code for creating a Twitter bot using their API.
import requests
import json
from requests.auth import AuthBase
from requests_oauthlib import OAuth1
from requests_oauthlib import OAuth1Session
# This is very simple code to start a simple Twitter bot.
# At the moment, this is only for posting tweets to your
# page. Twitter offers additional actions (Liking, Replying,
# Retweeting, etc) on their (confusing) developer website.
# Note: Twitter permits bots on their site, but you MUST
# be clear on your Twitter profile that it is a bot.
# Your Twitter credentials...
# (These are NOT your password and username. You can
# get these 4 credentials from the Twitter developer
# website.)
twt_key = 'YourTwitterKey123'
twt_secret = 'YourTwitterSecret123'
twt_token = 'YourAccessToken123'
twt_token_secret = 'YourAccessTokenSecret123'
base_url = 'https://api.twitter.com/1.1/statuses/update.json'
# Create a session object which contains your Twitter credentials
twit = OAuth1Session(twt_key,
client_secret = twt_secret,
resource_owner_key= twt_token,
resource_owner_secret = twt_token_secret)
def sendTweet(my_status):
'''Post a tweet on your Twitter page. my_status is a string.'''
r = twit.post(base_url, params = {'status': my_stat})
return(json.loads(r.content)) # Return the response from Twitter
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment