main.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. import uvicorn
  2. import fastapi
  3. from fastapi.middleware.cors import CORSMiddleware
  4. from linebot import LineBotApi, WebhookHandler
  5. from linebot.models import (
  6. MessageEvent, TextMessage, TextSendMessage, FollowEvent, TemplateSendMessage, ButtonsTemplate, URITemplateAction,
  7. )
  8. import dataset
  9. import requests
  10. import json
  11. app = fastapi.FastAPI()
  12. app.add_middleware(
  13. CORSMiddleware,
  14. allow_origins=['*'],
  15. allow_credentials=True,
  16. allow_methods=["*"],
  17. allow_headers=["*"],
  18. )
  19. # bot config
  20. line_bot_api = LineBotApi("SJT7VPT4RMQFLcS27jQBy3FcC24gtDrkcwJWZ5Xzqesr5T78LOKudHEJzt0k3b2S7n4KPwf27J7DVz2c8NQ4plSaaQylEeB1cYrfejaE/RPG/lCIQBYe4iBTzo26s4i2PcmT89837per/lTyvhVIKAdB04t89/1O/w1cDnyilFU=")
  21. handler = WebhookHandler("411ae3ef7e766739ed2c2c27b249d010")
  22. # db connect
  23. db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
  24. table = db['users']
  25. # callback event
  26. @app.post("/callback")
  27. async def callback(request: fastapi.Request):
  28. signature = request.headers['X-Line-Signature']
  29. body = await request.body()
  30. handler.handle(body.decode('utf-8'), signature)
  31. return 'OK'
  32. # nft collection api
  33. @app.get("/collection/{userid}")
  34. def collection(userid):
  35. xname = userid
  36. sql = 'SELECT a.* FROM nft a INNER JOIN ( SELECT userid FROM users WHERE userid="'+xname+'" ) b ON b.userid = a.userid'
  37. result = db.query(sql)
  38. rows={}
  39. i = 0
  40. for row in result:
  41. rows[i]=row
  42. i+=1
  43. return rows
  44. # follow event
  45. @handler.add(FollowEvent)
  46. def handle_follow(event):
  47. # get user id when follow
  48. real_user_id = event.source.user_id
  49. rand_num = '_test_000_000_002'
  50. user_id = event.source.user_id+rand_num
  51. # create user account
  52. url = 'https://nft-api-staging.joyso.io/api/v1/accounts'
  53. headers = {'Authorization': 'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'}
  54. # setup for temp use (unique id)
  55. data = 'uid='+user_id
  56. r = requests.post(url=url, headers=headers, data=data)
  57. # extract the account address
  58. dict_str = json.loads(r.text)
  59. user_account = dict_str['account']
  60. user_address = user_account['address']
  61. # write in sql
  62. data = dict(userid=real_user_id, useraddress=user_address)
  63. table.insert(data)
  64. print(" Follow event: "+event.source.user_id)
  65. line_bot_api.reply_message(
  66. event.reply_token,
  67. TextSendMessage(text='歡迎加入好友'))
  68. # message handler
  69. @handler.add(MessageEvent, message=TextMessage)
  70. def message(event):
  71. if '我要發送' in event.message.text:
  72. button_template_message = ButtonsTemplate(
  73. title=' ',
  74. text='點擊並打開發送頁面,便可以選擇要發送的NFT給對方!',
  75. actions=[
  76. URITemplateAction(
  77. label='打開發送頁面',
  78. uri='http://q.ptt.cx/a1/index-line.html?userid=' + event.source.user_id),])
  79. line_bot_api.reply_message(
  80. event.reply_token,
  81. TemplateSendMessage(
  82. alt_text="Receive",
  83. template=button_template_message))
  84. elif '我要接收' in event.message.text:
  85. user_id = event.source.user_id
  86. print(" message event: "+user_id)
  87. button_template_message = ButtonsTemplate(
  88. title=' ',
  89. text='點擊並打開接收頁面,即可分享接收地址給對方!',
  90. actions=[
  91. URITemplateAction(
  92. label='打開接收頁面',
  93. uri='http://q.ptt.cx/a1/index-line.html?userid=' + event.source.user_id),])
  94. line_bot_api.reply_message(
  95. event.reply_token,
  96. TemplateSendMessage(
  97. alt_text="Receive",
  98. template=button_template_message))
  99. else:
  100. line_bot_api.reply_message(
  101. event.reply_token,
  102. TextSendMessage(text="更多的服務內容,歡迎請上我們的官網!(http://ark.cards)"))
  103. if __name__ == '__main__':
  104. uvicorn.run("main:app", host="0.0.0.0", port=8228, reload=True)