newbot.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import copy
  2. import fastapi
  3. import fastapi.staticfiles as fastapiStaticfiles
  4. import linebot
  5. import linebot.models as linebotModels
  6. from linebot.models import (
  7. MessageEvent, TextMessage, TextSendMessage,
  8. SourceUser, SourceGroup, SourceRoom,
  9. TemplateSendMessage, ConfirmTemplate, MessageAction,
  10. ButtonsTemplate, ImageCarouselTemplate, ImageCarouselColumn, URIAction,
  11. PostbackAction, DatetimePickerAction,
  12. CameraAction, CameraRollAction, LocationAction,
  13. CarouselTemplate, CarouselColumn, PostbackEvent,
  14. StickerMessage, StickerSendMessage, LocationMessage, LocationSendMessage,
  15. ImageMessage, VideoMessage, AudioMessage, FileMessage,
  16. UnfollowEvent, FollowEvent, JoinEvent, LeaveEvent, BeaconEvent,
  17. MemberJoinedEvent, MemberLeftEvent,
  18. FlexSendMessage, BubbleContainer, ImageComponent, BoxComponent,
  19. TextComponent, IconComponent, ButtonComponent, MessageTemplateAction,
  20. SeparatorComponent, QuickReply, QuickReplyButton,URITemplateAction,
  21. ImageSendMessage)
  22. #uvicorn main:app --host 0.0.0.0 --port 443 --ssl-keyfile=/etc/letsencrypt/live/cal.ptt.cx/privkey.pem --ssl-certfile=/etc/letsencrypt/live/cal.ptt.cx/fullchain.pem
  23. #uvicorn main:app --host 0.0.0.0 --port 443 --ssl-keyfile=/etc/letsencrypt/live/ptt.cx/privkey.pem --ssl-certfile=/etc/letsencrypt/live/ptt.cx/chain.pem
  24. #uvicorn main:app --host 0.0.0.0 --port 443 --key-file=/etc/letsencrypt/live/ptt.cx/privkey.pem --certfile=/etc/letsencrypt/live/ptt.cx/cert.pem
  25. # --keyfile=./key.pem --certfile=./cert.pem
  26. # --ssl-cert-reqs 1
  27. #
  28. # --ssl-ca-certs=/etc/letsencrypt/live/ptt.cx/fullchain.crt
  29. app = fastapi.FastAPI()
  30. app.mount(
  31. '/static', fastapiStaticfiles.StaticFiles(directory='static'), name='static')
  32. from linebot import (
  33. LineBotApi, WebhookHandler
  34. )
  35. from linebot.exceptions import (
  36. InvalidSignatureError
  37. )
  38. from linebot.models import (
  39. MessageEvent, TextMessage,ImageSendMessage, TextSendMessage,FlexSendMessage, TemplateSendMessage,CarouselTemplate,ConfirmTemplate,PostbackAction,MessageAction,CarouselColumn,URIAction
  40. )
  41. import json
  42. import codecs
  43. line_bot_api = LineBotApi('FvP2AkKj3FSuTjY8Jhwn23DPIb38yDZBliLrDjRA8j+UOQPku+kH2Bz2coDFl4gruRAiLuHuBbtRpILXlN54zJ+bWF5IwU86FClG47VZxCcDcAkhwqCqjYi/Ju7dBzlg963d9CV1KiiBwoiqvV6J7AdB04t89/1O/w1cDnyilFU=')
  44. handler = WebhookHandler('58a73d69c3ad6416e0b465e64c30526a')
  45. #line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
  46. #handler = WebhookHandler('YOUR_CHANNEL_SECRET')
  47. @app.post('/callback')
  48. async def callback(request: fastapi.Request):
  49. signature = request.headers['X-Line-Signature']
  50. body = await request.body()
  51. handler.handle(body.decode('utf-8'), signature)
  52. return 'OK'
  53. @handler.add(FollowEvent)
  54. def handle_follow(event):
  55. print("in Follow")
  56. button_template_message =ButtonsTemplate(
  57. thumbnail_image_url="https://cal.ptt.cx:5443/static/images/coupon.png",
  58. title='Menu',
  59. text='填裝修問卷💖,送City Cafe 乙杯及裝修折抵券五萬元!請務必填寫真實資訊。🌻',
  60. image_size="cover",
  61. actions=[
  62. URITemplateAction(
  63. label='點我填問卷',
  64. uri='http://q.ptt.cx/a1/index-line.html'
  65. ),
  66. ]
  67. )
  68. line_bot_api.reply_message(
  69. event.reply_token,
  70. TemplateSendMessage(
  71. alt_text="Follow Event",
  72. template=button_template_message
  73. )
  74. )
  75. @handler.add(linebotModels.MessageEvent, message=linebotModels.TextMessage)
  76. def message_text(event):
  77. if event.message.text == 'push':
  78. line_bot_api.push_message(
  79. event.source.user_id, [
  80. TextSendMessage(text='PUSH!'),
  81. ]
  82. )
  83. if event.message.text == '叫':
  84. line_bot_api.reply_message(
  85. event.reply_token, linebotModels.AudioSendMessage(
  86. original_content_url=f'{baseUrl}/static/audio/noot_noot.mp3', duration=1000))
  87. if event.message.text=='ok':
  88. line_bot_api.reply_message(
  89. event.reply_token,
  90. TextSendMessage(text='ChoozMo 的FastAPI LINE Bot'))
  91. if event.message.text=='btn':
  92. line_bot_api.reply_message(
  93. event.reply_token,
  94. TextSendMessage(
  95. text='Quick reply',
  96. quick_reply=QuickReply(
  97. items=[
  98. QuickReplyButton(
  99. action=PostbackAction(label="label1", data="data1")
  100. ),
  101. QuickReplyButton(
  102. action=MessageAction(label="label2", text="text2")
  103. ),
  104. QuickReplyButton(
  105. action=DatetimePickerAction(label="label3",
  106. data="data3",
  107. mode="date")
  108. ),
  109. QuickReplyButton(
  110. action=CameraAction(label="label4")
  111. ),
  112. QuickReplyButton(
  113. action=CameraRollAction(label="label5")
  114. ),
  115. QuickReplyButton(
  116. action=LocationAction(label="label6")
  117. ),
  118. ])))
  119. #if __name__ == "__main__":
  120. # 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'))
  121. # app.run(host='0.0.0.0', port=14404,ssl_context=('/tmp/cert.pem','/tmp/chain.pem' ))