def run(): """ Runs the bot it a loop, checking for questions and replying """ # We use two twitter clients, one to search, another to update. Just # easier that way... twitter = Twitter(domain="search.twitter.com") twitter.uriparts = () last_id_replied = "" # Fetch the last message replied to from disk to # ensure we don't answer the same question twice. try: with file(LAST_ID_FILE, "r") as content: last_id_replied = content.read() except IOError as ex: print(LAST_ID_FILE + " not found") poster = Twitter( auth=OAuth(ACCESS_TOKEN, ACCESS_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET), secure=True, api_version="1", domain="api.twitter.com", ) while True: results = twitter.search(q=TWITTER_USER, since_id=last_id_replied)["results"] for result in results: last_id_replied = str(result["id"]) question = result["text"] # Only answer tweets with username at the beginning if REG_USER_BEGINNING.match(question) is None: continue # Remove our twitter name from the question. question = REG_USER_BEGINNING.sub("", question) asker = result["from_user"] print("<- {0} (@{1} {2})".format(question, asker, last_id_replied)) # Get the answer from the bot bot_response = decisiverobot.snarkyanswer(question) # Add the twitter @address of the asker msg = "@{0} {1}".format(asker, bot_response) print("-> " + msg) # post reply to twitter poster.statuses.update(status=msg, in_reply_to_status_id=last_id_replied) # Write the last message replied to to disk try: with file(LAST_ID_FILE, "w") as writer: writer.write(last_id_replied) except IOError as ex: print(ex) time.sleep(SLEEP_INTERVAL)
logging.basicConfig(filename='RobBot.log',level=logging.DEBUG,format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') #Load keystore file from the HOME directory keystore_file = os.environ.get('HOME', '') + os.sep + '.rob_bot_keystore' #Read the API keys and other settings into variables OAUTH_TOKEN,OAUTH_TOKEN_SECRET,TWITTER_CONSUMER_KEY,TWITTER_CONSUMER_SECRET,AI_SECRET_KEY,AI_API_KEY,TWITTER_NAME,CHAT_BOT_ID,SNOOZE_TIME= file.readlines(open(keystore_file)) if float(SNOOZE_TIME) < float(10): e= Exception("The SNOOZE_TIME IN YOUR keystore file has to be at least 10!!!") logging.warning("Error: %s" % e) raise e twitter = Twitter(domain='search.twitter.com') twitter.uriparts=() last_id_replied = '' poster = Twitter( auth=OAuth( OAUTH_TOKEN.rstrip(), OAUTH_TOKEN_SECRET.rstrip(), TWITTER_CONSUMER_KEY.rstrip(), TWITTER_CONSUMER_SECRET.rstrip()), secure=True, api_version='1', domain='api.twitter.com') #Infinite loop to monitor the twitter account while True: try: results = twitter.search(q=TWITTER_NAME, since_id=last_id_replied)['results']
def search_client(): client = Twitter(domain='search.twitter.com') client.uriparts = () return client
#import any other natual processing libs CONSUMER_KEY = 'o2EVUpU1Qn3zBRxyQ7xSxQ' CONSUMER_SECRET = 'u46YiTorqOCF0qmgpUSdeg2hxH77Cv6epTOdP869xI' #make sure you authenticate using the commandline tool. This will create a file called .twitter_oauth in your ~ if __name__ == "__main__": oauth_file = os.environ.get("HOME", '') + os.sep + '.twitter_oauth' oa_token, oa_token_secret = read_token_file(oauth_file) #twitter client to search sbird = Twitter(domain='search.twitter.com') #make sure this guy is a tuple sbird.uriparts = () #anyone talking to us? last_id_replied = '' #twitter client to post. Posting requires oAuth schutff pbird = Twitter(auth=OAuth(oa_token, oa_token_secret, CONSUMER_KEY, CONSUMER_SECRET), secure=True, api_version='1', domain="api.twitter.com") #main loop. Just keep searching anyone talking to us while True: mentions = sbird.search(q='@SpeakingSecret', since_id=last_id_replied)['results']
#These are the keys from the twitter tools for python library. CONSUMER_KEY = 'uS6hO2sV6tDKIOeVjhnFnQ' CONSUMER_SECRET = 'MEYTOS97VvlHX7K1rwHPEqVpTSqZ71HtvoK4sVuYk' #make sure you authenticate using the commandline tool. This will create a file called .twitter_oauth in your ~ if __name__ == "__main__": oauth_file = os.environ.get("HOME",'')+os.sep+'.twitter_oauth' oa_token , oa_token_secret = read_token_file(oauth_file) #twitter client to search sbird = Twitter(domain='search.twitter.com') #make sure this guy is a tuple sbird.uriparts =() #anyone talking to us? last_id_replied = '' #we serialize into a file in ~/.twitter_last_reply. check if this file is present and read value. last_file = os.environ.get("HOME",'')+os.sep+'.twitter_last_reply' if os.path.exists(last_file): try: id_file=file(last_file , 'r') id = id_file.readline() last_id_replied = id.strip() print "[+] Read last_reply_id", last_id_replied except IOError: print"[!] Could not read ", last_file