Recently I wanted to make a spear linebot. Also, I really wanted to make it with python!
Upload server developed with flask in anaconda environment to heroku I tried to make LINE BOT with Python using LINE BOT API
The following three are made with ruby in detail by my friends, so please take a look. LINE BOT gave me a leaflet (1) ~ Echolalia LINE BOT gave me a leaflet (2) ~ PUSH edition LINE BOT gave me a leaflet (3) -until the end
I want to make it to the point where I can return it!
conda-requirements.txt
# This file may be used to create an environment using:
# $ conda create --name <env> --file <this file>
# platform: osx-64
flask=0.10.1=py35_1
gunicorn=19.1.0=py35_0
itsdangerous=0.24=py35_0
jinja2=2.8=py35_0
markupsafe=0.23=py35_0
openssl=1.0.2h=0
pastedeploy=1.5.2=py35_1
pip=8.1.1=py35_1
python=3.5.1=0
readline=6.2=2
setuptools=20.7.0=py35_0
sqlite=3.9.2=0
tk=8.5.18=0
werkzeug=0.11.9=py35_0
wheel=0.29.0=py35_0
xz=5.0.5=1
zlib=1.2.8=0
runtime.txt
python-3.5.1
from flask import Flask
from flask import request
import os
import requests
import json
import re
LINEBOT_API_EVENT ='https://trialbot-api.line.me/v1/events'
LINE_HEADERS = {
'Content-type': 'application/json; charset=UTF-8',
'X-Line-ChannelID':os.environ['CONTENT'], # Channel ID
'X-Line-ChannelSecret':os.environ['CHANNEL'], # Channel secre
'X-Line-Trusted-User-With-ACL':os.environ['MID'] # MID (of Channel)
}
#proxies = {'https': os.environ['PROXIES']}
proxies = {'http': os.environ.get('FIXIE_URL', ''), 'https': os.environ.get('FIXIE_URL', '')}
def post_event( to, content):
msg = {
'to': [to],
'toChannel': 1383378250, # Fixed value
'eventType': "138311608800106203", # Fixed value
'content': content
}
print(msg)
r = requests.post(LINEBOT_API_EVENT, headers=LINE_HEADERS, data=json.dumps(msg),proxies = proxies)
def post_text( to, text ):
content = {
'contentType':1,
'toType':1,
'text':text,
}
post_event(to, content)
app = Flask(__name__)
@app.route("/callback", methods=['POST'])
def hello():
msgs = request.json['result']
for msg in msgs:
text = msg['content']['text']
print(text)
post_text(msg['content']['from'],text)
return ''
if __name__ == "__main__":
app.run()
Before I gave this article, I went to the code of the next step, so there may be extra things when I return the code, but I'm sorry. channelID etc. are stored as environment variables of heroku.
Recommended Posts