main.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import uvicorn
  2. import fastapi
  3. from linebot import LineBotApi, WebhookHandler
  4. from linebot.models import (
  5. MessageEvent, TextMessage, TextSendMessage, FollowEvent,
  6. )
  7. app = fastapi.FastAPI()
  8. # bot config
  9. line_bot_api = LineBotApi("SJT7VPT4RMQFLcS27jQBy3FcC24gtDrkcwJWZ5Xzqesr5T78LOKudHEJzt0k3b2S7n4KPwf27J7DVz2c8NQ4plSaaQylEeB1cYrfejaE/RPG/lCIQBYe4iBTzo26s4i2PcmT89837per/lTyvhVIKAdB04t89/1O/w1cDnyilFU=")
  10. handler = WebhookHandler("411ae3ef7e766739ed2c2c27b249d010")
  11. # callback event
  12. @app.post("/callback")
  13. async def callback(request: fastapi.Request):
  14. signature = request.headers['X-Line-Signature']
  15. body = await request.body()
  16. handler.handle(body.decode('utf-8'), signature)
  17. return 'OK'
  18. # follow event
  19. @handler.add(FollowEvent)
  20. def handle_follow(event):
  21. print(event.source.user_id)
  22. line_bot_api.reply_message(
  23. event.reply_token,
  24. TextSendMessage(text='歡迎加入好友'))
  25. if __name__ == '__main__':
  26. uvicorn.run("main:app", host="127.0.0.1", port=5000, reload=True)