-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrespondingbot.py
64 lines (46 loc) · 1.67 KB
/
respondingbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Twitter Bot Starter Kit: Responding Bot
# This bot listens to the account @ocertat, and when that account
# tweets, it responds with a line of Twain
# Download a Project Gutenberg "Plain Text UTF-8" file,
# open it in Notepad, remove junk at beginning,
# and replace all double-linebreaks with single linebreaks.
# Housekeeping: do not edit
import tweepy
import time
from credentials import *
from random import randint
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET)
api = tweepy.API(auth)
# initially, the script will assume that the last tweet was a null value
lasttweet = None
# What the bot will tweet
filename = open('twain.txt', 'r')
tweettext = filename.readlines()
filename.close()
# a function that picks a random line
def linenum():
return randint(0, len(tweettext))
# this is the function that does most of the work of the bot
def runTime():
# uses the global lasttweet variable, rather than the local one
global lasttweet
# gets the most recent tweet by @ocertat and prints its id
mostrecenttweet = api.user_timeline('ocertat')[0]
print(mostrecenttweet.id)
# compares the two tweets, and tweets a line of Twain
# if there is a new tweet from @ocertat
if mostrecenttweet != lasttweet:
line = tweettext[linenum()]
api.update_status(status=line)
print(line)
# updates lasttweet to the most recent tweet
lasttweet = mostrecenttweet
# runs the main function every 5 seconds
while True:
runTime()
print("sleeping")
time.sleep(5) # Sleep for 5 seconds
# To quit early: CTRL+C and wait a few seconds