Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine

Introduction

I made a bot using LINE Messaging API in Python + Google App Engine environment. At times, I didn't know much about it, so I'd like to write it down here as a memorandum.

Precautions when building the environment

-** Searching for Google App Engine ** etc. will bring up various information, so I'd like to leave the details of installation and settings to that.

--This time, I used ** Standard environment ** of ** Python **. Python also has a ** Flexible environment ** environment, which is better than the standard environment, but it seems that there is no free tier.


--The Python used this time is * 2.7.9 *, but it is under version control by the Python 3 version ** Anaconda **. --Therefore, you couldn't use the Google Cloud SDK as it is with the command, so you had to create a py27 environment with Anaconda once, do something like activate py27, and then type in the command. --By the way, when you activate and use another version in Anaconda, ** (py27) ** is displayed at the command.


--Deployment is done by command using ** Google Cloud SDK **. You can find out more about this by reading the GAE documentation as well. --At the time of initial startup, link the account with gcloud auth login, do gcloud init and set according to the screen, then `gcloud app deploy [project yaml file]` Or do `gcloud app deploy` in the root directory of your project. -- gcloud app deploy --version 2 It is also possible to manage the version when deploying.

Precautions when deploying

--In the standard environment, the Python library cannot be used just by writing "I will use this!" In the file, and you have to deploy it together with the main file.


--pip install [module] -t [installation location] It is better to install by using etc. to resolve the dependencies. (Used when installing **-t ** = ** --target ** in a location different from the normal module location.) --By the way, if you enter pip install -help etc. in the command, ** help ** in ** install ** of ** pip ** will be displayed. If I knew this from the beginning, I wouldn't have had such a hard time ...


――In addition, simply deploying the library together is not enough, and various settings are required. According to the official DOC, you can make the ** lib ** directory containing the modules recognizable by deploying ** appengine_config.py ** together. If you want to recognize other directories, you can change * lib * to * [arbitrary name] * in the code below.

appengine_config.py


# [START vendor]
import os
from google.appengine.ext import vendor

# Add any libraries installed in the "lib" folder.
vendor.add(os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib'))
# [END vendor]

――I wasn't sure how it was needed, but it seems better to include ** requirements.txt ** as well. If you paste the result of pip freeze, it seems OK for the time being.

With the above settings, finally on the code side

code


import flask

You will be able to do something.

Precautions for using LINE Messaging API

-** LINE Messaging API ** has a library for python, and in fact this time, one of the purposes was to use it. line-bot-sdk-python on GitHub (Can be installed with pip)

--Also, note that ** line-bot-sdk-python ** requires a module called requests for communication with the outside, but ** GAE cannot use requests. **about it. (A Google module called URLFetch is required to get, post, etc. on GAE)

As a result of various investigations, there was a module that bridges URLFetch and requests, so I installed it and added / changed it to line-bot-sdk-python as follows.

/lib/linebot/api.py


#※/lib is the directory where you installed the module.

# [START imports]
import requests
import requests_toolbelt.adapters.appengine

# Use the App Engine Requests adapter. This makes sure that Requests uses
# URLFetch.
requests_toolbelt.adapters.appengine.monkeypatch()
# [END imports]

Now you can use it on GAE without major changes to line-bot-sdk-python. With this library, you don't have to create your own LINE reply object. Click here for detailed specifications of ** LINE Messaging API **: Line API Reference

A little explanation of line-bot-sdk-python

HTTPS reception code


    jsonObject = request.json
    line_request = jsonObject['events'][0]  #The content of the request

Here you can see the content of the message coming from LINE with line_request. Write this at the beginning of the code that processes the message.

BOT main code


    line_bot_api = LineBotApi(your_channel_access_token)
    parser = WebhookParser(your_channel_secret))

The rest is easy. (The message object here is the * Send message object * on the LINE side.)

BOT main code


        line_bot_api.reply_message(
            replyToken,
            TextSendMessage(text=event.message.text)
        )

--``` line_bot_api.reply_message (replyToken, message object) `` Reply with


How to create a message object

-- Object (text) = TextSendMessage (text = "text") `` ` -- Object (photo) = ImageSendMessage (original_content_url = "URL of original image", preview_image_url = "URL of thumbnail") ` --``` Object (video) = VideoSendMessage (original_content_url = "URL of original video", preview_image_url = "URL of thumbnail") --``` Object (video) = AudioSendMessage (original_content_url = "URL of original audio", duration = "length (within 1 minute)") `` -- Object (Video) = LocationSendMessage (title = "Title", address = "Address", latitude = "Latitude", longitude = "Longitude") `` ` -- Object (video) = StickerSendMessage (package_id = "package ID", sticker_id = "sticker ID") `` `

Recommended Posts

Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine
Tweet (API 1.1) on Google App Engine for Python
[Python] Using Line API [1st Creation of Beauty Bot]
Getting Started with Google App Engine for Python & PHP
Runtime version of Google App Engine / Python Standard Environment
[LINE Messaging API] Create parrot return BOT with Python
Settings when developing App Engine / Python apps with VS Code
I made a Chatbot using LINE Messaging API and Python
Play with YouTube Data API v3 using Google API Python Client
Easy-to-understand demo of Imagemap Message of LINE Messaging API [PHP] [Ruby] [Python]
Implementation of CRUD using REST API with Python + Django Rest framework + igGrid
[Python3] Google translate google translate without using api
Settings when writing Google App Engine / Python apps in Intellij Idea
Google App Engine development with Docker
[LINE Messaging API] Create a BOT that connects with someone with Python
Using properties files with Flexible Environment Java 8 on Google App Engine
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
When using MeCab with virtualenv python
Precautions when using six with Python 2.5
[SEO] Flow / sample code when using Google Analytics API in Python
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
[Windows] Cause and solution of NotImplementedError when using asyncio.create_subprocess_shell () with Fast API
Deploy a Python app on Google App Engine and integrate it with GitHub
How to deal with OAuth2 error when using Google APIs from Python
[Python] Run Flask on Google App Engine
Try using Python with Google Cloud Functions
Working with OpenStack using the Python SDK
I can't deploy with google app engine
Creating Google Spreadsheet using Python / Google Data API
About the problem that the python version of Google App Engine does not mesh
An example of cloudbuild.yaml when auto-deploying Django to App Engine with Cloud Build
When using PyQtGraph with Python Pyside, pay attention to the order of import
Personal tips when doing various things with Python 3
[Python] A story about making a LINE Bot with a practical manned function on its own without using Salesforce [Messaging API]
What are you using when testing with Python?
Recent Ability of Image Recognition-MS State-of-the-art Research Results Using Computer Vision API with Python
Google App Engine SDK / Go (Linux version) and Python installed by linuxbrew are incompatible
[GCP] [Python] Deploy API serverless with Google Cloud Functions!
Upload JPG file using Google Drive API in Python
[Python] Easy Google Translate app using Eel and Googletrans
Initial settings when using the foursquare API in python
[LINE Messaging API] Create a rich menu in Python
Try projective transformation of images using OpenCV with Python
[Question] About API conversion of chat bot using Python
Easy way to scrape with python using Google Colab
Deploy a Django application on Google App Engine (Python3)
Google App Engine / Python development environment construction procedure (late 2014)
[Python] Get insight data using Google My Business API
Register a ticket with redmine API using python requests
(Beginner) Basic usage of Datastore on Google App Engine
OpenVINO using Inference Engine Python API in PC environment
Troublesome story when using Python3 with VScode on ubuntu
How to analyze with Google Colaboratory using Kaggle API
How to use Django on Google App Engine / Python
Speech transcription procedure using Python and Google Cloud Speech API
Speech file recognition by Google Speech API v2 using Python
Made "Unofficial Apple Refurbished Product Introduction" BOT with LINE Messaging API (v2) + API Gateway + lambda (python)
I built an application with Lambda that notifies LINE of "likes" using the Qiita API
Read English sentences by hitting Google Translate API with Python without using the distributed module