wikiquote-tweet-awslambda

An AWS Lambda to tweet a random quote from wikiquote.org.


Project maintained by jimmysoda Hosted on GitHub Pages — Theme by mattgraham

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:

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