line / line-bot-sdk-python

LINE Messaging API SDK for Python
https://pypi.python.org/pypi/line-bot-sdk
Apache License 2.0
1.87k stars 962 forks source link

Create new example for an echo-bot using AWS Lambda Functions, `linebot.v3` #627

Closed 1chooo closed 1 month ago

1chooo commented 2 months ago

Problem

Recently, while developing a Line Bot and working with AWS services, I noticed a lack of examples using linebot.v3 with AWS Lambda Functions. Therefore, I've created a Line Bot architecture using AWS Lambda Functions in a serverless setup. Below is the architecture diagram.

arch

Source Code

import json
import os

from linebot.v3 import WebhookHandler
from linebot.v3.messaging import (ApiClient, Configuration, MessagingApi,
                                  ReplyMessageRequest, TextMessage)
from linebot.v3.webhooks import MessageEvent, TextMessageContent

configuration = Configuration(access_token=os.getenv('CHANNEL_ACCESS_TOKEN'))
handler = WebhookHandler(os.getenv('CHANNEL_SECRET'))

@handler.add(MessageEvent, message=TextMessageContent)
def handle_message(event):
    with ApiClient(configuration) as api_client:
        line_bot_api = MessagingApi(api_client)

        line_bot_api.reply_message_with_http_info(
            ReplyMessageRequest(
                reply_token=event.reply_token,
                messages=[TextMessage(text=event.message.text)]
            )
        )

def lambda_handler(event, context):
    try: 
        body = event['body']
        signature = event['headers']['x-line-signature']
        handler.handle(body, signature)
        return {
            'statusCode': 200,
            'body': json.dumps('Hello from Lambda!')
        }
    except Exception as e:
        return {
            'statusCode': 500,
            'body': json.dumps(str(e))
        }

Solution

I will submit pull requests to create the new example -- aws-lambda-echo/ along with the necessary documentation. Additionally, I will include the requirements.txt file and a comprehensive README.md.

What I've done?

Check the post I've write on my medium, and it's the traditional Chinese Version 👉🏻 AWS: 用 AWS Lambda Function 開發 Serverless Line Bot — 1

Demo

demo

If it's convenient, you can also assign this feature to me so that I can contribute to the line-bot-sdk-python development.

Thanks, Hugo

mokuzon commented 1 month ago

@1chooo Apologies for the delay in responding. Thank you for your suggestion.

I'm sorry, but we do not intend to add more samples at present. Increasing the number of samples would increase maintenance costs, and the proposed samples are not significantly different from the existing samples.