forked from csclubiu/NoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
64 lines (53 loc) · 2 KB
/
bot.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
import tweepy
from datetime import datetime
from secret import consumer_key, consumer_secret, access_token, access_secret, handle
# get authentication info
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
# log into the API
api = tweepy.API(auth)
print('[{}] Logged into Twitter API as @{}\n-----------'.format(
datetime.now().strftime("%H:%M:%S %Y-%m-%d"),
handle
))
# string array of words that will trigger the on_status function
trigger_words = [
'@' + handle # respond to @mentions
]
# override the default listener to add code to on_status
class MyStreamListener(tweepy.StreamListener):
# listener for tweets
# -------------------
# this function will be called any time a tweet comes in
# that contains words from the array created above
def on_status(self, status):
# log the incoming tweet
print('[{}] Received: "{}" from @{}'.format(
datetime.now().strftime("%H:%M:%S %Y-%m-%d"),
status.text,
status.author.screen_name
))
# get the text from the tweet mentioning the bot.
# for this bot, we won't need this since it doesn't process the tweet.
# but if your bot does, then you'll want to use this
message = status.text
# after processing the input, you can build your output
# into this variable. again, since we're just reply "No.",
# we'll just set it as that.
response = "No."
# respond to the tweet
api.update_status(
status=response,
in_reply_to_status_id=status.id
)
print('[{}] Responded to @{}'.format(
datetime.now().strftime("%H:%M:%S %Y-%m-%d"),
status.author.screen_name
))
# create a stream to receive tweets
try:
streamListener = MyStreamListener()
stream = tweepy.Stream(auth = api.auth, listener=streamListener)
stream.filter(track=trigger_words)
except KeyboardInterrupt:
print('\nGoodbye')