We have summarized the procedure for creating a LineBot on AWS Lambda. I wrote from 1 without using line-bot-sdk-python. I use almost "reference" articles.
-What is Webhook? -[What is a webhook? I tried to draw it so that even children can understand it](https://kintone-blog.cybozu.co.jp/developer/000283.html#:~:text=Webhook%E3%81%A8%E3%81%AF% EF% BC% 88% E7% 95% A5% EF% BC% 89% E3% 80% 81,% E3% 81% A7% E9% 80% 9A% E7% 9F% A5% E3% 81% 99% E3 % 82% 8B% E4% BB% 95% E7% B5% 84% E3% 81% BF% E3% 81% A7% E3% 81% 99% E3% 80% 82)
import json
import logging
import os
import urllib.request
#Preparing for log output
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
#Log output of request contents
logger.info(event)
body = json.loads(event['body'])
for event in body['events']:
#Define a list of message objects for response
messages = []
# 2.Webhook event type is message
if event['type'] == 'message':
# 3.If the message type is text
if event['message']['type'] == 'text':
# 4.Use the content of the received text as a message object
if event['message']['text'] == 'honto':
messages.append({
'type': 'image',
'originalContentUrl': 'https://www.bizcourt.space/bizmarket/assets_c/2019/06/a_ocean_eyes_logo_bg_white_02-thumb-autox227-1304.png',
'previewImageUrl': 'https://www.bizcourt.space/bizmarket/assets_c/2019/06/a_ocean_eyes_logo_bg_white_02-thumb-autox227-1304.png'
})
else:
messages.append({
'type': 'text',
'text': event['message']['text']+ 'is'
})
#Define request information for response messages
url = 'https://api.line.me/v2/bot/message/reply'
headers = {
'Content-Type': 'application/json',
#Get a LINE channel access token from an environment variable
'Authorization': 'Bearer ' + os.environ['LINE_CHANNEL_ACCESS_TOKEN']
}
data = {
#Set response token and message object
'replyToken': event['replyToken'],
'messages': messages
}
request = urllib.request.Request(url, data = json.dumps(data).encode('utf-8'), method = 'POST', headers = headers)
with urllib.request.urlopen(request) as response:
#Log output of response contents
logger.info(response.read().decode("utf-8"))
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
Python AWS Lambda Function Handler
Throw a Post request
Use the urllib.request library. Official documentation Sample usage ... urllib.request is sufficient for Python HTTP client
Registration of environment variables From Using AWS Lambda Environment Variables
import os
region = os.environ['AWS_REGION']
You can use environment variables with.
・ Return the image Image object of official document Refer to this document
messages.append({
'type': 'image',
'originalContentUrl': 'Image URL',
'previewImageUrl': 'Image URL'
})
Then you can return the image.
LINE Messaging API x AWS Lambda simple response system # 1: Connect LINE to Lambda ← This service seems to be broken and the link is broken.
――Lambda is convenient. ――I want to improve my reading comprehension! ――LineBot seems to be applicable in more ways, so I'd like to do my best from now on.
Recommended Posts