Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fergusch committed Oct 24, 2019
0 parents commit f691eb3
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`
Binary file added __pycache__/secret.cpython-37.pyc
Binary file not shown.
64 changes: 64 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -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')
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tweepy
8 changes: 8 additions & 0 deletions secret.py
Original file line number Diff line number Diff line change
@@ -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']

0 comments on commit f691eb3

Please sign in to comment.