Explaining how to make LINE BOT in the world's easiest way (2) [Preparing a bot application in a local environment with Django in Python]

Introduction

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.

No. title
1 Easy-to-understand explanation of Python Web application (Django) even for beginners (1) [Environment construction]
2 Easy-to-understand explanation of Python Web application (Django) even for beginners (2) [Project creation]
3 Easy-to-understand explanation of Python Web application (Django) even for beginners (3) [Application creation / DB setting]
4 Easy-to-understand explanation of Python web application (Django) even for beginners (4) [Routing settings / Introduction to MTV design patterns]
5 Easy-to-understand explanation of Python Web application (Django) even for beginners (5) [Introduction to DB operation with Django shell]
6 Easy-to-understand explanation of Python web application (Django) even for beginners (6) [MTV design pattern completion]

Development environment

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'

Create a project for Line Bot API

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? apiA 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

Creating a Django application

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

Settings after creating a Django application

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

Database (SQLite) settings

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

Start the server

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.

スクリーンショット 2016-11-12 13.05.49.png

Summary

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.

Next commentary article

The world's most easy-to-understand explanation of how to make LINE BOT (3) [Linkage with server with Git]

bonus

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

Explaining how to make LINE BOT in the world's easiest way (2) [Preparing a bot application in a local environment with Django in Python]
The world's most easy-to-understand explanation of how to make a LINE BOT (1) [Account preparation]
Probably the easiest way to create a pdf with Python3
The world's most easy-to-understand explanation of how to make LINE BOT (3) [Linkage with server with Git]
How to install python package in local environment as a general user
The easiest way to get started with Django
The easiest way to synthesize speech with python
The easiest way to use OpenCV with python
Create a Django project and application in a Python virtual environment and start the server
How to get a list of files in the same directory with python
[Python] How to create a local web server environment with SimpleHTTPServer and CGIHTTPServer
How to convert / restore a string with [] in python
[Python] How to draw a line graph with Matplotlib
Explain in detail how to make sounds with python
How to build a Django (python) environment on docker
The easiest line bot in the world to lose weight
[Super easy] Let's make a LINE BOT with Python.
How to identify the element with the smallest number of characters in a Python list?
[Learning memo] How to make an application with Django ~ From virtual environment to pushing to github ~
How to make a Cisco Webex Teams BOT with Flask
Try running python in a Django environment created with pipenv
How to install NPI + send a message to line with python
How to convert an array to a dictionary with Python [Application]
How to use the __call__ method in a Python class
How to develop in a virtual environment of Python [Memo]
How to generate a query using the IN operator in Django
Until I return something with a line bot in Django!
How to get the last (last) value in a list in Python
How to build a python2.7 series development environment with Vagrant
How to get into the python development environment with Vagrant
How to make an interactive LINE BOT 004 (answer the closing date of a listed company)
If you know Python, you can make a web application with Django
[Python] Create a program to delete line breaks in the clipboard + Register as a shortcut with windows
How to make a string into an array or an array into a string in Python
[Introduction to Python] How to split a character string with the split function
How to check the memory size of a variable in Python
How to make a slack bot
Learning history to participate in team application development with Python ~ Build Docker / Django / Nginx / MariaDB environment ~
How to make a command to read the configuration file with pyramid
How to make a surveillance camera (Security Camera) with Opencv and Python
How to get a string from a command line argument in python
How to create a heatmap with an arbitrary domain in Python
[Introduction to Python] How to use the in operator in a for statement?
How to check the memory size of a dictionary in Python
I tried to make a 2channel post notification application with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
I tried to make a todo application using bottle with python
How to send a request to the DMM (FANZA) API with python
The easiest way to make Flask
Create a LINE Bot in Django
How to display bytes in the same way in Java and Python
How to debug a Python program by remotely connecting to a Docker container in WSL2 environment with VS Code
[Python] How to save the installed package and install it in a new environment at once Mac environment
Tweet in Chama Slack Bot ~ How to make a Slack Bot using AWS Lambda ~
The story of making a university 100 yen breakfast LINE bot with Python
How to make an artificial intelligence LINE bot with Flask + LINE Messaging API
[Introduction to Udemy Python3 + Application] 47. Process the dictionary with a for statement
[Python] Explains how to use the range function with a concrete example
How to get the date and time difference in seconds with python
[Introduction to Python] How to sort the contents of a list efficiently with list sort
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda