<file_sep># 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)
<file_sep>click==6.6
Flask==0.11.1
future==0.15.2
gunicorn==19.6.0
itsdangerous==0.24
Janome==0.2.8
Jinja2==2.8
line-bot-sdk==1.0.2
MarkupSafe==0.23
requests==2.11.1
Werkzeug==0.11.11
<file_sep># morphological-linebot

## Installation

```bash
$ git clone https://github.com/nnsnodnb/morphological-linebot.git
$ virtualenv venv
$ source venv/bin/activate
$ pip install requirements.txt
$ mv .env.sample .env
$ vim .env
```

Fill your BOT CHANNEL_ACCESS_TOKEN and CHANNEL_SECRET.

```
CHANNEL_ACCESS_TOKEN=''
CHANNEL_SECRET=''
```

Change to suit your environment

```python
app.run(host='0.0.0.0', port = 5000, threaded = True, debug = True)
# or
app.run(host='0.0.0.0', port = 5000, threaded = True, debug = False)
# and so on.
```

```bash
$ python bot.py
# or
$ gunicorn bot:app --bind 0.0.0.0:5000 -D
# and so on.
```

## LICENSE

MIT License. See [License](LICENSE)
