forked from emmaggie/sample-python-autoreply
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoResponse.py
More file actions
66 lines (47 loc) · 2.02 KB
/
AutoResponse.py
File metadata and controls
66 lines (47 loc) · 2.02 KB
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
65
66
import ConfigParser
import json
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
from tweepy import API
from nltk.chat import eliza
config = ConfigParser.ConfigParser()
config.read('.twitter')
consumer_key = config.get('apikey', 'key')
consumer_secret = config.get('apikey', 'secret')
access_token = config.get('token', 'token')
access_token_secret = config.get('token', 'secret')
stream_rule = config.get('app', 'rule')
account_screen_name = config.get('app', 'account_screen_name').lower()
account_user_id = config.get('app', 'account_user_id')
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
twitterApi = API(auth)
chatbot = eliza.Chat(eliza.pairs)
class ReplyToTweet(StreamListener):
def on_data(self, data):
print data
tweet = json.loads(data.strip())
retweeted = tweet.get('retweeted')
from_self = tweet.get('user',{}).get('id_str','') == account_user_id
if retweeted is not None and not retweeted and not from_self:
tweetId = tweet.get('id_str')
screenName = tweet.get('user',{}).get('screen_name')
tweetText = tweet.get('text')
chatResponse = chatbot.respond(tweetText)
replyText = '@' + screenName + ' ' + chatResponse
#check if repsonse is over 140 char
if len(replyText) > 140:
replyText = replyText[0:139] + '…'
print('Tweet ID: ' + tweetId)
print('From: ' + screenName)
print('Tweet Text: ' + tweetText)
print('Reply Text: ' + replyText)
# If rate limited, the status posts should be queued up and sent on an interval
twitterApi.update_status(status=replyText, in_reply_to_status_id=tweetId)
def on_error(self, status):
print status
if __name__ == '__main__':
streamListener = ReplyToTweet()
twitterStream = Stream(auth, streamListener)
twitterStream.userstream(_with='user')