12345678910111213141516171819202122232425262728293031323334 |
- import uvicorn
- import fastapi
- from linebot import LineBotApi, WebhookHandler
- from linebot.models import (
- MessageEvent, TextMessage, TextSendMessage, FollowEvent,
- )
- app = fastapi.FastAPI()
- # bot config
- line_bot_api = LineBotApi("SJT7VPT4RMQFLcS27jQBy3FcC24gtDrkcwJWZ5Xzqesr5T78LOKudHEJzt0k3b2S7n4KPwf27J7DVz2c8NQ4plSaaQylEeB1cYrfejaE/RPG/lCIQBYe4iBTzo26s4i2PcmT89837per/lTyvhVIKAdB04t89/1O/w1cDnyilFU=")
- handler = WebhookHandler("411ae3ef7e766739ed2c2c27b249d010")
- # callback event
- @app.post("/callback")
- async def callback(request: fastapi.Request):
- signature = request.headers['X-Line-Signature']
- body = await request.body()
- handler.handle(body.decode('utf-8'), signature)
- return 'OK'
- # follow event
- @handler.add(FollowEvent)
- def handle_follow(event):
- print(event.source.user_id)
- line_bot_api.reply_message(
- event.reply_token,
- TextSendMessage(text='歡迎加入好友'))
- if __name__ == '__main__':
- uvicorn.run("main:app", host="127.0.0.1", port=5000, reload=True)
|