Post Tweets when approved in Kintone using AWS Lambda

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:

  1. An employee registers a tweet as well as the datetime to post the tweet in a Kintone record.
  2. A reviewer checks the tweet contents and approves or rejects it.
  3. 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:

  1. When the set datetime passes, the Lambda function is triggered and retrieves the tweet data from Kintone.
  2. The data is tweeted.
  3. 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.

image.png

 

Notes:

  1. This customization used a Node.js v12.18.0 environment.
  2. 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. 

Screen_Shot_2020-06-15_at_3.23.29_PM.png

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.

 

Screen_Shot_2020-06-10_at_2.44.21_PM.png

 

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.

 

Screen_Shot_2020-06-10_at_2.47.53_PM.png

 

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.

 

twitter_03.png

 

Click Generate above the Access token & access token secret box.

 

twitter_04_01.png

 

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.

 

twitter_04.png

 

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.

 

lambda_01.png

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.

 

lambda_02.png

 

After creating the function, click Add trigger.

 

lambda_03.png

 

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.

 

eventbridge.png


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.

 

Screen_Shot_2020-06-10_at_6.34.36_PM.png

 

After uploading the zip file, navigate to Environment variables and click Manage environment variables.

 

Screen_Shot_2020-06-10_at_6.39.33_PM.png

 

Click Add environment variable to create a new row of variable settings.

 

Screen_Shot_2020-06-10_at_6.39.41_PM.png

 

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.

 

edit_environment_variables.png

 

Finally, navigate down to the Basic settings and click Edit.

 

edit_basic_settings.png 

 

Enter the settings as follows, checking that the Handler, Timeout, and Execution role values are correct. Click to save when done.

 

edit_basic_settings_2.png

Test the Configuration

Register tweet data to Kintone and proceed the process management status to Tweet scheduled.

 

kintone_record.png

 

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.

 

records.png 

Finally

Note that images may be added to the tweet with the following conditions:

  1. Accepted file types: JPG, PNG, GIF, WEBP
  2. Maximum file size: 5MB
  3. Maximum static images in one tweet (JPG, PNG, WEBP): Up to 4
  4. Maximum GIFs in one tweet: 1 
  5. 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!

 

Was this article helpful?
0 out of 0 found this helpful
Do you have any questions or issues related to this article?
Please share your views with us in the Community forums!