CoolFace
Apppublic

rationalzeng/test

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py54 linesDownload Raw Back to root
1# -*- coding: utf-8 -*-2"""Untitled5.ipynb3 4Automatically generated by Colab.5 6Original file is located at7    https://colab.research.google.com/drive/1ufQspm4bRPq4tF0ubdS5sOjsxlSbv8vN8"""9 10import os11from flask import Flask, request, abort12 13from linebot.v3 import WebhookHandler14from linebot.v3.exceptions import InvalidSignatureError15from linebot.v3.webhooks import MessageEvent, TextMessageContent16from linebot.v3.messaging import (17    Configuration, ApiClient, MessagingApi,18    ReplyMessageRequest,19    TextMessage20)21 22app = Flask(__name__)23 24configuration = Configuration(access_token=os.getenv('LINE_CHANNEL_ACCESS_TOKEN'))25line_handler = WebhookHandler(os.getenv('LINE_CHANNEL_SECRET'))26 27@app.route("/callback", methods=['POST'])28def callback():29    signature = request.headers['X-Line-Signature']30    body = request.get_data(as_text=True)31    try:32        line_handler.handle(body, signature)33    except InvalidSignatureError:34        abort(400)35    return 'OK'36 37 38@line_handler.add(MessageEvent, message=TextMessageContent)39def handle_message(event):40    with ApiClient(configuration) as api_client:41        line_bot_api = MessagingApi(api_client)42        line_bot_api.reply_message(43            ReplyMessageRequest(44                reply_token=event.reply_token,45                messages=[46                    TextMessage(text=event.message.text)47                ]48            )49        )50 51 52 53if __name__ == "__main__":54    app.run()