Skip to content

jimmysoda/wikiquote-tweet-awslambda

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wikiquote-tweet-awslambda

An AWS Lambda to tweet a random quote from wikiquote.org and a matching image from pixabay.com upon pressing an AWS IoT Button.

Summary

An AWS Lambda application written in Python 3.x to tweet a randomly-selected inspirational quote from wikiquote.org with a matching image from pixabay.com. The AWS Lambda expects an event in the format sent by the AWS IoT Button.

Learn more about the AWS IoT button, i.e., the programmable Amazon Dash button, at https://aws.amazon.com/iotbutton/.

Follow @asTheQuoteSays on Twitter for a demo!

This code is released using the MIT license. See LICENSE for more details.

Requirements

This lambda function retrieves Pixabay and Twitter API keys from environment variables:

  • PIXABAY_KEY: The Pixabay API key
  • TWITTER_CONSUMER_KEY: The Twitter API consumer key
  • TWITTER_CONSUMER_SECRET: The Twitter API consumer secret
  • TWITTER_ACCESS_TOKEN: The Twitter API access token
  • TWITTER_ACCESS_SECRET: The Twitter API access token secret

Visit the Pixabay API and Twitter API documentaiton sites for instructions on how to obtain these keys.

Setting the PHONE_NUMBER environment variable and allowing sns:Publish via AWS IAM for the lambda function enables sending a notification SMS with the payload sent by the button and the URL to the resulting tweet.

The following function policy copied from the IoT example lambda functions allows limits AWS SNS publish to SMS but not topics or endpoints.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "sns:Publish"
            ],
            "Resource": [
                "*"
            ]
        },
        {
            "Effect": "Deny",
            "Action": [
                "sns:Publish"
            ],
            "Resource": [
                "arn:aws:sns:*:*:*"
            ]
        }
    ]
}

Dependencies

The requests and tweepy libraries need to be uploaded to AWS Lambda in a deployment package. Remember to install these dependencies via pip at the root of the package and to set the value of the Handler field in AWS Lambda to wikiquote_tweet_awslambda.lambda_handler.

Usage

The AWS Lambda function (lambda_handler) calls the tweet_inspirational_quote function with a different language depending on the click type received in the event argument:

Click Type Language Function Call
SINGLE English tweet_inspirational_quote('en')
DOUBLE Spanish tweet_inspirational_quote('es')
LONG Portuguese tweet_inspirational_quote('pt')

Currently only English (en) and Spanish (es) are well tested and supported. Any language other than these two and Portuguese (pt) is automatically mapped to English.

Acknowledgements

  • Thanks to @onema for the lambda-tweet AWS Lambda application that I used as a reference on how to use tweepy
  • Thanks to @natetyles for the wikiquotes-api repository that I used as a reference on how to call the wikiquote.org API and parse its response
  • Thanks to @dev-techmoe for the tweepy status structure gist that I used as a reference to parse the results from tweepy