I participated in the summer internship of Sciseed inc. and developed LINE Chatbot. Since the official version of Messaging API was announced from LINE on September 29, 2016, this time I created a bot that automatically responds with the official version. This article [I made a Chatbot using LINE Messaging API and Python (1)] (http://qiita.com/Kosuke-Szk/items/eea6457616b6180c82d) It is a continuation of. This time, we will describe server construction and Webhook URL registration.
The source code is available on github. https://github.com/Sciseed/yukko-line-bot
・ MAC OS X 10.10.3 -Python 3.4.0 · Django 1.8.14
.heroku account ・ Heroku toolbelt ・ Virtualenv
$ mkdir bottest
$ cd bottest
$ virtualenv virenv
$ source virenv/bin/activate
$ pip install django-toolbelt
$ django-admin.py startproject bottest
$ cd bottest
$ python manage.py startapp bot
Since gunicorn is included in django-toolbelt, use it ``
Create a Procfile (without extension) at the top level of the app and describe the following contents
web: gunicorn bottest.wsgi bot:app --log-file -
Create a text file called runtime.txt in the same hierarchy as Procfile and write it as follows.
runtime.txt
python-3.4.0
This will recognize the python version when running the app on heroku (required work for 3.X series)
$ python manage.py runserver
In this specification, message data is only exchanged through an external API, so there is no need to hold data internally and no database is used. So you will be asked for migration, but it is not necessary.
http://127.0.0.1:8000/
To check the operation.
Do git management
$ git init
$ git add .
$ git commit -m "Initial commit"
Create repository on heroku
$ heroku create [app name]
Deploy to heroku
$ git push heroku master
Check if deployed
$ heroku open
Type the URL of your heroku application into the webhook URL to verify. (At the time of testing, the app itself should not do anything to the request) If it fails, you will be notified of the error status. When successful, nothing is displayed.
This time
heroku logs -t
By displaying the log with, you can check how the POST method of verify is processed. If 200 is returned in HTTP status, it is successful.
Let's talk to the bot from the LINE app. If successful, you should see a text message thrown along with the POST method from heroku's log.
・ POST request was rejected by csrf all the time In django, measures are taken to protect web applications from CSRF (Cross site request forgers) attacks. Roughly speaking, it is a function that does not accept POST requests other than the regular route. For this reason, for example, if you throw a POST to the app with curl, and of course you try to verify from LINE, you will get an error. Since this is a test, temporarily turn off csrf protection
settings.py
MIDDLEWARE_CLASSES = [
...
'django.middleware.csrf.CsrfViewMiddleware', #<=Comment out here
...
]
-When passing through the verify of the LINE API, I had implemented the implementation to add processing to the request from the beginning, so even if I threw the request from LINE, the error continued to return and I suffered. (The response according to the LINE response format specification could not be returned) Therefore, at the time of testing, it is better not to implement the request processing part, but to see only the receipt of the POST request and the simple response to it in the log. -I had a hard time because I made a mistake in the header format when I made a request to the LINE API. (It was not sent correctly in the dictionary format.) If an error is returned, please also check that the header and body are described in the correct format.
How to deploy a Django app on heroku in just 5 minutes Don't lose to Ruby! How to run Python (Django) on Heroku
Recommended Posts