Create a LINE BOT with Minette for Python

Speaking of BOT in Japan, it is LINE BOT. Here, we will introduce how to create a BOT application framework using Minette for Python.

Things to prepare in advance

--Channel Secret and Channel Access Token of LINE Messaging API --docomo API key for chat API (optional. In that case, it will be a return BOT)

Installation of dependent libraries

Install the pytz and requests required for Minette to work, and the web application framework flask and LINE SDK required to launch the LINE endpoint. If you have already installed it, you can skip this step.

$ pip install pytz
$ pip install requests
$ pip install Flask
$ pip install line-bot-sdk

Minette installation

As the title suggests, we use the BOT framework Minette. For more information on Minette, please refer to the following articles.

-Introduction of BOT Framework Minette for Python

Installation is OK with a single pip command.

$ pip install minette

Let's check the operation with the Echolalia BOT.

$ minette
user> hello
minette> You said: hello

Publishing to the Internet

Since the inbound of the LINE Messaging API is designed to hit the webhook specified by the developer and pass a message, it is necessary to make the machine running Minette accessible from the Internet.

I think there are various methods, but here I will explain the procedure using ngrok, which feels easy and easy.

ngrok installation

Roughly speaking, ngrok is a routing and tunneling tool (recognition) that supports various protocols. For details, see ngrok official, and download and unzip the binary according to the platform from the download page.

When you unzip it, one executable file with the name ngrok will appear, so hit this one as follows. If you execute it normally, you will not be able to work after that, so I try to execute it in the background.

$ ./ngrok http 5050 -log=stdout > ngrok.log &

Check the Internet URL as well as check the operation.

$ curl http://localhost:4040/api/tunnels

{"tunnels":[{"name":"command_line","uri":"/api/tunnels/command_line","public_url":"https://abcd1234.ngrok.io","proto":"https","config":{"addr":"localhost:5050","inspect":true},"metrics":{"conns":{"count":0,"gauge":0,"rate1":0,"rate5":0,"rate15":0,"p50":0,"p90":0,"p95":0,"p99":0},"http":{"count":0,"rate1":0,"rate5":0,"rate15":0,"p50":0,"p90":0,"p95":0,"p99":0}}},{"name":"command_line (http)","uri":"/api/tunnels/command_line+%28http%29","public_url":"http://abcd1234.ngrok.io","proto":"http","config":{"addr":"localhost:5050","inspect":true},"metrics":{"conns":{"count":0,"gauge":0,"rate1":0,"rate5":0,"rate15":0,"p50":0,"p90":0,"p95":0,"p99":0},"http":{"count":0,"rate1":0,"rate5":0,"rate15":0,"p50":0,"p90":0,"p95":0,"p99":0}}}],"uri":"/api/tunnels"} 

The information was returned in a row, but since https://abcd1234.ngrok.io shown in the item ofpublic_urlis the URL for accessing the Raspberry Pi issued by ngrok, this Make a note of it by copying it.

Set to LINE developers' Webhook URL

Set / api of the URL (or the URL of the execution environment itself) issued from ngrok earlier.

Webhook URLの設定

It is like this. Note that the service has not been started yet, so even if you press Verify, it will fail.

Launched as an endpoint for the LINE Messaging API

The minette command allows you to launch a sample implementation of the LINE Messaging API endpoint in addition to the generic Web API.

First, set the authentication-related information required to use the LINE Messaging API in the environment variables.

$ export LINE_CHANNEL_SECRET="Your channel secret"
$ export LINE_ACCESS_TOKEN="Your access token"

If you have the API key of docomo chat API, you can make it a chat BOT by setting as follows. If this step is omitted, it will operate as a return BOT in the same way as the operation check of Minette.

$ export CHAT_API_KEY="Your API key"
$ export DEFAULT_DIALOG_SERVICE="minette.dialog.chat_dialog.ChatDialogService"

Now that you're ready, let's start the endpoint. Add the -l option.

$ minette -l

Let's check the operation.

If it works like this, it's a success. Thank you for your support.

Recommended Posts

Create a LINE BOT with Minette for Python
[Python] [LINE Bot] Create a parrot return LINE Bot
Create a Twitter BOT with the GoogleAppEngine SDK for Python
[LINE Messaging API] Create a BOT that connects with someone with Python
Steps to create a Twitter bot with python
Create a directory with python
I made a LINE BOT with Python and Heroku
[For play] Let's make Yubaba a LINE Bot (Python)
Create a Layer for AWS Lambda Python with Docker
[Super easy] Let's make a LINE BOT with Python.
[LINE Messaging API] Create parrot return BOT with Python
Create a virtual environment with Python!
Create a LINE Bot in Django
Create a machine learning app with ABEJA Platform + LINE Bot
Create a Mastodon bot with a function to automatically reply with Python
Create a child account for connect with Stripe in Python
[Python] Create a screen for HTTP status code 403/404/500 with Django
Introducing the BOT framework Minette for Python
Create a Python function decorator with Class
Build a blockchain with Python ① Create a class
Let's create a virtual environment for Python
[Python] Create a virtual environment with Anaconda
Let's create a free group with Python
Create a word frequency counter with Python 3.4
Let's make a Twitter Bot with Python!
Create a Python execution environment for Windows with VScode + Remote WSL
Create a striped illusion with gamma correction for Python3 and openCV3
Create a color picker for the color wheel with Python + Qt (PySide)
Create a USB boot Ubuntu with a Python environment for data analysis
I made a stamp substitute bot with line
Create a frame with transparent background with tkinter [Python]
Send a message to LINE with Python (LINE Notify)
The story of making a university 100 yen breakfast LINE bot with Python
Make a Twitter trend bot with heroku + Python
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Create a Python module
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 1
Procedure for creating a LineBot made with Python
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a python3 build environment with Sublime Text3
LINE BOT with Python + AWS Lambda + API Gateway
Create a dashboard for Network devices with Django!
Create a color bar with Python + Qt (PySide)
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part 2
I made a LINE Bot with Serverless Framework!
Create an automatic grade management app for Tenhou private room with LINE bot and Python Part ③
Commands for creating a python3 environment with virtualenv
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)
Create a Photoshop format file (.psd) with python
Create a Python environment
Make a LINE WORKS bot with Amazon Lex
Create a Python console application easily with Click
Read line by line from a file with Python
I made a Mattermost bot with Python (+ Flask)
Create a slack bot
[AWS] I made a reminder BOT with LINE WORKS