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