Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import ConfigParser ,json ,tweepy
- 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')
- account_user_id = config.get('app', 'account_user_id')
- auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
- auth.secure = True
- auth.set_access_token(access_token, access_token_secret)
- twitterApi = tweepy.API(auth)
- chatbot = eliza.Chat(eliza.pairs)
- print 'Start'
- class ReplyToTweet(tweepy.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:137] + '...'
- 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 = tweepy.Stream(auth, streamListener)
- twitterStream.userstream(_with='user')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement