I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~

Introduction

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.

Source code

The source code is available on github. https://github.com/Sciseed/yukko-line-bot

Development environment

・ MAC OS X 10.10.3 -Python 3.4.0 · Django 1.8.14

Things necessary

.heroku account ・ Heroku toolbelt ・ Virtualenv

Create an empty app with django

Create directory for app

$ mkdir bottest $ cd bottest

Virtual environment construction

$ virtualenv virenv $ source virenv/bin/activate

Install django-toolbelt

$ pip install django-toolbelt

Create a Django project

$ django-admin.py startproject bottest

Create a Django app

$ cd bottest $ python manage.py startapp bot

gunicorn settings

Since gunicorn is included in django-toolbelt, use it ``

Creating a Procfile

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 -

python version specification

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)

Try starting a local server

$ 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.

Deploy to heroku

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

Test the webhook URL

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) image 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.

Test from LINE client

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.

I was addicted to

・ 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.

reference

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

I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I made a Chatbot using LINE Messaging API and Python
I made a LINE BOT with Python and Heroku
I made a Line-bot using Python!
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
I tried using Twitter api and Line api
I made a poker game server chat-holdem using websocket with python
[Python] I wrote a REST API using AWS API Gateway and Lambda.
I made a Python Qiita API wrapper "qiipy"
I made a LINE BOT that returns a terrorist image using the Flickr API
I made a login / logout process using Python Bottle.
[LINE Messaging API] Create a rich menu in Python
[Python] I made a LINE Bot that detects faces and performs mosaic processing.
[Python] I tried running a local server using flask
I made a quick feed reader using feedparser in Python
I made a daemon with Python
I made a server with Python socket and ssl and tried to access it from a browser
[Python] I made a Line bot that randomly asks English words.
I made a simple circuit with Python (AND, OR, NOR, etc.)
[Python3] I made a decorator that declares undefined functions and methods.
I made a game called Battle Ship using pygame and tkinter
I created a Python library to call the LINE WORKS API
I made a Nyanko tweet form with Python, Flask and Heroku
[LINE Messaging API] Create a BOT that connects with someone with Python
I made a Python wrapper library for docomo image recognition API.
I made a chatbot with Tensor2Tensor and this time it worked
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
I made a payroll program in Python!
I made a character counter with Python
I tried drawing a line using turtle
Beginner: I made a launcher using dictionary
I tried using UnityCloudBuild API from Python
I made a Hex map with Python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
Start a web server using Bottle and Flask (I also tried using Apache)
I tried to create a sample to access Salesforce using Python and Bottle
I want to make a web application using React and Python flask
I made a music bot using discord.py and Google Drive API (tested with Docker → deployed to Heroku)
I made a stamp substitute bot with line
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I tried object detection using Python and OpenCV
I made a Twitter fujoshi blocker with Python ①
[Python] I made a Youtube Downloader with Tkinter.
Create a web map using Python and GDAL
I made a LINE Bot with Serverless Framework!
I tried reading a CSV file using Python
Launch a web server with Python and Flask
Prepare a pseudo API server using GitHub Actions
Try using ChatWork API and Qiita API in Python
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I made a Line Bot that uses Python to retrieve unread Gmail emails!
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!