Hanashi who became a read through BOT when trying to make a morphological analysis BOT using the popular LINE BOT API

background

Recently, the LINE BOT API is popular in the streets (is it late to notice?) According to official announcements and press releases

What is "BOT API Trial Account"? With BOT API Trial Account, you can try API development that enables two-way communication between your service and LINE users. Introduction of BOT API Trial Account | LINE Business Center

Open to the first 10,000 third-party developers ahead of the opening of a messaging API that allows corporate accounts to interact with users in a conversational manner.

You can create a bot account that sends and receives messages via API by linking the system or service developed by the user with the LINE account. Expected to be used in various applications and fields such as restaurant search, schedule notification and coupon issuance linked with the schedule app, and operation from LINE by linking with hardware such as home appliances. ITmedia LINE BOT API News

Possibility of one to one marketing? スクリーンショット 2016-08-04 1.52.29.png

http://lbc.line.me/ Than

There was a person making something interesting and I wanted to imitate it. ・ The story of making a BOT with AWS Lambda that analyzes images using Google Cloud Vision because the LINE API has been released

Story of implementing BOT that teaches translation of English words with LINE BOT API

Search for today's lunch with LINE BOT API It seems that it is connected with google API

What i did

・ Register a BOT API Trial Account ・ Registration of Callback URL -Server IP Whitelist settings (register the IP addresses allowed when sending messages) ・ Morphological analysis code creation

Generally speaking stumbling point -The callback URL must be encrypted. And it is necessary to specify port 443 (SSL port number) (https: //○○○○.ssl-xserver.jp:443/linebot/callback.py) -The set callback is reflected slowly. -The reflection of Server IP Whitelist is also slow.

Registration flow

Registered from LINE Business Center, it was said that the first 10,000 people, but probably not スクリーンショット 2016-08-04 1.37.10.png

Just enter it normally.

Once registration is complete ・ Channel ID ・ Channel Secret ・ MID You can get 3 keys. This is the code that will be set in the request header when making an API request.

スクリーンショット 2016-08-04 1.37.52.png

server

Some people are AWS, some are rental servers, and some are Heroku (PaaS?). This time, I used the rental server Xserver. スクリーンショット 2016-08-04 1.58.37.png Click SSL Settings

スクリーンショット 2016-08-04 1.58.50.png

Just set up common SSL. Describe the set URL (https: //○○○○.ssl-xserver.jp) as a callback URL on the dashboard of LINE developers.

Morphological analysis

The language is Python. I referred to this site.

callback.py


from flask import Flask
from flask import request

from janome.tokenizer import Tokenizer

import requests
import json
import re

import settings

LINEBOT_API_EVENT ='https://trialbot-api.line.me/v1/events'
LINE_HEADERS = {
    'Content-type': 'application/json; charset=UTF-8',
    'X-Line-ChannelID':settings.CHANNEL_ID,
    'X-Line-ChannelSecret':settings.CHANNEL_SECRET,
    'X-Line-Trusted-User-With-ACL':settings.MID
}

def post_event(to, content):
    msg = {
        'to': [to],
        'toChannel': 1383378250,
        'eventType': "138311608800106203",
        'content': content
    }
    r = requests.post(LINEBOT_API_EVENT, headers = LINE_HEADERS, data = json.dumps(msg))

def post_text(to, text):
    content = {
        'contentType':1,
        'toType':1,
        'text':text,
    }
    post_event(to, content)


commands = (
    (re.compile('author', 0), lambda x: 'https://nnsnodnb.moe'),
)

app = Flask(__name__)

@app.route("/callback", methods=['POST'])
def callback():
    messages = request.json['result']

    for message in messages:
        text = message['content']['text']
        for matcher, action in commands:
            if matcher.search(text):
                response = action(text)
                break
        else:
            post_text(message['content']['from'], 'Under analysis...')
            #Morphological analysis
            response = ''
            t = Tokenizer()
            for token in t.tokenize(message['content']['text']):
                response += str(token) + '\n'
        post_text(message['content']['from'], response)
    return ''

if __name__ == "__main__":
    app.run(host = '0.0.0.0', port = 8001, threaded = True, debug = True)

Upload this on the server with FTP software!

Should be! !! !!

Not morphologically analyzed Read through IMG_0466.JPG

I don't know if it's a code mistake or waiting for the callback URL to be reflected, but I'll wait a little longer

If you want to see it using the read-through BOT, please make friends (may be deleted without notice) スクリーンショット 2016-08-04 2.08.42.png

At the end

Depending on how I did it, I felt that it was easy for individuals to create BOTs with convenient functions and BOTs that shine in the business scene (although I was not allowed to do morphological analysis ...). It seems that chatbots are also popular in the neighborhood.

I also feel that there was a different approach if I had knowledge about the server

It's been a while since the read-through was so painful.

There is a person who writes the same thing, and a mysterious feeling of familiarity http://naoyashiga.hatenablog.com/entry/2016/04/12/123312

Referenced site

I made a LINE bot with Python + Flask http://nnsnodnb.hatenablog.jp/entry/line-bot-made-flask

Introducing 21 examples of LineBot http://blog.minato.jp.net/entry/linebot

How to use LINE API (BOT API) that can be used by individuals (PHP). The trial version can be used by the first 10,000 people! https://www.panzee.biz/archives/9115 LINE developers BOT API official https://developers.line.me/bot-api/overview

Recommended Posts

Hanashi who became a read through BOT when trying to make a morphological analysis BOT using the popular LINE BOT API
Make a morphological analysis bot loosely with LINE + Flask
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
I made a LINE BOT that returns a terrorist image using the Flickr API
Send a push message to the LINE Bot when the LTE-M Button is pressed [SORACOM]
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
Create a real-time auto-reply bot using the Twitter Streaming API
Let's make a LINE bot using various services [ngrok edition]
[Python] I tried to make a simple program that works on the command line using argparse.
Make a LINE BOT (chat)