Your Name há 3 anos atrás
pai
commit
204baf6d62
2 ficheiros alterados com 241 adições e 1 exclusões
  1. 229 0
      step_question/apis/jbot.py
  2. 12 1
      step_question/apis/main.py

+ 229 - 0
step_question/apis/jbot.py

@@ -0,0 +1,229 @@
+from flask import Flask, request, abort
+import copy
+from linebot import (
+    LineBotApi, WebhookHandler
+)
+from linebot.exceptions import (
+    InvalidSignatureError
+)
+from linebot.models import (
+    MessageEvent, TextMessage,ImageSendMessage, TextSendMessage,FlexSendMessage, TemplateSendMessage,CarouselTemplate,ConfirmTemplate,PostbackAction,MessageAction,CarouselColumn,URIAction
+)
+import json
+import codecs
+
+#colors=[{'c1':}]
+## json editor : https://jsonformatter.org/json-editor
+
+fr=codecs.open('/home/jared/src/pi/bubble.json','r','utf-8')
+lines=fr.readlines()
+content=' '.join(lines)
+fr.close()
+
+objelmt=json.loads(content)
+objelmt['body']['contents'][0]['contents'][0]['text']='this is a test'
+
+objelmt['header']['contents'][2]['contents'][0]['backgroundColor']='#0D8186'
+objelmt['header']['contents'][2]['backgroundColor']='#9FD8E36E'
+objelmt['header']['backgroundColor']='#27ACB2'
+
+##7D51E4
+objelmt2=copy.deepcopy(objelmt)
+objelmt2['header']['contents'][2]['contents'][0]['backgroundColor']='#7D51E4'
+objelmt2['header']['contents'][2]['backgroundColor']='#9FD8E36E'
+objelmt2['header']['backgroundColor']='#A17DF5'
+
+
+##7D51E4
+objelmt3=copy.deepcopy(objelmt)
+objelmt3['header']['contents'][2]['contents'][0]['backgroundColor']='#DE5658'
+objelmt3['header']['contents'][2]['backgroundColor']='#FAD2A76E'
+objelmt3['header']['backgroundColor']='#FF6B6E'
+
+
+objelmt4=copy.deepcopy(objelmt)
+objelmt4['header']['contents'][2]['contents'][0]['backgroundColor']='#0367D3'
+objelmt4['header']['contents'][2]['backgroundColor']='#FAD2A76E'
+objelmt4['header']['backgroundColor']='#2387F3'
+
+
+objelmt5=copy.deepcopy(objelmt)
+objelmt5['header']['contents'][2]['contents'][0]['backgroundColor']='#FFC107'
+objelmt5['header']['contents'][2]['backgroundColor']='#FAD2A76E'
+objelmt5['header']['backgroundColor']='#FFE127'
+
+
+objelmt6=copy.deepcopy(objelmt)
+objelmt6['header']['contents'][2]['contents'][0]['backgroundColor']='#343A40'
+objelmt6['header']['contents'][2]['backgroundColor']='#FAD2A76E'
+objelmt6['header']['backgroundColor']='#545A60'
+
+
+objelmt7=copy.deepcopy(objelmt)
+objelmt7['header']['contents'][2]['contents'][0]['backgroundColor']='#17A2B8'
+objelmt7['header']['contents'][2]['backgroundColor']='#FAD2A76E'
+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')
+#line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
+#handler = WebhookHandler('YOUR_CHANNEL_SECRET')
+
+
+@app.route("/callback", methods=['POST','GET'])
+def callback():
+    # get X-Line-Signature header value
+    signature = request.headers['X-Line-Signature']
+
+    # get request body as text
+    body = request.get_data(as_text=True)
+    app.logger.info("Request body: " + body)
+    print("Request body: " + body)
+    # handle webhook body
+    try:
+        handler.handle(body, signature)
+    except InvalidSignatureError:
+        print("Invalid signature. Please check your channel access token/channel secret.")
+        abort(400)
+
+    return 'OK',200
+
+@app.route('/', methods=['GET'])
+def serve():
+    return "Hello world", 200
+
+@handler.add(MessageEvent, message=TextMessage)
+def handle_message(event):
+    print(event.message.text)
+    if event.message.text=='業績':
+        line_bot_api.reply_message(
+            event.reply_token,
+            TextSendMessage(text='您本月營收為9,287元'))
+
+    if event.message.text=='新貨':
+        line_bot_api.reply_message(
+            event.reply_token,
+            TextSendMessage(text='寶貝的毛巾該換囉! \
+小朋友開學必備!家中必備! \
+這款小方巾,不論是洗臉、嬰兒擦口水 \
+或是帶去學校當手帕都很適合 '))
+
+
+    if event.message.text=='防蚊':
+        url='https://pic.pimg.tw/youwin721/1593748433-4270703895-g.png'
+        line_bot_api.reply_message(
+            event.reply_token,
+            ImageSendMessage(url,url))
+
+
+    if event.message.text=='華 地址':
+        line_bot_api.reply_message(
+            event.reply_token,
+            TextSendMessage(text='林曉華的收件地址是: 110台北市信義區嘉興街235號'))
+
+
+
+
+
+
+    if event.message.text=='AA':
+        confirm_template_message = TemplateSendMessage(
+            alt_text='Confirm template',
+            template=ConfirmTemplate(
+                text='Are you sure?',
+                actions=[
+                    PostbackAction(
+                        label='postback',
+                        display_text='postback text',
+                        data='action=buy&itemid=1'
+                    ),
+                    MessageAction(
+                        label='message',
+                        text='message text'
+                    )
+                ]
+            )
+        )
+
+        line_bot_api.reply_message(event.reply_token,confirm_template_message)
+
+
+
+
+
+    elif event.message.text=='cc':
+
+        flex_message = FlexSendMessage(
+            alt_text='hello',
+            contents=jsobj
+        )
+        line_bot_api.reply_message(event.reply_token,flex_message)
+
+
+    elif event.message.text=='BB':
+        carousel_template_message = TemplateSendMessage(
+            alt_text='Carousel template',
+            template=CarouselTemplate(
+                columns=[
+                    CarouselColumn(
+                        thumbnail_image_url='https://github.githubassets.com/images/modules/site/logos/ibm-logo.png',
+                        title='this is menu1',
+                        text='description1',
+                        actions=[
+                            PostbackAction(
+                                label='postback1',
+                                display_text='postback text1',
+                                data='action=buy&itemid=1'
+                            ),
+                            MessageAction(
+                                label='message1',
+                                text='message text1'
+                            ),
+                            URIAction(
+                                label='uri1',
+                                uri='http://example.com/1'
+                            )
+                        ]
+                    ),
+                    CarouselColumn(
+                        thumbnail_image_url='https://github.githubassets.com/images/modules/site/logos/ibm-logo.png',
+                        title='this is menu2',
+                        text='description2',
+                        actions=[
+                            PostbackAction(
+                                label='postback2',
+                                display_text='postback text2',
+                                data='action=buy&itemid=2'
+                            ),
+                            MessageAction(
+                                label='message2',
+                                text='message text2'
+                            ),
+                            URIAction(
+                                label='uri2',
+                                uri='http://example.com/2'
+                            )
+                        ]
+                    )
+                ]
+            )
+        )
+        line_bot_api.reply_message(event.reply_token,carousel_template_message)
+
+    else:
+        line_bot_api.reply_message(
+            event.reply_token,
+            TextSendMessage(text=event.message.text))
+
+
+if __name__ == "__main__":
+    app.run(host='0.0.0.0', port=443,ssl_context=('/tmp/fullchain.pem', '/tmp/privkey.pem'))
+#    app.run(host='0.0.0.0', port=14404,ssl_context=('/tmp/cert.pem','/tmp/chain.pem' ))
+

+ 12 - 1
step_question/apis/main.py

@@ -23,7 +23,7 @@ import boto3
 from botocore.exceptions import ClientError
 from email.mime.base import MIMEBase
 from email.mime.application import MIMEApplication
-
+import random
 
 app = FastAPI()
 app.add_middleware(
@@ -35,6 +35,7 @@ app.add_middleware(
 )
 
 
+
 def hhh_send_mail(email,coffee=False):
     SENDER = "Gorgeous Space <noreply@hhh.com.tw>"
     RECIPIENT = "jeweiliang@gmail.com"
@@ -163,6 +164,16 @@ async def line_submit(request: Request):
 
 app.mount("/a1", StaticFiles(directory="static"), name="static")
 
+@app.get("/coffee")
+async def coffee():
+    val=random.randint()
+    coffee='0'
+    if val%2==0:
+        coffee='1'
+    return {'coffee':coffee}
+
+
+
 @app.get("/a1")
 async def redirect():
 #    url = app.url_path_for("/a1/index.html")