Skip to content

Instantly share code, notes, and snippets.

@matmoody
Created December 24, 2015 03:58
Show Gist options
  • Save matmoody/3b0fda986731430c3339 to your computer and use it in GitHub Desktop.
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.
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