Skip to content

Instantly share code, notes, and snippets.

@utgwkk
Created October 9, 2015 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save utgwkk/8ffa37b7da50fe28054a to your computer and use it in GitHub Desktop.
Save utgwkk/8ffa37b7da50fe28054a to your computer and use it in GitHub Desktop.
Posts a URL of the tweet you favorite to a Slack channel if it has a media.
import tweepy
import requests
token = ""
slackURL = "https://slack.com/api/"
params = {'token': token, 'channel': '#random', 'text': '', 'as_user': 'true'}
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
me = api.me().screen_name
def create_twitter_url(id_str, screen_name):
return "https://twitter.com/"+screen_name+"/status/"+id_str
class MyStream(tweepy.StreamListener):
def on_event(self, status):
if status.event == 'favorite' and status.source['screen_name'] == me and 'extended_entities' in status.target_object:
params['text'] = create_twitter_url(status.target_object['id_str'], status.target_object['user']['screen_name'])
r = requests.post(slackURL + "chat.postMessage", params=params)
if __name__ == '__main__':
stream = tweepy.Stream(auth, MyStream())
stream.userstream()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment