This is a continuation of the article "I tried to reproduce Mr. Saito from" Eine Kleine Nachtmusik "as Mr. Sakurai of Mr. Children" Become. If you are interested, I recommend you to read the above article first.
First of all, I will introduce what I made.
If you register as a friend here, you can try it!
https://lin.ee/WEqJMO7
It's simple to use. I will talk to Mr. Sakurai. Then, Mr. Sakurai will sing a part of Mr. Children's song that suits that feeling. (To be exact, it will reply in text) Behind the scenes, I'm using COTOHA's sentiment analysis API It returns a part of the lyrics that are close to the sent sentence and the emotion score. Therefore, please understand that there are many cases where the contents do not match very much. (Please use it as if you were playing.)
I feel that I am encouraging with expressions such as flickering hope for the tiredness of self-restraint caused by corona.
Please refer to Previous article for the part registered in the DB that stores lyrics phrases and emotion scores. This time, I will introduce the configuration from LINE to the reply. The configuration is simple and uses LINE's Messaging API, AWS's API Gateway, Lambda, RDS, and COTOHA's API.
①-②: Use LINE Messaging API to call API Gateway from LINE by Web Hook ③: Call the Lambda function created by setting API Gateway ④: Get the emotion score of the message sent by calling the COTOHA API ⑤: Get lyrics phrases with similar emotion scores from the DB created on RDS ⑥-⑦: Return the acquired phrase as a LINE reply using the Messaging API.
I will explain API Gateway, Lambda, and LINE Messaging API that may be difficult to set up. There are no particularly difficult settings for RDS and COTOHA, so I will omit them.
(1) Create a new API from API Gateway
(2) Created as a new REST API
(3) Creating a new method
Create by POST.
(4) Lambda registration
Enter the integration type "Lambda function" and the Lambda name in the Lambda function so that Lambda can execute it.
(5) Click the integration request
(6) HTTP header settings
Register the name as Authentication and the mapping source as'Bearer \ <LINE access token >'. Obtaining a LINE access token will be described later. After registering, enter it here.
(7) API deployment
Click Actions → Deploy API Enter the stage name and deploy. (Here, it is called prod)
(8) Issuance of API URI
When deployed, the URL should be published in Stage → prod → POST on the left menu. I will register it as a Webhook of the LINE Messaging API later, so let's copy the URL somewhere.
Please refer to lambda / lambda.py of GitHub for the source code. Please note that the DB connection information, COTOHA API connection information, and LINE API token information are registered in the Lambda environment variables.
Also, mysql-connector-python is not included by default, so you need to put it on Lambda. This time I used a Lambda layer. Please refer to here for how to use it.
For first-time users, please refer to here for channel creation.
The minimum required settings are as follows.
(1) Webhook settings
Register the URL created by API Gateway in advance.
(2) Issuance of access token
(3) Turn off the automatic response setting
If you do not set this, you will get an irrelevant reply.
(4) Confirmation of Messaging API settings
Jump to LINE Official Account Manager Click the Messaging API on the left menu and check if the Webhook URL is registered. If it is not registered, enter the API URL in the same way as (1).
I referred to the following article.
LINE Bot development with API Gateway + Lambda
Recommended Posts