diff --git a/README.md b/README.md new file mode 100644 index 0000000..f021eac --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# NoBot +Template code for the CS Club / General Motors Twitter API workshop/challenge. + +Whenever someone mentions the bot, it will respond with "No." See comments in the code on how to modify it for your own purposes. + +## Setup +1. Clone this repository or just copy the code from `bot.py` +1. Run `pip install -r requirements.txt` to install dependencies +1. Modify the code to your liking +1. To run the bot, just run `python bot.py` \ No newline at end of file diff --git a/__pycache__/secret.cpython-37.pyc b/__pycache__/secret.cpython-37.pyc new file mode 100644 index 0000000..ac8a209 Binary files /dev/null and b/__pycache__/secret.cpython-37.pyc differ diff --git a/bot.py b/bot.py new file mode 100644 index 0000000..4ea0ccf --- /dev/null +++ b/bot.py @@ -0,0 +1,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') \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..69ae13e --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +tweepy \ No newline at end of file diff --git a/secret.py b/secret.py new file mode 100644 index 0000000..f009f0d --- /dev/null +++ b/secret.py @@ -0,0 +1,8 @@ +import os + +# retrieve keys from environment variables +consumer_key = os.environ['TWBOT_CON_KEY'] +consumer_secret = os.environ['TWBOT_CON_SECRET'] +access_token = os.environ['TWBOT_ACCESS_TOKEN'] +access_secret = os.environ['TWBOT_ACCESS_SECRET'] +handle = os.environ['TWBOT_HANDLE'] \ No newline at end of file