newbot.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import copy
  2. import fastapi
  3. import fastapi.staticfiles as fastapiStaticfiles
  4. import linebot
  5. #uvicorn main:app --port 443 --ssl-keyfile=/etc/letsencrypt/live/ptt.cx/key.pem --ssl-certfile=/etc/letsencrypt/live/ptt.cx/cert.pem
  6. app = fastapi.FastAPI()
  7. app.mount(
  8. '/static', fastapiStaticfiles.StaticFiles(directory='static'), name='static')
  9. from linebot import (
  10. LineBotApi, WebhookHandler
  11. )
  12. from linebot.exceptions import (
  13. InvalidSignatureError
  14. )
  15. from linebot.models import (
  16. MessageEvent, TextMessage,ImageSendMessage, TextSendMessage,FlexSendMessage, TemplateSendMessage,CarouselTemplate,ConfirmTemplate,PostbackAction,MessageAction,CarouselColumn,URIAction
  17. )
  18. import json
  19. import codecs
  20. line_bot_api = LineBotApi('FvP2AkKj3FSuTjY8Jhwn23DPIb38yDZBliLrDjRA8j+UOQPku+kH2Bz2coDFl4gruRAiLuHuBbtRpILXlN54zJ+bWF5IwU86FClG47VZxCcDcAkhwqCqjYi/Ju7dBzlg963d9CV1KiiBwoiqvV6J7AdB04t89/1O/w1cDnyilFU=')
  21. handler = WebhookHandler('58a73d69c3ad6416e0b465e64c30526a')
  22. #line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
  23. #handler = WebhookHandler('YOUR_CHANNEL_SECRET')
  24. @app.post('/callback')
  25. async def callback(request: fastapi.Request):
  26. signature = request.headers['X-Line-Signature']
  27. body = await request.body()
  28. bot.handle(body.decode('utf-8'), signature)
  29. return 'OK'
  30. @handler.add(linebotModels.MessageEvent, message=linebotModels.TextMessage)
  31. def message_text(event):
  32. if event.message.text == '叫':
  33. lineBotApi.reply_message(
  34. event.reply_token, linebotModels.AudioSendMessage(
  35. original_content_url=f'{baseUrl}/static/audio/noot_noot.mp3', duration=1000))
  36. #if __name__ == "__main__":
  37. # app.run(host='0.0.0.0', port=443,ssl_context=('/etc/letsencrypt/live/ptt.cx/fullchain.pem', '/etc/letsencrypt/live/ptt.cx/privkey.pem'))
  38. # app.run(host='0.0.0.0', port=14404,ssl_context=('/tmp/cert.pem','/tmp/chain.pem' ))