[PYTHON] I tried linebot with flask (anaconda) + heroku

Introduction

Recently I wanted to make a spear linebot. Also, I really wanted to make it with python!

reference

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

This goal

I want to make it to the point where I can return it!

Preferences file

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

Source code

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()

Finally

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

I tried linebot with flask (anaconda) + heroku
I tried Flask with Remote-Containers of VS Code
I tried fp-growth with python
I tried scraping with Python
Touch Flask + run with Heroku
I tried Learning-to-Rank with Elasticsearch!
I tried clustering with PyCaret
I tried gRPC with Python
I tried scraping with python
Work memo that I tried i18n with Flask app
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
Make Echolalia LINEbot with Python + heroku
I tried trimming efficiently with OpenCV
I tried summarizing sentences with summpy
I tried machine learning with liblinear
I tried web scraping with python.
Run the app with Flask + Heroku
I tried moving food with SinGAN
I can't install mysql-connector-python with anaconda
I tried implementing DeepPose with PyTorch
I tried face detection with MTCNN
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sentence generation with GPT-2
I tried learning LightGBM with Yellowbrick
I tried face recognition with OpenCV
I made a Nyanko tweet form with Python, Flask and Heroku
The diagrams were interesting so I tried wrapping them with flask
I tried to create a linebot (implementation)
I tried multiple regression analysis with polynomial regression
I tried sending an SMS with Twilio
I tried using Amazon SQS with django-celery
I tried to implement Autoencoder with TensorFlow
I tried to create a linebot (preparation)
I tried to visualize AutoEncoder with TensorFlow
Vue.js + Flask environment construction memorandum ~ with Anaconda3 ~
I tried to get started with Hy
I tried scraping Yahoo News with Python
I tried using Selenium with Headless chrome
I tried factor analysis with Titanic data!
I tried learning with Kaggle's Titanic (kaggle②)
I tried sending an email with python.
I tried non-photorealistic rendering with Python + opencv
I tried a functional language with Python
I tried batch normalization with PyTorch (+ note)
I tried recursion with Python ② (Fibonacci sequence)
I tried implementing DeepPose with PyTorch PartⅡ
Easy web app with Python + Flask + Heroku
I tried to implement CVAE with PyTorch
I tried playing with the image with Pillow
Launch environment with LineBot + Heroku + Docker + Python
I tried to solve TSP with QAOA
I tried simple image recognition with Jupyter
Getting Started with Heroku, Deploying Flask App
I tried CNN fine tuning with Resnet
Launch Flask application with Docker on Heroku
I tried natural language processing with transformers.
#I tried something like Vlookup with Python # 2
I tried handwriting recognition of runes with scikit-learn
I tried to predict next year with AI
How to upload with Heroku, Flask, Python, Git (4)