Last time, I created an account for Line Bot. From this time, we will build a system for Line Bot in earnest.
I often see commentary articles written in PHP for building a Line Bot system, but this time I will build a system using Python. The reason is that we plan to use "natural language processing" such as analyzing the sentences exchanged by Line Bot and giving the best answer to it, and Python has abundant libraries of such natural language processing systems. is.
Then, we will create an application for bots using "Django", which is the most typical Python web framework. If you've created Django or other web applications, you can proceed as it is, but if you're a beginner or want to know how to use Django, you can read the commentary I wrote earlier. It's easy, so please take a look.
We will proceed on the premise of a Mac environment.
Check the Python version as follows.
Check the python version of your system
$ python3 --version
Python 3.5.2
Install django
$ pip3 install django==1.10.3
Django version check
$ python3
Python 3.5.2 (default, Jun 29 2016, 13:43:58)
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()
'1.10.3'
First, create a Python VirtualEnvironment for the API, which is the biggest way to proceed with a Python project.
$ python3 -m venv api
Then create a project in this. This is also named ʻapi`.
Creating a project
$ cd api
$ django-admin startproject api .
Isn't this a virtual environment?
api
A directory named is created, and the directory structure is as follows.
Check directory structure (bin etc. omitted)
$ tree -L 2 api
api
├── api
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
├── bin
├── include
├── lib
├── manage.py
└── pyvenv.cfg
In this ʻapi project, we will create an application called
linebot. First, install the python web framework
Django. Keep the Django version as the current latest version,
1.10.3`.
Then, we will create a linebot application in this ʻapi` project.
Creating an application
$ cd api
$ django-admin startapp linebot .
$ tree -L 1 .
api
├── api
├── bin
├── include
├── lib
├── linebot
├── manage.py
└── pyvenv.cfg
After creating the application, various settings are required, but here we will introduce only the minimum required ones.
api/settings.py (part)
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'linebot', #add to
)
#Change time zone
LANGUAGE_CODE = 'ja'
TIME_ZONE = 'Asia/Tokyo'
USE_TZ = False #Changed to False by Default because of UTC timezone
Set the DB. This time, we will use SQLite without any changes from the initial settings.
DB settings
$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
Applying contenttypes.0001_initial... OK
Applying auth.0001_initial... OK
Applying admin.0001_initial... OK
Applying admin.0002_logentry_remove_auto_add... OK
Applying contenttypes.0002_remove_content_type_name... OK
Applying auth.0002_alter_permission_name_max_length... OK
Applying auth.0003_alter_user_email_max_length... OK
Applying auth.0004_alter_user_username_opts... OK
Applying auth.0005_alter_user_last_login_null... OK
Applying auth.0006_require_contenttypes_0002... OK
Applying auth.0007_alter_validators_add_error_messages... OK
Applying auth.0008_alter_user_username_max_length... OK
Applying sessions.0001_initial... OK
Now, let's start the server locally and check if the default web page is displayed.
Start web server
$ python3 manage.py runserver
Performing system checks...
System check identified no issues (0 silenced).
November 12, 2016 - 13:04:05
Django version 1.10.3, using settings 'linebot.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
Once the web server is up, you can access http://127.0.0.1:8000/ from your browser.
Now you have a local development environment.
The Line bot is not local and can only be run by placing this web application on a remote server. So, next time, I will explain how to synchronize this application to the server.
We are waiting for you to follow us!
Service introduction Please feel free to contact us if you are interested in "Kikagaku", a one-on-one tutoring service for machine learning that allows you to learn "math → programming → web applications" all at once.
Recommended Posts