It seems that the Messaging API has been newly released from LINE, so I made a BOT that just analyzes morphological elements as usual.

Introduction

I made the exact same thing with my Trial account before, but I got a threatening email saying "I'm going to delete all my accounts !!", so I moved.

Repository

What i made

https://github.com/nnsnodnb/morphological-linebot

LINE Official SDK

https://github.com/line/line-bot-sdk-python

BOT

9FlxYyqoOH.png

I wish I could get on the SDK's Wiki page ...

environment

Source code

bot.py


# coding: utf-8

from __future__ import unicode_literals

from flask import Flask, request, abort

from linebot import LineBotApi, WebhookHandler
from linebot.exceptions import InvalidSignatureError
from linebot.models import MessageEvent, TextMessage, TextSendMessage

from janome.tokenizer import Tokenizer

import os, re, json

def load_env():
    try:
        with open('.env') as f:
            content = f.read()
    except IOError:
        content = ''

    for line in content.splitlines():
        m1 = re.match(r'\A([A-Za-z_0-9]+)=(.*)\Z', line)
        if m1:
            key, val = m1.group(1), m1.group(2)
            m2 = re.match(r"\A'(.*)'\Z", val)
            if m2:
                val = m2.group(1)
            m3 = re.match(r'\A"(.*)"\Z', val)
            if m3:
                val = re.sub(r'\\(.)', r'\1', m3.group(1))
            os.environ.setdefault(key, val)

load_env()

app = Flask(__name__)

line_bot_api = LineBotApi(os.environ.get('CHANNEL_ACCESS_TOKEN'))
handler = WebhookHandler(os.environ.get('CHANNEL_SECRET'))

@app.route("/callback", methods=['POST'])
def callback():
    signature = request.headers['X-LINE-Signature']

    body = request.get_data(as_text=True)
    receive_json = json.loads(body)
    message = receive_json['events'][0]['message']['text']

    response_arrays = []
    t = Tokenizer()
    for token in t.tokenize(message):
        response_arrays.append(str(token))

    response = ''
    for item in range(len(response_arrays)):
        if len(response_arrays) == item + 1:
            response += str(response_arrays[item])
        else:
            response += str(response_arrays[item] + '\n')

    try:
        line_bot_api.reply_message(receive_json['events'][0]['replyToken'], TextSendMessage(text = response))
    except InvalidSignatureError:
        abort(400)

    return 'OK'

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

.env


CHANNEL_ACCESS_TOKEN='hogehoge' #Very long guy
CHANNEL_SECRET=''					#The one who is hiding as before

Settings on the LINE management screen side

Sure enough, you have to use HTTPS, so let's do our best!

スクリーンショット 2016-10-21 午前0.45.56.png

Do your best to reach the LINE @ management screen and use ** "Account Settings" **> ** "Bot Settings" ** to make the settings you want!

Actual response

FullSizeRender.jpg

It's very hard to see ...

Finally

If there is anything, I want you to submit a pull request etc.! Also, please give me a LINE Wiki page.

I was impatient at first because there is no request.headers ['X-LINE-Signature'] in the old API and this new API. By the way, in the old API, it corresponds to request.headers ['X-Line-Channelsignature'].

Recommended Posts

It seems that the Messaging API has been newly released from LINE, so I made a BOT that just analyzes morphological elements as usual.
I made a Line bot that guesses the gender and age of a person from an image
[LINE Messaging API] I want to send a message from the program to everyone's LINE
In Python, I made a LINE Bot that sends pollen information from location information.
A script that makes it easy to create rich menus with the LINE Messaging API
I made a Chatbot using LINE Messaging API and Python
[Python] I made a Line bot that randomly asks English words.
[LINE Messaging API] Create a BOT that connects with someone with Python
I made a slack bot that notifies me of the temperature
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I started to work at different times, so I made a bot that tells me the time to leave
[Python] I made a bot that tells me the current temperature when I enter a place name on LINE
I made a Discord bot in Python that translates when it reacts
I made a simple timer that can be started from the terminal