|
@@ -2,12 +2,15 @@ import uvicorn
|
|
import fastapi
|
|
import fastapi
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from fastapi.middleware.cors import CORSMiddleware
|
|
from linebot import LineBotApi, WebhookHandler
|
|
from linebot import LineBotApi, WebhookHandler
|
|
-from linebot.models import (
|
|
|
|
|
|
+from linebot.models import (
|
|
MessageEvent, TextMessage, TextSendMessage, FollowEvent, TemplateSendMessage, ButtonsTemplate, URITemplateAction,
|
|
MessageEvent, TextMessage, TextSendMessage, FollowEvent, TemplateSendMessage, ButtonsTemplate, URITemplateAction,
|
|
- )
|
|
|
|
|
|
+)
|
|
import dataset
|
|
import dataset
|
|
import requests
|
|
import requests
|
|
import json
|
|
import json
|
|
|
|
+import qrcode
|
|
|
|
+from PIL import Image
|
|
|
|
+import base64, io
|
|
|
|
|
|
app = fastapi.FastAPI()
|
|
app = fastapi.FastAPI()
|
|
|
|
|
|
@@ -19,7 +22,6 @@ app.add_middleware(
|
|
allow_headers=["*"],
|
|
allow_headers=["*"],
|
|
)
|
|
)
|
|
|
|
|
|
-
|
|
|
|
# bot config
|
|
# bot config
|
|
line_bot_api = LineBotApi("SJT7VPT4RMQFLcS27jQBy3FcC24gtDrkcwJWZ5Xzqesr5T78LOKudHEJzt0k3b2S7n4KPwf27J7DVz2c8NQ4plSaaQylEeB1cYrfejaE/RPG/lCIQBYe4iBTzo26s4i2PcmT89837per/lTyvhVIKAdB04t89/1O/w1cDnyilFU=")
|
|
line_bot_api = LineBotApi("SJT7VPT4RMQFLcS27jQBy3FcC24gtDrkcwJWZ5Xzqesr5T78LOKudHEJzt0k3b2S7n4KPwf27J7DVz2c8NQ4plSaaQylEeB1cYrfejaE/RPG/lCIQBYe4iBTzo26s4i2PcmT89837per/lTyvhVIKAdB04t89/1O/w1cDnyilFU=")
|
|
handler = WebhookHandler("411ae3ef7e766739ed2c2c27b249d010")
|
|
handler = WebhookHandler("411ae3ef7e766739ed2c2c27b249d010")
|
|
@@ -36,20 +38,57 @@ async def callback(request: fastapi.Request):
|
|
handler.handle(body.decode('utf-8'), signature)
|
|
handler.handle(body.decode('utf-8'), signature)
|
|
return 'OK'
|
|
return 'OK'
|
|
|
|
|
|
|
|
+
|
|
# nft collection api
|
|
# nft collection api
|
|
@app.get("/collection/{userid}")
|
|
@app.get("/collection/{userid}")
|
|
def collection(userid):
|
|
def collection(userid):
|
|
xname = userid
|
|
xname = userid
|
|
- sql = 'SELECT a.* FROM nft a INNER JOIN ( SELECT userid FROM users WHERE userid="'+xname+'" ) b ON b.userid = a.userid'
|
|
|
|
|
|
+ sql = 'SELECT a.* FROM nft a INNER JOIN ( SELECT userid FROM users WHERE userid="' + xname + '" ) b ON b.userid = a.userid'
|
|
result = db.query(sql)
|
|
result = db.query(sql)
|
|
- rows={}
|
|
|
|
|
|
+ rows = {}
|
|
i = 0
|
|
i = 0
|
|
for row in result:
|
|
for row in result:
|
|
- rows[i]=row
|
|
|
|
- i+=1
|
|
|
|
|
|
+ rows[i] = row
|
|
|
|
+ i += 1
|
|
return rows
|
|
return rows
|
|
|
|
|
|
|
|
|
|
|
|
+# receive handler
|
|
|
|
+@app.get("/receive/{userid}")
|
|
|
|
+def receive(userid):
|
|
|
|
+
|
|
|
|
+ # generate qr code from user id
|
|
|
|
+ results = table.find_one(userid=userid)
|
|
|
|
+ address = results['useraddress']
|
|
|
|
+ qr = qrcode.QRCode(
|
|
|
|
+ version=1,
|
|
|
|
+ box_size=10,
|
|
|
|
+ border=5)
|
|
|
|
+ qr.add_data(address)
|
|
|
|
+ qr.make(fit=True)
|
|
|
|
+
|
|
|
|
+ img_qr = qr.make_image(fill='black', back_color='white')
|
|
|
|
+ filename = "qrcode/" + userid + '.png'
|
|
|
|
+ img_save = img_qr.save(filename)
|
|
|
|
+
|
|
|
|
+ # open file
|
|
|
|
+ im = Image.open(filename)
|
|
|
|
+
|
|
|
|
+ # convert
|
|
|
|
+ imgByteArr = io.BytesIO()
|
|
|
|
+ im.save(imgByteArr, format='PNG')
|
|
|
|
+
|
|
|
|
+ # decode to ascii
|
|
|
|
+ x = imgByteArr.getvalue()
|
|
|
|
+ base64.b64encode(x).decode('ascii')
|
|
|
|
+ encode = base64.b64encode(x).decode('ascii')
|
|
|
|
+
|
|
|
|
+ # add to db
|
|
|
|
+ data = dict(base64=encode)
|
|
|
|
+ result = data['base64']
|
|
|
|
+ # print(data['base64'])
|
|
|
|
+ # table.insert(data)
|
|
|
|
+ table.update(dict(userid=userid, base=result), ['userid'])
|
|
|
|
|
|
# follow event
|
|
# follow event
|
|
@handler.add(FollowEvent)
|
|
@handler.add(FollowEvent)
|
|
@@ -57,13 +96,13 @@ def handle_follow(event):
|
|
# get user id when follow
|
|
# get user id when follow
|
|
real_user_id = event.source.user_id
|
|
real_user_id = event.source.user_id
|
|
rand_num = '_test_000_000_002'
|
|
rand_num = '_test_000_000_002'
|
|
- user_id = event.source.user_id+rand_num
|
|
|
|
|
|
+ user_id = event.source.user_id + rand_num
|
|
|
|
|
|
# create user account
|
|
# create user account
|
|
url = 'https://nft-api-staging.joyso.io/api/v1/accounts'
|
|
url = 'https://nft-api-staging.joyso.io/api/v1/accounts'
|
|
headers = {'Authorization': 'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'}
|
|
headers = {'Authorization': 'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'}
|
|
# setup for temp use (unique id)
|
|
# setup for temp use (unique id)
|
|
- data = 'uid='+user_id
|
|
|
|
|
|
+ data = 'uid=' + user_id
|
|
r = requests.post(url=url, headers=headers, data=data)
|
|
r = requests.post(url=url, headers=headers, data=data)
|
|
# extract the account address
|
|
# extract the account address
|
|
dict_str = json.loads(r.text)
|
|
dict_str = json.loads(r.text)
|
|
@@ -72,11 +111,11 @@ def handle_follow(event):
|
|
# write in sql
|
|
# write in sql
|
|
data = dict(userid=real_user_id, useraddress=user_address)
|
|
data = dict(userid=real_user_id, useraddress=user_address)
|
|
table.insert(data)
|
|
table.insert(data)
|
|
- print(" Follow event: "+event.source.user_id)
|
|
|
|
|
|
+ print(" Follow event: " + event.source.user_id)
|
|
line_bot_api.reply_message(
|
|
line_bot_api.reply_message(
|
|
event.reply_token,
|
|
event.reply_token,
|
|
TextSendMessage(text='歡迎加入好友'))
|
|
TextSendMessage(text='歡迎加入好友'))
|
|
-
|
|
|
|
|
|
+
|
|
# message handler
|
|
# message handler
|
|
@handler.add(MessageEvent, message=TextMessage)
|
|
@handler.add(MessageEvent, message=TextMessage)
|
|
def message(event):
|
|
def message(event):
|
|
@@ -94,16 +133,28 @@ def message(event):
|
|
alt_text="Receive",
|
|
alt_text="Receive",
|
|
template=button_template_message))
|
|
template=button_template_message))
|
|
|
|
|
|
- elif '我要接收' in event.message.text:
|
|
|
|
- user_id = event.source.user_id
|
|
|
|
- print(" message event: "+user_id)
|
|
|
|
|
|
+ elif '我要接收' in event.message.text:
|
|
button_template_message = ButtonsTemplate(
|
|
button_template_message = ButtonsTemplate(
|
|
title=' ',
|
|
title=' ',
|
|
text='點擊並打開接收頁面,即可分享接收地址給對方!',
|
|
text='點擊並打開接收頁面,即可分享接收地址給對方!',
|
|
actions=[
|
|
actions=[
|
|
URITemplateAction(
|
|
URITemplateAction(
|
|
label='打開接收頁面',
|
|
label='打開接收頁面',
|
|
- uri='http://q.ptt.cx/a1/index-line.html?userid=' + event.source.user_id),])
|
|
|
|
|
|
+ uri='http://ark.cards/receive/' + event.source.user_id), ])
|
|
|
|
+ line_bot_api.reply_message(
|
|
|
|
+ event.reply_token,
|
|
|
|
+ TemplateSendMessage(
|
|
|
|
+ alt_text="Receive",
|
|
|
|
+ template=button_template_message))
|
|
|
|
+
|
|
|
|
+ elif '我的收藏' in event.message.text:
|
|
|
|
+ button_template_message = ButtonsTemplate(
|
|
|
|
+ title=' ',
|
|
|
|
+ text='點擊並打開接收頁面,即可分享接收地址給對方!',
|
|
|
|
+ actions=[
|
|
|
|
+ URITemplateAction(
|
|
|
|
+ label='打開接收頁面',
|
|
|
|
+ uri='http://ark.cards/collection/' + event.source.user_id), ])
|
|
line_bot_api.reply_message(
|
|
line_bot_api.reply_message(
|
|
event.reply_token,
|
|
event.reply_token,
|
|
TemplateSendMessage(
|
|
TemplateSendMessage(
|
|
@@ -115,5 +166,6 @@ def message(event):
|
|
event.reply_token,
|
|
event.reply_token,
|
|
TextSendMessage(text="更多的服務內容,歡迎請上我們的官網!(http://ark.cards)"))
|
|
TextSendMessage(text="更多的服務內容,歡迎請上我們的官網!(http://ark.cards)"))
|
|
|
|
|
|
|
|
+
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8228, reload=True)
|
|
uvicorn.run("main:app", host="0.0.0.0", port=8228, reload=True)
|