main.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. import uvicorn
  2. import fastapi
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from fastapi.responses import HTMLResponse
  5. from linebot import LineBotApi, WebhookHandler
  6. from linebot.models import (
  7. MessageEvent, TextMessage, TextSendMessage, FollowEvent, TemplateSendMessage, ButtonsTemplate, URITemplateAction,
  8. )
  9. import dataset
  10. import requests
  11. import json
  12. import qrcode
  13. # from PIL import Image
  14. # import base64, io
  15. from random import randrange
  16. import models
  17. import datetime as dt
  18. app = fastapi.FastAPI()
  19. app.add_middleware(
  20. CORSMiddleware,
  21. allow_origins=['*'],
  22. allow_credentials=True,
  23. allow_methods=["*"],
  24. allow_headers=["*"],
  25. )
  26. # bot config
  27. line_bot_api = LineBotApi("SJT7VPT4RMQFLcS27jQBy3FcC24gtDrkcwJWZ5Xzqesr5T78LOKudHEJzt0k3b2S7n4KPwf27J7DVz2c8NQ4plSaaQylEeB1cYrfejaE/RPG/lCIQBYe4iBTzo26s4i2PcmT89837per/lTyvhVIKAdB04t89/1O/w1cDnyilFU=")
  28. handler = WebhookHandler("411ae3ef7e766739ed2c2c27b249d010")
  29. # callback event
  30. @app.post("/callback")
  31. async def callback(request: fastapi.Request):
  32. signature = request.headers['X-Line-Signature']
  33. body = await request.body()
  34. handler.handle(body.decode('utf-8'), signature)
  35. return 'OK'
  36. # follow event
  37. @handler.add(FollowEvent)
  38. def handle_follow(event):
  39. # get user id when follow
  40. real_user_id = event.source.user_id
  41. # db connect and search
  42. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  43. table = db['users']
  44. result1 = table.find_one(userid=real_user_id)
  45. # 都存在db的話
  46. if result1:
  47. db.close()
  48. line_bot_api.reply_message(
  49. event.reply_token,
  50. TextSendMessage(text='很高興再見到您!'))
  51. # 建立全新使用者
  52. else:
  53. # create user account api
  54. url = 'https://nft-api-staging.joyso.io/api/v1/accounts'
  55. headers = {'Authorization': 'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'}
  56. # setup for temp use (unique id)
  57. rand_num = str(randrange(99999))
  58. user_id = event.source.user_id + rand_num
  59. data = 'uid=' + user_id
  60. r = requests.post(url=url, headers=headers, data=data)
  61. # extract the account address
  62. dict_str = json.loads(r.text)
  63. user_account = dict_str['account']
  64. user_address = user_account['address']
  65. # generate qr code from user id
  66. qr = qrcode.QRCode(
  67. version=1,
  68. box_size=10,
  69. border=5)
  70. qr.add_data(user_address)
  71. qr.make(fit=True)
  72. img_qr = qr.make_image(fill='black', back_color='white')
  73. filename = "/var/www/ArkCard-Linebot/ArkCard-web/qrcode/" + real_user_id + '.png'
  74. img_save = img_qr.save(filename)
  75. # add to db
  76. data = dict(userid=real_user_id, useraddress=user_address)
  77. table.insert(data)
  78. db.close()
  79. line_bot_api.reply_message(
  80. event.reply_token,
  81. TextSendMessage(text='歡迎加入好友'))
  82. # message handler
  83. @handler.add(MessageEvent, message=TextMessage)
  84. def message(event):
  85. if '我要發送' in event.message.text:
  86. button_template_message = ButtonsTemplate(
  87. title=' ',
  88. text='點擊並打開收藏的NFT,可以選擇想要發送的NFT給對方!',
  89. actions=[
  90. URITemplateAction(
  91. label='打開發送頁',
  92. uri='https://ark.cards/collect.html?' + event.source.user_id),])
  93. line_bot_api.reply_message(
  94. event.reply_token,
  95. TemplateSendMessage(
  96. alt_text="Receive",
  97. template=button_template_message))
  98. elif '我要接收' in event.message.text:
  99. button_template_message = ButtonsTemplate(
  100. title=' ',
  101. text='點擊並打開接收頁面,即可分享接收地址給對方!',
  102. actions=[
  103. URITemplateAction(
  104. label='打開接收頁',
  105. uri='https://ark.cards/qr-code.html?' + event.source.user_id),])
  106. line_bot_api.reply_message(
  107. event.reply_token,
  108. TemplateSendMessage(
  109. alt_text="Receive",
  110. template=button_template_message))
  111. elif 'NFT商店' in event.message.text:
  112. button_template_message = ButtonsTemplate(
  113. title=' ',
  114. text='點擊並打開NFT商品頁,就可以購買您所想要的NFT商品哦!',
  115. actions=[
  116. URITemplateAction(
  117. label='打開NFT商品頁',
  118. uri='https://ark.cards/shop.html?' + event.source.user_id),])
  119. line_bot_api.reply_message(
  120. event.reply_token,
  121. TemplateSendMessage(
  122. alt_text="Receive",
  123. template=button_template_message))
  124. elif 'NFT收藏' in event.message.text:
  125. button_template_message = ButtonsTemplate(
  126. title=' ',
  127. text='點擊並打開收藏的NFT,可以查看收到的NFT!',
  128. actions=[
  129. URITemplateAction(
  130. label='打開收藏頁',
  131. uri='https://ark.cards/collect.html?' + event.source.user_id), ])
  132. line_bot_api.reply_message(
  133. event.reply_token,
  134. TemplateSendMessage(
  135. alt_text="Receive",
  136. template=button_template_message))
  137. else:
  138. button_template_message = ButtonsTemplate(
  139. title=' ',
  140. text='更多的服務內容,歡迎請上我們的官網!',
  141. actions=[
  142. URITemplateAction(
  143. label='ArkCard的官網',
  144. uri='https://ark.cards'),])
  145. line_bot_api.reply_message(
  146. event.reply_token,
  147. TemplateSendMessage(
  148. alt_text="Receive",
  149. template=button_template_message))
  150. # nft collection api
  151. @app.get("/collection/{userid}")
  152. def collection(userid):
  153. # 連到ownership表單去找,並回傳json
  154. # db connect
  155. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  156. table1 = db['ownership']
  157. nft = {}
  158. i = 0
  159. if not table1.find_one(userid=userid):
  160. db.close()
  161. return "error: user don't have any nft"
  162. else:
  163. results = db.query("SELECT nftid, amount FROM arkcard.ownership WHERE userid IN (SELECT userid FROM arkcard.ownership WHERE userid = '"+userid+"');")
  164. for item in results:
  165. nft[i] = item
  166. i += 1
  167. db.close()
  168. return nft
  169. # receive handler
  170. @app.get("/receive/{userid}")
  171. def receive(userid):
  172. # 確定要傳送的對象,到user去找到它
  173. # db connect
  174. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  175. table = db['users']
  176. if not table.find_one(userid=userid):
  177. db.close()
  178. return "ERROR: User Not Found"
  179. else:
  180. result = table.find_one(userid=userid)
  181. db.close()
  182. return {"userid": result['userid'], "address": result['address']}
  183. # send handler
  184. @app.post("/send")
  185. async def receive(userModel : models.TransactionNft):
  186. # 從網頁上選定好商品,在選定好數量後輸入收方地址;將把該商品轉到收方,並存下交易記錄
  187. # 從ownership找到該userid的擁有人,再把擁有人userid改為收方,並在trans留下記錄
  188. # db connect
  189. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  190. table1 = db['users']
  191. table2 = db['ownership']
  192. table3 = db['transactions']
  193. # find the userid from the selected nft
  194. fromuser = userModel.fromuser
  195. address = userModel.address
  196. result1 = table1.find_one(address=address)
  197. result2 = table1.find_one(userid=fromuser)
  198. # 例:
  199. # {
  200. # "fromuser": "a01",
  201. # "address": "0x4cd0ea8b1bdb5ab9249d96ccf3d8a0d3ada2bc76",
  202. # "dic": {"10":"2", "20":"3"}
  203. # }
  204. # 第一個為nftid, 第二個為amount,可以同時輸入多組nft給同一人
  205. the_list = userModel.dic
  206. fr_token = "from token"
  207. _hash = "hash"
  208. # confirm if the user exist
  209. # 如果沒有這個接受者,回傳錯誤
  210. if not result1:
  211. db.close()
  212. return {'msg': 'user not found'}
  213. # 確認發送方的nft夠不夠發放
  214. else:
  215. for nftid, amount in the_list.items():
  216. print("nft: "+nftid+",數量:"+ amount)
  217. # 直接針對取到的值去做確認數量和更新表單,最後再記錄
  218. # 確認sender的總數
  219. result3 = db.query('SELECT SUM(amount) AS total FROM arkcard.ownership WHERE userid="' + fromuser + '" AND nftid ="' + nftid + '";')
  220. # 把總數撈出來,存到total
  221. for row in result3:
  222. rows = row
  223. total = rows['total']
  224. # 如果總數不夠,就報錯回傳
  225. if total == None or total < int(amount):
  226. db.close()
  227. return {"messge: NFT not enough"}
  228. # 總數夠,在ownership刪去數量,並再新增ownership一筆
  229. else:
  230. # 今天到這,需要找出怎麼扣除總數的方式,目前找到的總數,會更新到資料欄位,導致原本才100個的,會被更新成499個,因為全加到一個
  231. # 找接收者的userid
  232. to_user = result1['userid']
  233. old_address = result2['address']
  234. print(total)
  235. new_amount = total - int(amount)
  236. # 更新原user總數
  237. result4 = db.query('DELETE FROM arkcard.ownership WHERE userid="' + fromuser + '" AND nftid ="' + nftid + '";')
  238. result5 = db.query('UPDATE arkcard.ownership SET amount = amount - "' + amount + '" WHERE userid = "' + fromuser + '" AND nftid = "' + nftid + '" LIMIT 1;')
  239. new_data1 = dict(userid=fromuser, address=old_address, nftid=nftid, amount=new_amount)
  240. table2.insert(new_data1)
  241. # 增加新user數量
  242. new_data2 = dict(userid=to_user, address=address, nftid=nftid, amount=int(amount))
  243. table2.insert(new_data2)
  244. # 在transactions記錄from和to, 以及時間等等
  245. now = dt.datetime.now()
  246. trans_data = dict(to_token=address, time=now, amount=int(amount), userid=fromuser, from_token=fr_token, hash=_hash)
  247. table3.insert(trans_data)
  248. db.close()
  249. return {"messge: NFT 夠"}
  250. # shop handler
  251. @app.get("/shop")
  252. def shop():
  253. # 為了顯示正確的nft網頁和數量,直接顯示nft表出來
  254. # db connect
  255. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  256. result1 = db.query('SELECT nftid, launched, amount, type, title, context FROM arkcard.nft WHERE launched="y" AND nftid NOT IN(SELECT nftid FROM arkcard.nft_to_event);')
  257. nfts = {}
  258. i = 0
  259. for row in result1:
  260. nfts[i] = row
  261. i += 1
  262. return nfts
  263. db.close()
  264. @app.post("/buy")
  265. async def buy(userModel : models.BuyNft):
  266. # db connect
  267. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  268. table1 = db['users']
  269. table2 = db['ownership']
  270. table3 = db['nft']
  271. table4 = db['transactions']
  272. shops = "shop"
  273. hash = "hash"
  274. # input
  275. the_list = userModel.dic
  276. address = userModel.address
  277. # 例:
  278. # {
  279. # "address": "d04",
  280. # "dic": {"1":"2","2":"3"}
  281. # }
  282. result1 = table1.find_one(address=address)
  283. # 找不到該使用者就回報錯誤
  284. if not result1:
  285. db.close()
  286. return "該用戶不存在!如果有疑問,請洽網站的服務信箱!"
  287. else:
  288. for nftid, amount in the_list.items():
  289. # 先user找到該使用者的userid留著備用
  290. userid = result1['userid']
  291. # 更新到ownership
  292. owner_data = [dict(userid=userid, address=address, nftid=nftid, amount=int(amount))]
  293. table2.insert_many(owner_data)
  294. # 更新nft數量
  295. result3 = table3.find_one(nftid=nftid)
  296. nft_total = result3['amount']
  297. total = int(nft_total) - int(amount)
  298. nft_data = dict(nftid=nftid, amount=int(total))
  299. table3.update(nft_data, ['nftid'])
  300. # 最後再來處理transactions
  301. now = dt.datetime.now()
  302. trans_data = dict(to_token=address, time=now, amount=int(amount), userid=userid, from_token=shops, hash=hash)
  303. table4.insert(trans_data)
  304. db.close()
  305. return "您已購買成功!"
  306. @app.post("/event")
  307. async def nftdrops(userModel : models.NftDrops):
  308. # db connect
  309. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  310. table1 = db['nft_drop_list']
  311. # input
  312. userid = userModel.userid
  313. email = userModel.email
  314. the_current_event = eventPeriod()
  315. if the_current_event == 0:
  316. return "目前沒有任何進行中的活動"
  317. else:
  318. # 再針對有上架的 event 填寫客戶到 nftdroplist
  319. now = dt.datetime.now()
  320. result1 = table1.find_one(userid=userid)
  321. if not result1:
  322. for key, item in the_current_event.items():
  323. table1.insert(dict(eventid=item, userid=userid, email=email, time=now))
  324. db.close()
  325. return "完成加入"
  326. else:
  327. db.close()
  328. return "該資料已存在"
  329. @app.post("/eventPeriod")
  330. def eventPeriod():
  331. # db connect
  332. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  333. # 設定event始終期間,與上架與否
  334. table3 = db['event']
  335. now = dt.datetime.now()
  336. # 先去找 event 表,確認在期間之內
  337. result = db.query("SELECT * FROM arkcard.event WHERE start < NOW() and NOW() < end and launched='yes';")
  338. rows = {}
  339. i = 0
  340. for row in result:
  341. rows[i] = row['eventid']
  342. i += 1
  343. return rows
  344. db.close()
  345. if __name__ == '__main__':
  346. uvicorn.run("main:app", host="0.0.0.0", port=8228, reload=True,ssl_context=('/etc/letsencrypt/live/ark.cards/fullchain.pem', '/etc/letsencrypt/live/ark.cards/privkey.pem'))