Spaces:
Running
Running
from fastapi import FastAPI | |
from linebot import LineBotApi, WebhookHandler | |
from linebot.exceptions import InvalidSignatureError | |
from linebot.models import MessageEvent, TextMessage | |
from starlette.exceptions import HTTPException | |
import os | |
app = FastAPI() | |
line_bot_api = LineBotApi(os.environ["LINE_CHANNEL_ACCESS_TOKEN"]) | |
handler = WebhookHandler(os.environ["LINE_CHANNEL_SECRET"]) | |
def root(): | |
return {"title": "Echo Bot"} | |
async def callback( | |
request: Request, | |
background_tasks: BackgroundTasks, | |
x_line_signature=Header(None), | |
): | |
body = await request.body() | |
try: | |
background_tasks.add_task( | |
handler.handle, body.decode("utf-8"), x_line_signature | |
) | |
except InvalidSignatureError: | |
raise HTTPException(status_code=400, detail="Invalid signature") | |
return "ok" | |
def handle_message(event): | |
if event.type != "message" or event.message.type != "text": | |
return | |
message = TextMessage(text=event.message.text) | |
line_bot_api.reply_message(event.reply_token, message) |