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.
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.)
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
The intended use is asked. This time I will create a bot as a hobbyist, so select "Making a Bot"
After that, I will answer the questions in a straightforward manner.
It may take some time for some people because they need to answer in English.
Finally, a confirmation screen will appear, so click "Looks good!".
Read the usage treaty.
If this screen appears, it's OK. A confirmation email has arrived at the registered email address, so open it and register.
When you log in to the Twitter developer screen, you will be asked for the name of the app, so answer appropriately.
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.
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.
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".
Since it is Read (read only), change it to "Read + Write + Direct Message".
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.
This completes the Twitter API settings.
Create a function from "Lambda> Function". Select Serverless Application Repository> Public Applications, search for "iot" and select iot-twitter-bridge.
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.
You have now created a Lambda function.
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.
Create a test event. Create a twitterTest event using the hello-worrld template as shown in the image below.
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.
Also check the Twitter side. 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}]}
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.
I am always grateful.
Creating a twitter app https://yosiakatsuki.net/blog/create-twitter-application/
Recommended Posts