Speaking of BOT in Japan, it is LINE BOT. Here, we will introduce how to create a BOT application framework using Minette for Python.
--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)
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
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
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.
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_url
is the URL for accessing the Raspberry Pi issued by ngrok, this Make a note of it by copying it.
Set / api
of the URL (or the URL of the execution environment itself) issued from ngrok earlier.
It is like this. Note that the service has not been started yet, so even if you press Verify, it will fail.
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