forked from csclubiu/NoBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit f691eb3
Showing
5 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
tweepy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |