newbot.py 7.0 KB

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