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.
https://github.com/nnsnodnb/morphological-linebot
https://github.com/line/line-bot-sdk-python
BOT
I wish I could get on the SDK's Wiki page ...
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
Sure enough, you have to use HTTPS, so let's do our best!
Do your best to reach the LINE @ management screen and use ** "Account Settings" **> ** "Bot Settings" ** to make the settings you want!
It's very hard to see ...
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