Skip to content

Instantly share code, notes, and snippets.

@shrys
Last active June 2, 2020 21:47
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 shrys/8495695587fa0f2f062e42bc4e155dfa to your computer and use it in GitHub Desktop.
Save shrys/8495695587fa0f2f062e42bc4e155dfa to your computer and use it in GitHub Desktop.
tweet/retweet notification of a user on slack hook
import tweepy
import time
import sys
import inspect
import requests
import json
consumer_key = 'xxxxxxxxxxxxxxxxxxx'
consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
access_token_secret = 'xxxxxxxxxxxxxxxx'
slack_hook = 'https://hooks.slack.com/services/TXXXXXXXXXX/BXXXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX'
screen_name = '@name'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
auth.secure = True
api = tweepy.API(auth)
user = api.get_user(screen_name=screen_name)
class CustomStreamListener(tweepy.StreamListener):
def on_status(self, status):
if(
(
status.in_reply_to_status_id is None and
not
(
hasattr(status, 'retweeted_status') and
(
status.retweeted_status.retweet_count == 0 or
status.retweeted_status.author.id == user.id
)
)
)
):
requests.post(
url=slack_hook,
data=json.dumps({'text': status.text}),
headers={'Content-type': 'application/json'}
)
streamListener = CustomStreamListener()
stream = tweepy.Stream(auth=api.auth, listener=CustomStreamListener())
stream.filter(follow=[str(user.id)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment