Refactored Code to use OOP

Added request_handler
Twitter "commands" are in separate file
main
Hammy 4 years ago
parent 86c089e0c5
commit c58a46ee71

@ -1,44 +1,26 @@
#!/usr/bin/env python
import random import random
from twython import Twython from twython import Twython
from twitter.bot.aws_secrets import get_secret from twitter.bot.aws_secrets import get_secret
from twitter.bot.commands import send_tweet
# Create the Twython Twitter client using the credentials stored in SSM
twitter = Twython(
get_secret("CONSUMER_KEY"), class Winston:
get_secret("CONSUMER_SECRET"), bot = Twython(
get_secret("ACCESS_TOKEN_KEY"), get_secret("CONSUMER_KEY"),
get_secret("ACCESS_TOKEN_SECRET") get_secret("CONSUMER_SECRET"),
) get_secret("ACCESS_TOKEN_KEY"),
get_secret("ACCESS_TOKEN_SECRET")
# Sample random tweets )
potential_tweets = [
"Hello! I'm Winston From Overwatch!", @staticmethod
"Winston! From! Overwatch!", def potential_tweets():
"Winston? From Overwatch?" return [
] "Hello! I'm Winston From Overwatch!",
"Winston! From! Overwatch!",
"Winston? From Overwatch?"
def send_tweet(tweet_text): ]
"""Sends a tweet to Twitter"""
@staticmethod
twitter.update_status(status=tweet_text) def request_handler():
send_tweet(random.choice(Winston.potential_tweets()))
def handler(event, context):
"""Sends random tweet from list of potential tweets"""
send_tweet(random.choice(potential_tweets))
def follow_someone(screen_name):
twitter.create_friendship(screen_name=screen_name)
def follow_fernando():
follow_someone("fmc_sea")
def like_tweet(tweet_id):
twitter.create_favorite(id=tweet_id)

@ -0,0 +1,19 @@
from twitter.bot.bot import Winston
def send_tweet(tweet_text):
"""Sends a tweet to Twitter"""
Winston.bot.update_status(status=tweet_text)
def follow_someone(username):
"""Follows someone based on given username"""
Winston.bot.create_friendship(username=username)
def like_tweet(tweet_id):
"""Likes a tweet based on it's ID"""
Winston.bot.create_favorite(id=tweet_id)

@ -0,0 +1,6 @@
from twitter.bot.bot import Winston
def handler(event, context):
"""Sends random tweet from list of potential tweets"""
Winston.request_handler()
Loading…
Cancel
Save