Python, Lambda, API Gateway에서 LINE 채팅 Bot 만들기
7804 단어 람다파이썬APIGatewaylinebotAWS
LINE Developers에 채널 등록
를 열고 '지금 시작하기'를 눌러 채널을 만듭니다.
AWS Lambda 함수 생성
Lambda 화면으로 전환하여 함수를 처음부터 만들어 봅시다.

함수 이름은 LINEBot이고 런타임은 Python 3.8입니다. 그리고 함수 만들기를 누르자.
나중에 Lambda 환경 설정을 편집합시다.

값에 Messaging API의 Channel access token (엄청 긴 녀석)을 복사하여 저장하십시오.
여기에서 "lambda_function.py"를 편집하여 실제로 코드를 작성합니다.
lambda_function.py 편집
lambda_function.py
import logging
import os
import urllib.request
import json
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def lambda_handler(event, context):
logger.info(event)
# JSON文字列をPythonオブジェクトに変換してをeventsをループで回す
for message_event in json.loads(event['body'])['events']:
logger.info(json.dumps(message_event))
url = 'https://api.line.me/v2/bot/message/reply'
# リクエストヘッダー
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + os.environ['ACCESSTOKEN']
}
# リクエストボディ
data = {
'replyToken': message_event['replyToken'],
'messages': [
{
"type": "text",
"text": message_event['message']['text'],
}
]
}
req = urllib.request.Request(url=url, data=json.dumps(data).encode('utf-8'), method='POST', headers=headers)
with urllib.request.urlopen(req) as res:
logger.info(res.read().decode("utf-8"))
logger는 Cloud Watch에서 log를 출력하고 있지 않아도 됩니다!
htps : //에서 ゔぇぺぺrs. 네. 비 · 자 / 레후 렌세 / 메사 긴 g 아피 / # 센 드 레이 ply
공식적으로 아래와 같은 방법으로 POST 요청하면 좋다고 쓰고 있었습니다.
HTTPリクエスト POST
https://api.line.me/v2/bot/message/reply
リクエストヘッダー
Content-Type application/json
Authorization Bearer {channel access token}
リクエストボディ
replyToken Webhookで受信する応答トークン
messages 送信するメッセージ
그리고 앵무새를 되돌리려면 다음과 같은 구조로되어 있었기 때문에"events":[{
"replyToken":"xxxxxxxxxxxxxxxxxxx",
"message":{"type":"text","id":"xxxxxxxxxxxxxxx","text":"こんにちは"}
}]
"text": message_event['message']['text']로 앵무새를 반환할 수 있다.API Gateway 설정

Lambda가 방금 만든 LINEBot의 "함수 개요"에서 "트리거 추가"버튼을 누릅니다. "트리거 선택"에서 "API GateWay", "API 만들기"를 각각 선택하고 API 유형은 "HTTP"를 선택합니다. 보안은 이번에는 "오픈"을 선택합니다. 지금까지 완료되면 "추가"버튼을 누릅니다.

그런 다음 방금 만든 트리거의 API 끝점을 복사하여 Messaging API의 Webhook URL에 붙여넣습니다.
완성

그러면 이렇게 앵무새 반환 채팅 bot의 완성입니다! !
참고문헌
Reference
이 문제에 관하여(Python, Lambda, API Gateway에서 LINE 채팅 Bot 만들기), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/ymktmk_tt/items/7ad4e63e62795bb2418b텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)