[PYTHON] I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda

Introduction

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.

What I made

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.

System overview

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.

sakuraisan-image2 (1).png

①-②: 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.

Detailed description of each service

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.

API Gateway settings

(1) Create a new API from API Gateway api-1.png

(2) Created as a new REST API

api-2.png

(3) Creating a new method

Create by POST.

api-3.png

(4) Lambda registration

Enter the integration type "Lambda function" and the Lambda name in the Lambda function so that Lambda can execute it.

スクリーンショット 2020-08-17 22.33.40.png

(5) Click the integration request

api-4.png

(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.

api-5.png

(7) API deployment

Click Actions → Deploy API Enter the stage name and deploy. (Here, it is called prod)

api-7.png

(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.

api-6.png

Lambda settings

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.

lambda-1.png

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.

LINE Messaging API settings

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.

line-1.png

(2) Issuance of access token

line-2.png

(3) Turn off the automatic response setting

If you do not set this, you will get an irrelevant reply.

line-3.png

(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).

line-5.png

line-4.png

reference

I referred to the following article.

LINE Bot development with API Gateway + Lambda

Recommended Posts

I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I tried to make a Web API
LINE BOT with Python + AWS Lambda + API Gateway
I tried to make a castle search API with Elasticsearch + Sudachi + Go + echo
I tried to make a simple image recognition API with Fast API and Tensorflow
[5th] I tried to make a certain authenticator-like tool with python
Rubyist tried to make a simple API with Python + bottle + MySQL
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
I made a Twitter Bot with Go x Qiita API x Lambda
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried ChatOps with Slack x API Gateway x Lambda (Python) x RDS
I tried to make a strange quote for Jojo with LSTM
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
I tried to make a mechanism of exclusive control with Go
I made a stamp substitute bot with line
Python: I tried to make a flat / flat_map just right with a generator
How to make an artificial intelligence LINE bot with Flask + LINE Messaging API
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
I want to make a game with Python
I tried to make my own high school girl BOT with Rinna style with LINE BOT (Python & Heroku)
I tried to make a calculator with Tkinter so I will write it
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
I made a LINE Bot with Serverless Framework!
I tried to operate Linux with Discord Bot
Make a LINE WORKS bot with Amazon Lex
I tried to make a url shortening service serverless with AWS CDK
I tried to make a ○ ✕ game using TensorFlow
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to make a simple mail sending application with tkinter of Python
When I tried to make a VPC with AWS CDK but couldn't make it
[Patent analysis] I tried to make a patent map with Python without spending money
Try implementing a Cisco Spark bot with AWS Lambda + Amazon API Gateway (Python)
I tried to extract a line art from an image with Deep Learning
I tried to make a translation BOT that works on Discord using googletrans
[AWS] I made a reminder BOT with LINE WORKS
I tried to make a "fucking big literary converter"
I tried to create a table only with Django
I made a household account book bot with LINE Bot
Make a morphological analysis bot loosely with LINE + Flask
I tried to draw a route map with Python
I made a LINE BOT with Python and Heroku
I tried to uncover our darkness with Chatwork API
I tried to automatically generate a password with Python3
I tried to make an OCR application with PySimpleGUI
[Super easy] Let's make a LINE BOT with Python.
I want to bind a local variable with lambda
Make a LINE BOT (chat)
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to make a real-time sound source separation mock with Python machine learning
A Python beginner made a chat bot, so I tried to summarize how to make it.
I tried to make creative art with AI! I programmed a novelty! (Paper: Creative Adversarial Network)
A beginner tried coloring line art with chainer. I was able to do it.
I tried to implement a volume moving average with Quantx
How to make a Cisco Webex Teams BOT with Flask
I made a LINE BOT that returns parrots with Go