[Line / Python] Beacon implementation memo

Python environment construction

Install version 3.6.8 from pkg [How to install from pkg file](https://qiita.com/ms-rock/items/72b8f1abc661c539bb09#21-pkg%E3%83%95%E3%82%A1%E3%82%A4%E3%83 % AB% E3% 81% 8B% E3% 82% 89% E3% 81% AE% E3% 82% A4% E3% 83% B3% E3% 82% B9% E3% 83% 88% E3% 83% BC % E3% 83% AB% E6% 96% B9% E6% B3% 95)

# Python3.6.Check the installation location of 8
$ which python

#Install python with brew to install pip3
$ brew install python

#virtualenv installation
$ pip3 install virtualenv

#Create virtual environment
$ virtualenv --python=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 env

#Version confirmation
$ python
Python 3.6.8 (v3.6.8:3c6b436a57, Dec 24 2018, 02:04:31)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

How to get inside the virtual environment source bin/activate

Python3 installation (Mac version)

Line account registration

Preparation

--Create a Line Developer account --Creating a channel --Bot creation

Follow the steps below to "Create a LINE bot with Flask". [Until you implement LINE bot in Flask and deploy to heroku](https://qiita.com/suigin/items/0deb9451f45e351acf92#%E9%96%8B%E7%99%BA%E7%92%B0%E5 % A2% 83)

Line Bot environment construction

Run from the beginning of the virtual environment.

$ source bin/activate
$ pip install flask 
$ pip install line-bot-sdk
$ brew cask install ngrok

SDK to use: line-bot-sdk-python What is homebrew-cask

Use of environment variables

pip3 install python-dotenv

Best practices for configuration files in Flask How to write environment variables that you don't want to put on [GitHub] Python

test

Test if Flask is working

Add the following to ʻapp.py, access the root path, and confirm that hello world! `is displayed.

@app.route("/")
def hello_world():
    return "hello world!"

flask execution command flask run --host=0.0.0.0

ngrok If you want to test Line, you need to enter the test URL in the webhook. At this time, the URL of https is required, and ngrok is convenient at that time.

What ngrok is doing

You can publish your local development on https. Please check below for details. ngrok is too convenient

Actions

In a separate terminal, do the following flask run --host=0.0.0.0 ngrok http 5000

Heroku

Login

$ brew install heroku/brew/heroku

#The browser will be displayed, so log in there
$ heroku login

Deploy

Required files

Create the following three new

requirements.txt Use feeze to export the version you are currently in the virtual environment to requirements.txt. Check the list of installed packages with Python, pip list / freeze

runtime.txt Describe the Python version.

Procfile

web: gunicorn app:app --log-file -

About WSGI Differences between web server and application server in Rails development (translation)

Nginx passes the request to Unicorn. Unicorn passes the request to Rack. Rack passes the request to the Rails router. The router passes the request to the appropriate controller. Then the responses are returned in reverse order.

Application creation

heroku create [app name]

Push code

git push heroku master

Config settings

#List of environment variables
$ heroku config

#Add environment variable
$ heroku config:set ENV_NAME=value

[Heroku command] environment variables

Ssh in Heroku environment

heroku run bash Heroku Command Collection

Beacon implementation

By implementing the following, you can define the action when Beacon Event occurs. If you want to change the event, change the argument (BeaconEvent) of@ handler.add (BeaconEvent). So, the program below should describe how to handle ** after the event. ** ** The event occurrence condition is implemented on the Line side.

#Beacon event fired
@handler.add(BeaconEvent)
def handle_beacon(event):
    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text='Hello World'))

Decorator

The above implementation uses a decorator.

By using a decorator, you can add a process that is automatically executed when you call a function provided as a library. Quoted from About Python Decorators

Processing order

handler = WebhookHandler(channel_secret)

#Channel the WebhookHandler class_Initialize with secret
handler.handle(body, signature)

##behavior of handle method
#Execution of handle method of WebhookHandler class
#Execute parse method of WebhookParser class
#Since the body contains events, append to events and return events
#Turn events with a for statement

##decorator behavior
#add method is called
#Returns a decorator
#At this time,app.handle implemented in py_message method is called
#In the decorator method__add_The handler method is called_There is a value in handlers

#The add method of the WebhookHandler class is decorated and the decorator is called
# __add_Called handler

##I don't know yet
#I don't know where the add method is running
# handle_I can execute it without calling the message method, so I do not know where it is executed in the handle method

Around DB

SQLAlchemy settings

The following will be helpful. Procedure to start Flask + SQLAlchemy project

Flask migration

It will automatically generate a migration file from the model file. In my own words about Flask-Migrate

When adding migration (when changing existing schema)

Run locally

#Sets the database revision to the revision specified as an argument without performing the migration.
$ flask db stamp head
$ flask db migrate
$ flask db upgrade

Run to Heroku

heroku run FLASK_APP=app.py flask db stamp head
heroku run FLASK_APP=app.py flask db migrate
heroku run FLASK_APP=app.py flask db migrate

Flask-Migrate

Recommended Posts

[Line / Python] Beacon implementation memo
Python memo
Python memo
python memo
Python memo
Python memo
Python memo
[Python] Memo dictionary
python beginner memo (9.2-10)
python beginner memo (9.1)
★ Memo ★ Python Iroha
LINE heroku python
[Python] EDA memo
Python 3 operator memo
[My memo] python
Python3 metaclass memo
[Python] Basemap memo
Python beginner memo (2)
[Python] Numpy memo
Python class (Python learning memo ⑦)
My python environment memo
python openCV installation (memo)
Python module (Python learning memo ④)
RNN implementation in python
Visualization memo by Python
ValueObject implementation in Python
Python test package memo
[Python] Memo about functions
python regular expression memo
Binary search (python2.7) memo
[My memo] python -v / python -V
Python3 List / dictionary memo
[Memo] Python3 list sort
Python Tips (my memo)
[Python] Memo about errors
DynamoDB Script Memo (Python)
Python basic memo --Part 2
python recipe book Memo
Basic Python command memo
Python OpenCV tutorial memo
Python basic grammar memo
TensorFlow API memo (Python)
python useful memo links
Python decorator operation memo
Python basic memo --Part 1
Effective Python Memo Item 3
SVM implementation in python
Divisor enumeration Python memo
7-line interpreter implementation summary
Python memo (for myself): Array
Python exception handling (Python learning memo ⑥)
Display character strings without line breaks in python (personal memo)
Python execution time measurement memo
Fizzbuzz in Python (in one line)
Twitter graphing memo with Python
python> print> N line breaks> print ('\ n'* 3)> 4 line breaks> print ('\ n'* 3),> 3 line breaks
Python and ruby slice memo
Python Basic Grammar Memo (Part 1)
Python code memo for yourself
Try LINE Notify in Python
Raspberry Pi + Python + OpenGL memo