import uvicorn import fastapi from linebot import LineBotApi, WebhookHandler from linebot.models import ( MessageEvent, TextMessage, TextSendMessage, FollowEvent, ) import configparser app = fastapi.FastAPI() # bot config config = configparser.ConfigParser() config.read('config.ini') line_bot_api = LineBotApi(config.get('line-bot', 'channel_access_token')) handler = WebhookHandler(config.get('line-bot', 'channel_secret')) # 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='歡迎加入好友')) # # test answer, echo handler # @handler.add(MessageEvent, message=TextMessage) # def echo(event): # if '誰' in event.message.text: # line_bot_api.reply_message( # event.reply_token, # TextSendMessage(text='我是您的專屬祕書')) # else: # line_bot_api.reply_message( # event.reply_token, # TextSendMessage(text=event.message.text) # ) if __name__ == '__main__': uvicorn.run("main:app", host="127.0.0.1", port=5000, reload=True)