Created
December 24, 2015 03:58
-
-
Save matmoody/3b0fda986731430c3339 to your computer and use it in GitHub Desktop.
When specific keyword is mentioned on Twitter, send the tweet to one of your Slack Channels.
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
from tweepy.streaming import StreamListener | |
from tweepy import OAuthHandler | |
from tweepy import Stream | |
import json | |
import requests | |
client_key = "TwitterConsumerKey" | |
client_secret = "TwitterConsumerSecret" | |
token = "TwitterAppAccessToken" | |
token_secret = "TwitterAppAccessTokenSecret" | |
# Send Notification to your Slack Channel | |
def notify_slack(text, tweet_id): | |
url = "https://hooks.sl" | |
payload = {"text": text + " Go To This Tweet: http://twitter.com/statuses/%s" % tweet_id} | |
r = requests.post(url, data=json.dumps(payload)) | |
print r.text | |
class TweetListener(StreamListener): | |
def on_data(self, data): | |
tweet = json.loads(data) | |
# Send tweet text & link to each tweet to slack | |
notify_slack(tweet['text'], tweet['id']) | |
return True | |
def on_error(self, status): | |
print "Error: %s" % status | |
listener = TweetListener() | |
auth = OAuthHandler(client_key, client_secret) | |
auth.set_access_token(token, token_secret) | |
stream = Stream(auth, listener) | |
stream.filter(track=['festivus']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment