Introduction
This article introduces how to utilize AWS Lambda so that Kintone can be used to make drafts of official tweets to be approved by a reviewer and then scheduled to post automatically to Twitter.
The general flow of the operation is as follows:
- An employee registers a tweet as well as the datetime to post the tweet in a Kintone record.
- A reviewer checks the tweet contents and approves or rejects it.
- If approved, the tweet is posted at the set datetime.
AWS Lambda is used to host the function that checks for ready tweets and posts them to Twitter at their defined times. It also updates the Kintone record by changing the status to Complete and inserting a link to the posted tweet in the record.
Customization Overview
The general flow of the customization is as follows:
- When the set datetime passes, the Lambda function is triggered and retrieves the tweet data from Kintone.
- The data is tweeted.
- The Kintone record is updated to change the process management status and the URL to the tweet is added to the record.
The following diagram shows the general flow of the customization.
Notes:
- This customization used a Node.js v12.18.0 environment.
- To use the Twitter API, a Twitter developer account is needed. For details, refer to the the Apply for Access page of the Twitter developer website.
Set Up the Kintone App
Create the form
Refer to the following table for the fields and their individual settings to add to the App form.
Field Type |
Field Name |
Field Code |
Remarks |
---|---|---|---|
Date and time |
Tweet date and time |
tweetDate |
Set as required |
Text area |
Tweet content |
tweetBody |
Set as required |
Attachment |
Image file |
imageFile |
|
Link (URL) |
Tweet URL |
tweetUrl |
* A URL to the tweet is automatically set in this field after the tweet is posted. |
Generate an API token
Create an API token with view and edit permissions. For information on generating API tokens, refer to Using API token authentication.
Process management
In the App settings, navigate to Process Management and click the Enable process management checkbox. For the Status Settings and Process Flow Settings, refer to the following image. Do not set a default assignee for the Tweet scheduled status. If set, the status cannot be updated with the API token.
Set the Twitter Settings
Prepare the Twitter account
Log in to Twitter if the necessary Twitter account already exists. If not, create a new Twitter account from Twitter.com.
Apply for a developer account
A Twitter developer account is required to use the Twitter API. For those using the Twitter API for the first time, refer to the Apply for access page of the Twitter developer website.
Register a new application
Access the Apps page of the Twitter developer website.
Click Create an App to create a new application. Enter in an appropriate name for the app under App name, and a description of the application in Application description. The website URL can be a dummy URL such as http://sample.com.
In the Tell us how this app will be used field, fill in a brief description of the application and how it will be used.
Click Create to save the app.
Next, navigate to the Keys and tokens tab and make a note of the API key and API secret key under Consumer API keys.
Click Generate above the Access token & access token secret box.
Make note of the displayed Access token and Access token secret. They will not be displayed again and if forgotten will need to be regenerated.
Create an executable of the Lambda function
This step must be done in an environment where Node.js is installed.
Save the following sample code as a file named index.js.
Run the following commands to install the module and create the ZIP file (the Lambda function executable).
If an error occurs with the ZIP command, check that the file index.js and the directory node_modules exist in the hierarchy.
Set the AWS Settings
Prepare the AWS account
Log in to AWS if the appropriate AWS account exists. If necessary, refer to the Are you a first-time user of AWS Lambda? of the What is AWS Lambda? documentation to create a new account.
Create an execution role
Refer to the AWS Lambda execution role documentation to create a role to execute Lambda.
In this sample, the role is created with the name AWSLambdaExecute as an example.
Create a new Lambda function
In AWS Lambda, click Create function.
Choose Author from scratch and enter in the rest of the settings as shown in the image below. The Function name can be anything, the Runtime should be set to Node.js 12.x, and the Execution role should be set as the role that was just created in the previous step.
After creating the function, click Add trigger.
Using the drop down, select EventBridge (CloudWatch Events) as the trigger.
Enter in any value for Rule and Rule name, and under Schedule expression, set the rate to 1 minute. Note that usage of EventBridge is charged based on the number of events published. Refer to the Amazon EventBridge Pricing page for details.
Click the kintone-to-twitter box to return to the Lambda function settings and click Actions to open up the Actions menu. Click to Upload a .zip file and upload kintone-to-twitter.zip, the Lambda function execution file created earlier.
After uploading the zip file, navigate to Environment variables and click Manage environment variables.
Click Add environment variable to create a new row of variable settings.
Referring to the following section of the index.js code, create a row of variable settings for each variable, matching up the capitalized portion of the variables with the values obtained earlier.
The environment variable settings should look like so.
Finally, navigate down to the Basic settings and click Edit.
Enter the settings as follows, checking that the Handler, Timeout, and Execution role values are correct. Click to save when done.
Test the Configuration
Register tweet data to Kintone and proceed the process management status to Tweet scheduled.
Check that the tweet is posted when the set datetime has passed. If the configuration is successful, the record status should change to Complete and the link to the posted tweet should be entered in the Link field. The image below shows a few completed tweet records in addition to others that are ready to be tweeted or are still in review.
Finally
Note that images may be added to the tweet with the following conditions:
- Accepted file types: JPG, PNG, GIF, WEBP
- Maximum file size: 5MB
- Maximum static images in one tweet (JPG, PNG, WEBP): Up to 4
- Maximum GIFs in one tweet: 1
- GIF files must be uploaded alone and cannot be mixed with other image types.
The configuration in this customization article can be emulated with other services as well, to post data from Kintone to a service using AWS Lambda as the bridge. Try setting up more automated tasks using Kintone and AWS Lambda to optimize workflows!