newbot.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.get("/msg/{item_id}")
  48. async def coffee_msg(item_id):
  49. line_bot_api.push_message(item_id, TextSendMessage(text='開啟下方完整券樣(密碼9888)至櫃台掃碼兌換。https://txp.rs/v/EvX69b4Xq9'))
  50. line_bot_api.push_message(item_id, ImageSendMessage('https://cal.ptt.cx:5443/static/images/finalcoupon.png', 'https://cal.ptt.cx:5443/static/images/finalcoupon.png') )
  51. print('send images')
  52. return {"code":"ok" }
  53. @app.post('/callback')
  54. async def callback(request: fastapi.Request):
  55. signature = request.headers['X-Line-Signature']
  56. body = await request.body()
  57. handler.handle(body.decode('utf-8'), signature)
  58. return 'OK'
  59. @handler.add(FollowEvent)
  60. def handle_follow(event):
  61. print("in Follow")
  62. print(event.source.user_id)
  63. button_template_message =ButtonsTemplate(
  64. thumbnail_image_url="https://cal.ptt.cx:5443/static/images/coupon.png",
  65. title='Menu',
  66. text='填裝修問卷💖,送City Cafe 乙杯及裝修折抵券五萬元!請務必填寫真實資訊。🌻',
  67. image_size="cover",
  68. actions=[
  69. URITemplateAction(
  70. label='點我填問卷',
  71. uri='http://q.ptt.cx/a1/index-line.html?userid='+event.source.user_id
  72. ),
  73. ]
  74. )
  75. line_bot_api.reply_message(
  76. event.reply_token,
  77. TemplateSendMessage(
  78. alt_text="Follow Event",
  79. template=button_template_message
  80. )
  81. )
  82. @handler.add(linebotModels.MessageEvent, message=linebotModels.TextMessage)
  83. def message_text(event):
  84. if event.message.text == 'push':
  85. line_bot_api.push_message(
  86. event.source.user_id, [
  87. TextSendMessage(text='PUSH!'),
  88. ]
  89. )
  90. if event.message.text == '叫':
  91. line_bot_api.reply_message(
  92. event.reply_token, linebotModels.AudioSendMessage(
  93. original_content_url=f'{baseUrl}/static/audio/noot_noot.mp3', duration=1000))
  94. if event.message.text=='ok':
  95. line_bot_api.reply_message(
  96. event.reply_token,
  97. TextSendMessage(text='ChoozMo 的FastAPI LINE Bot'))
  98. if event.message.text=='btn':
  99. line_bot_api.reply_message(
  100. event.reply_token,
  101. TextSendMessage(
  102. text='Quick reply',
  103. quick_reply=QuickReply(
  104. items=[
  105. QuickReplyButton(
  106. action=PostbackAction(label="label1", data="data1")
  107. ),
  108. QuickReplyButton(
  109. action=MessageAction(label="label2", text="text2")
  110. ),
  111. QuickReplyButton(
  112. action=DatetimePickerAction(label="label3",
  113. data="data3",
  114. mode="date")
  115. ),
  116. QuickReplyButton(
  117. action=CameraAction(label="label4")
  118. ),
  119. QuickReplyButton(
  120. action=CameraRollAction(label="label5")
  121. ),
  122. QuickReplyButton(
  123. action=LocationAction(label="label6")
  124. ),
  125. ])))
  126. #if __name__ == "__main__":
  127. # 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'))
  128. # app.run(host='0.0.0.0', port=14404,ssl_context=('/tmp/cert.pem','/tmp/chain.pem' ))