Parcourir la source

add check account to recieve page

ming il y a 2 ans
Parent
commit
4556789ab8
1 fichiers modifiés avec 58 ajouts et 0 suppressions
  1. 58 0
      app/api/api_v1/endpoints/line.py

+ 58 - 0
app/api/api_v1/endpoints/line.py

@@ -58,6 +58,63 @@ headers = {
     'Authorization':
         'Basic %s' % 'bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'
 }
+def check_account(event):
+    real_user_id = event.source.user_id
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
+    table = db['users']
+    result1 = table.find_one(userid=real_user_id)
+
+    # 都存在db的話
+    if result1:
+        db.close()
+        settings.line_bot_api.reply_message(
+            event.reply_token,
+            TextSendMessage(text='很高興再見到您!'))
+
+    # 建立全新使用者
+    else:
+        # create user account api
+        url = 'https://nft-api-staging.joyso.io/api/v1/accounts'
+        headers = {
+            'Authorization':
+            'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'
+        }
+        try:
+            profile = settings.line_bot_api.get_profile(real_user_id)
+            print(profile['displayName'])
+        except LineBotApiError as e:
+            pass
+        user_id = event.source.user_id
+        data = 'uid=' + user_id
+        r = requests.post(url=url, headers=headers, data=data)
+
+        # extract the account address
+        dict_str = json.loads(r.text)
+        user_account = dict_str['account']
+        user_address = user_account['address']
+
+        # generate qr code from user id
+        qr = qrcode.QRCode(
+            version=1,
+            box_size=10,
+            border=5)
+        qr.add_data(user_address)
+        qr.make(fit=True)
+        img_qr = qr.make_image(fill='black', back_color='white')
+        filename = "/var/www/ArkCard-Linebot/ArkCard-web/qrcode/" + \
+                   real_user_id + '.png'
+        img_qr.save(filename)
+
+        # add to db
+        data = dict(userid=real_user_id, useraddress=user_address)
+        table.insert(data)
+
+        db.close()
+
+        settings.line_bot_api.reply_message(
+            event.reply_token,
+            TextSendMessage(text='歡迎加入好友'))
+
 
 # callback event
 @router.post("/callback")
@@ -155,6 +212,7 @@ def message(event):
                 template=button_template_message))
 
     elif '我要接收' in event.message.text:
+        check_account(event)
         button_template_message = ButtonsTemplate(
             title=' ',
             text='點擊並打開接收頁面,即可分享接收地址給對方!',