Python beginners decided to make a LINE bot with Flask (Flask rough commentary)

Why did that happen?

Immediately after becoming independent as a freelancer, I decided to make a LINE bot at work. I started self-education because I wanted to implement it in Python, which is said to be good at artificial intelligence and natural language processing, and give it extensibility. I'm new to Python.

About this article

Before the detailed bot specifications were decided, I learned the following for self-education. https://www.udemy.com/course/python-flask-python-api-line-bot/

However, Python was only learned by dot installation and Progate, and I had no knowledge of the framework, so it became "Flask?". In this article, I thought I'd put together Flask for myself. I'm really a beginner, so please point out any differences. ..

Flask is a Python web framework

The point is that it is difficult to write Python by hand and create a web page, so it seems that you should easily proceed with development using a framework that can be shared.

Reference: https://dividable.net/python/flask-python/

It claims to be a "microweb application framework" and seems to have only the minimum necessary functions.

Rough comparison with Django, the same web framework ↓

Flask Django
weight Light Heavy
Customizability high Not suitable for detailed customization
Initial setup early Various preparations are required
Learning cost Low because there are few functions I can do various things, but it seems to be quite difficult
Suitable business Single-function Web application / business management screen Large-scale service for enterprises

Because it looks like this, it seems that Flask is more suitable when making a bot. I want you to get a reply quickly!

Rough usage of Flask

Please refer to Official for installation and environment construction.

After installation, just write the following and it will work as a Web service. It's that easy! The author has been trained to write Java, so I was impressed that it was dizzyingly easy.

How to make the familiar Hello World

python


from flask import Flask      #Import Flask class
app = Flask(__name__)        #Declare the name of your Flask application

@app.route('/')              #Set the URL to launch the function
def hello_world():           #Function definition
    return "Hello World!"    #This time in the browser"Hello World!"To display

if __name__ == '__main__':   #When started as an application
    app.run()                #Run the application on the local server

What is characteristic is ʻapp = Flask (__ name__)andname =='main'`. It took me a long time to understand here.

What is __name__?

First, let's talk about the module name of this application. If you use only one module, you have to use __name__. This is because the name is different when it is launched as an application and when it is imported as a module ('__ main__' when launched as an application, its import name when imported).

ʻIf name =='main': `guarantees that it will only run on that server when the script is run directly from the Pyrhon interpreter, not when it is imported as a module.

Officially, it is described as above. Hmm, I see?

__name__ is an automatically defined variable that contains the module name of the current file.

Suppose you've created three Flask applications (let's name them One, Two, Three) Consider the case where One and Two are imported by Three.

Suppose that each of the three has a description of ʻif name =='main': app.run () `.

For example, when running Three on the server, by replacing __name__ with the appropriate name, It seems that One and Two are designed not to run on the server. (The module name at this time is that Three is treated as __main__, One is treated as ʻOne, and Two is treated as Two`.)

RUN on local server

Now, let's execute the created hello.py.

$ python hello.py

Flask has a built-in local development server, so If you run the above from the command line, you can access it on localhost port 5000 (http://127.0.0.1:5000/).

Let's open a web browser and access the URL. It is OK if "Hello World" is displayed in the upper left corner. You are now ready to use Flask. You did it!

If you are using a virtual environment such as Docker, access the corresponding address and port. (I might write another article soon ...)

To stop the server, press control-C.

Hello World is done. next?

After I was able to do this, I tried making a bot for Echolalia, and then proceeded to development for work. Various people have written articles on how to make a parrot return bot, so if you look at that, I think that you can proceed without difficulty.

Next time, I'll start with a database-related article that was messed up during work development. Please take a look if you are interested. ..

reference

https://a2c.bitbucket.io/flask/installation.html#installation (official) https://www.udemy.com/course/python-flask-python-api-line-bot/ https://methane.github.io/flask-handson/start.html

Recommended Posts

Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Make a morphological analysis bot loosely with LINE + Flask
[Super easy] Let's make a LINE BOT with Python.
How to make a Cisco Webex Teams BOT with Flask
Let's make a Twitter Bot with Python!
Send a message to LINE with Python (LINE Notify)
Make a Twitter trend bot with heroku + Python
How to make an artificial intelligence LINE bot with Flask + LINE Messaging API
Create a LINE BOT with Minette for Python
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
Steps to create a Twitter bot with python
Try to make a dihedral group with Python
Make a LINE WORKS bot with Amazon Lex
I made a Mattermost bot with Python (+ Flask)
Machine learning beginners tried to make a horse racing prediction model with python
If you want to make a discord bot with python, let's use a framework
[Python] How to draw a line graph with Matplotlib
Try to make a command standby tool with python
I made a LINE BOT with Python and Heroku
[For play] Let's make Yubaba a LINE Bot (Python)
Make a LINE BOT (chat)
Make a fortune with Python
How to install NPI + send a message to line with python
Make a LINE bot with GoogleAppEngine / py. Simple naked version
Create a Mastodon bot with a function to automatically reply with Python
Experiment to make a self-catering PDF for Kindle with Python
I want to send a message from Python to LINE Bot
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]
Let's make a GUI with python.
How to make a slack bot
Make a recommender system with python
Let's make a graph with python! !!
[5th] I tried to make a certain authenticator-like tool with python
Rubyist tried to make a simple API with Python + bottle + MySQL
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
How to make a surveillance camera (Security Camera) with Opencv and Python
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[LINE Messaging API] Create a BOT that connects with someone with Python
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
I tried to make my own high school girl BOT with Rinna style with LINE BOT (Python & Heroku)
Let's make a shiritori game with Python
[Python] How to make a class iterable
Make a rare gacha simulator with Flask
Fractal to make and play with Python
Let's make a voice slowly with Python
[Python] [LINE Bot] Create a parrot return LINE Bot
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a web framework with Python! (2)
The story of making a university 100 yen breakfast LINE bot with Python
How to make a rock-paper-scissors bot that can be easily moved (commentary)
Try to make a capture software with as high accuracy as possible with python (2)
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
How to draw a vertical line on a heatmap drawn with Python seaborn