Coder Social home page Coder Social logo

jimmysoda / wikiquote-tweet-awslambda Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 22 KB

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

License: MIT License

Python 100.00%
aws aws-lambda aws-iot aws-iot-python aws-iot-button wikiquote pixabay twitter amazon-dash-button

wikiquote-tweet-awslambda's Introduction

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

wikiquote-tweet-awslambda's People

Contributors

jimmysoda avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.