When processing images using AWS Lambda etc. or when using it as a resource of AWS.rekognition I hope it helps. This time, I will skip the settings of LINE Developper.
If you do a similar search, many people are using the SDK, but this time I would like to avoid using it.
Part of the main function
lambda_function.py
import requests
import os
import json
import boto3
from io import BytesIO
#Header generation
HEADER = {
'Content-type':
'application/json',
'Authorization':
'Bearer ' +'Channel access token'
}
#main
def lambda_handler(event, context):
#Json Load
body = json.loads(event['body'])
for event in body['events']:
payload = {'replyToken': event['replyToken'], 'messages': []}
#When ImageMessage arrives
if event['message']['type'] == 'image':
#HEADER and messages(pyload)With post
if len(payload['messages']) > 0:
response = requests.post(
'https://api.line.me/v2/bot/message/reply',
headers=HEADER,
data=json.dumps(payload))
I want to save the image when ImageMessage arrives, so
lambda_function.py
#When ImageMessage arrives
if event['message']['type'] == 'image':
I would like to write the process in this IF statement.
lambda_function.py
MessageId = event['message']['id'] #Message ID
ImageFile = requests.get('https://api-data.line.me/v2/bot/message/
'+ MessageId +'/content',headers=HEADER) #Get Image content
Image_bin = BytesIO(ImageFile.content)
Image = Image_bin.getvalue() #Image acquisition
Get content ← has more details, so please read it if you are interested.
lambda_function.py
S3 = boto3.client('s3')
FileName = MessageId + '.jpeg' #Message ID+File name jpeg
#put process to s3
S3.put_object(Bucket='Bucket name',
Body=Image, #Photo
Key=FileName) #file name
I think it's okay if you write the processing up to this point, send an image from LINE, and save it in S3. If you feel uncomfortable that the image is saved and remains every time during processing, write the following code I will solve it.
lambda_function.py
S3.delete_object(Bucket="Bucket name", Key=FileName) #Delete transmitted image
Recommended Posts