[PYTHON] Tweet from AWS Lambda

Introduction

We will continue to create an automatic notification function for leaving information. In the previous article, I was able to send location information from my smartphone to AWS. Next, use AWS Lambda to connect to Twitter.

About notification means

As a premise, the notification destination is assumed to be my wife's iPhone, so I do not want to make any special settings. The following methods can be used to easily notify a third-party smartphone from AWS. ・ LineBot ・ TwitterBot ·Email Of these, we will notify you using Twitter, which seems to be the fastest to implement. (Please let me know if there is an easier way.)

Twitter API registration

Like Bot, in order to tweet using a program, you need to register to use the "Twitter API". If you haven't registered for a Twitter account yet, please do so before registering. If you do not register your email address in your account, you will not be able to register to use the "Twitter API".

Access the following site and register to use the Twitter API. https://developer.twitter.com/en/apps

7.PNG

The intended use is asked. This time I will create a bot as a hobbyist, so select "Making a Bot"

8.PNG

After that, I will answer the questions in a straightforward manner. 9.PNG

It may take some time for some people because they need to answer in English. a.PNG

Finally, a confirmation screen will appear, so click "Looks good!". b.PNG

Read the usage treaty. c.PNG

If this screen appears, it's OK. A confirmation email has arrived at the registered email address, so open it and register. d.PNG

Twitter API initial settings

When you log in to the Twitter developer screen, you will be asked for the name of the app, so answer appropriately.

e.PNG

Here you will see the key to tweet from the program, but you can safely ignore it as it will be reissued later. Click Test an endpoint.

f.PNG

After registering the app, set the permissions. In the initial state, you can only read tweets. If you do not set this permission, you will not be able to tweet from the program, and an error will occur later.

Open the "Developer Portal" at the top right of the screen.

F2.PNG

Select the app name you named earlier from "Project & Apps" on the tab on the left side of the screen. Then, the setting screen of the application will appear, so click "Edit" of "App Permissions".

h.PNG

Since it is Read (read only), change it to "Read + Write + Direct Message".

q.PNG

Next, check the Key and Token to use the API. Regenerate "API Key & Secret" and make a note of (1) API key and (2) API key secret. Also, generate "Access Token & Secret" and make a note of ③ Access token, Access and ④ token secret. You'll enter these four pieces of information into AWS Lambda later.

g.PNG

This completes the Twitter API settings.

Build AWS Lambda

Create a function from "Lambda> Function". Select Serverless Application Repository> Public Applications, search for "iot" and select iot-twitter-bridge. 6.PNG

In the application settings, enter the four information you noted earlier in the Twitter API. The terminology is not unified, but CONSUMER_KEY is the API key. 図1.png

You have now created a Lambda function. k.PNG

Let's check the default function code. In json format, deviceID and incomingText are input from the trigger, and it seems to be a mechanism to tweet the information. For the time being, leave the function as it is and test the connection with the Twitter API. n.PNG

test

Create a test event. Create a twitterTest event using the hello-worrld template as shown in the image below. r.PNG

For the time being, paste the contents.

{
  "deviceID": "6060",
  "incomingText": "twitter from aws"
}

Test If Success is displayed as shown in the image below, it is successful. t.PNG

Also check the Twitter side. u.PNG Thank you for your support.

By the way, if you test with the same content, you will get an error. It seems that it is a specification of Twitter to prevent continuous posting with the same content.

details: {u'errors': [{u'message': u'Status is a duplicate.', u'code': 187}]}

in conclusion

I was able to tweet from AWS for the purpose of creating an automatic leave notification function. After that, it seems that you can do it by connecting Iot and Lambda and playing with the function a little.

Reference URL

I am always grateful.

Creating a twitter app https://yosiakatsuki.net/blog/create-twitter-application/

Recommended Posts

Tweet from AWS Lambda
Run BigQuery from Lambda
Try AWS Lambda Destinations
Terraform configured to launch AWS Lambda from Amazon SQS
[Python] Scraping in AWS Lambda
The simplest AWS Lambda implementation
Query Athena from Lambda Python
Web scraping using AWS lambda
Notes on accessing SQS from AWS VPC Lambda via endpoint
A quick explanation from creating AWS Lambda Layers to linking
Send a request from AWS Lambda to Amazon Elasticsearch Service
A story that I was addicted to calling Lambda from AWS Lambda.
lambda
Get celebrity tweet history from twitter
Summary if using AWS Lambda (Python)
[AWS] Create API with API Gateway + Lambda
[Lambda] [Python] Post to Twitter from Lambda!
Write AWS Lambda function in Python
Things to note when running Python on EC2 from AWS Lambda
Manipulating kintone data with Python & C Data ODBC Driver from AWS Lambda
Try giving AWS Lambda environment variables?
Using Lambda with AWS Amplify with Go
Operate Dynamodb from Lambda like SQL
Notify HipChat with AWS Lambda (Python)
AWS Lambda Development My Best Practices
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
Python calling Google Cloud Vision API from LINE BOT via AWS Lambda
Understanding from the mechanism Twilio # 3-1 --AWS API Gateway + Lambda implementation Walkthrough (Part 1)
Modules cannot be imported in Python on EC2 run from AWS Lambda
Change AWS EC2 instance from t2 to t3
[AWS] Using ini files with Lambda [Python]
Create wordcloud from your tweet with python3
Environment when AWS Lambda includes native libraries
Tweet from python with Twitter Developer + Tweepy
Regularly post to Twitter using AWS lambda!
[AWS] Link Lambda and S3 with boto3
[Python] Run Headless Chrome on AWS Lambda
Connect to s3 with AWS Lambda Python
[AWS] Do SSI-like things with S3 / Lambda
Summary of how to write AWS Lambda
Python + Selenium + Headless Chromium with aws lambda
I just did FizzBuzz with AWS Lambda
[AWS] Detects the specified character string from the Lambda execution log and notifies slack
[AWS; Introduction to Lambda] 2nd; Extract sentences from json file and save S3 ♬
Create a setting in terraform to send a message from AWS Lambda Python3.8 to Slack
[AWS] Addressing an issue where Lambda called from CodePipeline is still in progress