Skip to content

Instantly share code, notes, and snippets.

@melizeche
Created February 9, 2016 21:19
Show Gist options
  • Save melizeche/74bacf859f0a4a03a19e to your computer and use it in GitHub Desktop.
Save melizeche/74bacf859f0a4a03a19e to your computer and use it in GitHub Desktop.
sample tweepy bot
#!/usr/bin/env python3
import json
import tweepy
STRING_TO_CHECK = "El texto que queres que este 'escuchando', en el caso del comentabot es su usuario"
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
class TwitterAPI:
def __init__(self):
listener = StListener()
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
self.stream = tweepy.Stream(auth, listener)
self.api = tweepy.API(auth)
def tweet(self, message):
try:
print(message[:140])
self.api.update_status(status=message[:140])
except Exception as err:
msg = "Tweet Error: {0}".format(err)
print(msg)
def reply(self, message, reply_id):
try:
print(message[:140])
self.api.update_status(
status=message[:140], in_reply_to_status_id=reply_id)
except Exception as err:
msg = "Reply Error: {0}".format(err)
print(msg)
class StListener(tweepy.streaming.StreamListener):
def on_data(self, data):
#do something
parsed_tweet = json.loads(data)
print(parsed_tweet)
return True
def on_error(self, status):
print(status)
#bot init
twitter = TwitterAPI()
if __name__ == "__main__":
#listening
twitter.stream.filter(track=[STRING_TO_CHECK], async=True)
#tweet
twitter.tweet("Hello World!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment