Last active
July 22, 2021 15:35
-
-
Save bpluly/6618a32d5c80e3486d3e29c8de518fb4 to your computer and use it in GitHub Desktop.
Tweepy Account Activity Getting DMs
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
# This is a part of a web app that subscribes to all Activity using the Tweepy ActivityAPI module. | |
# This assumes that the authentication and setup of the application connected to a user has been done. | |
# In this case the connection is for a single Twitter Account because it's actually a service handling | |
# everything the backend of zettel.io needs in the way of API and services. | |
# The webhook gets all of the Account Activity and sends each event onto a queue, there's a filter | |
# subscribed to the queue which parses the events and cherry picks the ones that are wanted. | |
# Things that look like dictionaries in this tend to be a database or a document, it's all config related | |
# in this example. | |
from tweepy.binder import bind_api | |
from tweepy.error import TweepError | |
from tweepy.parsers import JSONParser, Parser | |
from tweepy.activityAPI import ActivityAPI | |
# Twitter URL handlers | |
class twsetup: | |
""" Create the webhook, store its ID etc in 'sys' and subscribe the zettel.io twitter account""" | |
def GET(self): | |
# set up | |
logger.info('Entered twSetup') | |
document = sysconfig['sys'] | |
key = document['twitter_access_token_key'] | |
secret = document['twitter_access_token_secret'] | |
auth = tweepy.OAuthHandler(TW_CONSUMER_TOKEN, TW_CONSUMER_SECRET) | |
auth.set_access_token(key, secret) | |
api = ActivityAPI(auth) | |
activityResponse = api.enable_activity(url = WEBHOOK, env = ENV) | |
logger.info(activityResponse) | |
document['tw-webhookid'] = activityResponse['id'] | |
document['tw-webhookurl'] = activityResponse['url'] | |
document['tw-valid'] = activityResponse['valid'] | |
document['tw-created'] = activityResponse['created_at'] | |
document.save() | |
activityResponse = api.subscribe_activity(ENV) | |
logger.info('zettel.io api webhook enabled') | |
return render.tweets(tweets=api.user_timeline()) # this just shows the connection to the account is good. | |
class twsubscribe: | |
""" Subscribe the zettel.io twitter user for the Activity API""" | |
def GET(self): | |
document = sysconfig['sys'] | |
key = document['twitter_access_token_key'] | |
secret = document['twitter_access_token_secret'] | |
auth = tweepy.OAuthHandler(TW_CONSUMER_TOKEN, TW_CONSUMER_SECRET) | |
auth.set_access_token(key, secret) | |
api = ActivityAPI(auth) | |
logger.info('Make subscribe_activity API call') | |
activityResponse = api.subscribe_activity(env=ENV) | |
logger.info('activityResponse') | |
raise web.OK | |
class twwebhook: | |
""" The webhook for all twitter events on the zettel.io account | |
Initially interested in @mentions, then add retweets, quoted tweets and finally private messages.""" | |
def POST(self): | |
#for now just pass on to the SQS queue | |
sqsConn = boto.sqs.connect_to_region('eu-west-2',aws_access_key_id=AmazonAccessKey, aws_secret_access_key=AmazonSecretKey) | |
publishQueue = sqsConn.get_queue(submitQueue) | |
newMessage = Message() | |
newMessage.set_body(web.data()) | |
publishQueue.write(newMessage) | |
raise web.OK | |
def GET(self): | |
""" The CRC Twitter check that the hook is validated""" | |
parameters = web.input() | |
logger.info('twwebhook:CRC check') | |
# create a HMAC SHA-256 hash from incoming token and your TW_CONSUMER secret, base64 the result and return as JSON | |
sha256_hash_digest = hmac.new(TW_CONSUMER_SECRET.encode(), parameters.crc_token.encode(), digestmod=hashlib.sha256).digest() | |
hashresponse = {'response_token': 'sha256=' + base64.b64encode(sha256_hash_digest).decode('utf-8')} | |
web.header('Content-Type','application/json') | |
return json.dumps(hashresponse) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment