Your Name 3 роки тому
батько
коміт
02aec2eb23
2 змінених файлів з 36 додано та 5 видалено
  1. 26 3
      step_question/apis/jbot.py
  2. 10 2
      step_question/apis/main.py

+ 26 - 3
step_question/apis/jbot.py

@@ -1,5 +1,11 @@
-from flask import Flask, request, abort
 import copy
+import fastapi
+import fastapi.staticfiles as fastapiStaticfiles
+app = fastapi.FastAPI()
+app.mount(
+    '/static', fastapiStaticfiles.StaticFiles(directory='static'), name='static')
+
+
 from linebot import (
     LineBotApi, WebhookHandler
 )
@@ -69,7 +75,6 @@ objelmt7['header']['backgroundColor']='#37C2D8'
 jsobj={"type": "carousel", "contents": [objelmt,objelmt2,objelmt3,objelmt4,objelmt5,objelmt6,objelmt7]}
 
 
-app = Flask(__name__)
 
 line_bot_api = LineBotApi('FvP2AkKj3FSuTjY8Jhwn23DPIb38yDZBliLrDjRA8j+UOQPku+kH2Bz2coDFl4gruRAiLuHuBbtRpILXlN54zJ+bWF5IwU86FClG47VZxCcDcAkhwqCqjYi/Ju7dBzlg963d9CV1KiiBwoiqvV6J7AdB04t89/1O/w1cDnyilFU=')
 handler = WebhookHandler('58a73d69c3ad6416e0b465e64c30526a')
@@ -77,7 +82,25 @@ handler = WebhookHandler('58a73d69c3ad6416e0b465e64c30526a')
 #handler = WebhookHandler('YOUR_CHANNEL_SECRET')
 
 
-@app.route("/callback", methods=['POST','GET'])
+@app.post('/callback')
+async def callback(request: fastapi.Request):
+    signature = request.headers['X-Line-Signature']
+    body = await request.body()
+    bot.handle(body.decode('utf-8'), signature)
+    return 'OK'
+
+
+@bot.add(linebotModels.MessageEvent, message=linebotModels.TextMessage)
+def message_text(event):
+    if event.message.text == '叫':
+        lineBotApi.reply_message(
+            event.reply_token, linebotModels.AudioSendMessage(
+                original_content_url=f'{baseUrl}/static/audio/noot_noot.mp3', duration=1000))
+
+
+
+
+#@app.route("/callback", methods=['POST','GET'])
 def callback():
     # get X-Line-Signature header value
     signature = request.headers['X-Line-Signature']

+ 10 - 2
step_question/apis/main.py

@@ -24,6 +24,8 @@ from botocore.exceptions import ClientError
 from email.mime.base import MIMEBase
 from email.mime.application import MIMEApplication
 import random
+import requests
+
 
 app = FastAPI()
 app.add_middleware(
@@ -36,7 +38,7 @@ app.add_middleware(
 
 
 coffee='0'
-def hhh_send_mail(email,coffee=False):
+def hhh_send_mail(email,coffee=False,lineid=None):
     SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
     RECIPIENT = "jeweiliang@gmail.com"
     AWS_REGION = "us-east-1"
@@ -54,6 +56,12 @@ def hhh_send_mail(email,coffee=False):
         # Set message body
         if coffee:
             body = MIMEText("感謝填寫幸福空間問卷,開啟下方完整券樣(密碼9888)至櫃台掃碼兌換。https://txp.rs/v/EvX69b4Xq9  \n\n 附件是您的五萬元裝修折價券\n", "plain")
+            try:
+                if lineid is not None:
+                    x = requests.get('https://cal.ptt.cx:5443/msg/'+lineid)
+                    print(x.status_code)
+            except:
+                traceback.print_exc()
         else:
             body = MIMEText("感謝填寫幸福空間問卷,這是您的五萬元裝修折價券\n", "plain")
 
@@ -160,7 +168,7 @@ async def line_submit(request: Request):
     print(result)
     get_db().insert(result)
 #    mail_to_user(email)
-    x = threading.Thread(target=hhh_send_mail, args=(email,True))
+    x = threading.Thread(target=hhh_send_mail, args=(email,True,result['userid']))
     x.start()
 
     return RedirectResponse(url="/a1/index_complete_line_after.html", status_code=HTTP_302_FOUND)