|
@@ -1,11 +1,12 @@
|
|
|
-
|
|
|
import uvicorn
|
|
|
import fastapi
|
|
|
from linebot import LineBotApi, WebhookHandler
|
|
|
from linebot.models import (
|
|
|
- MessageEvent, TextMessage, TextSendMessage, FollowEvent,
|
|
|
+ MessageEvent, TextMessage, TextSendMessage, FollowEvent, TemplateSendMessage, ButtonsTemplate, URITemplateAction,
|
|
|
)
|
|
|
-import mysql.connector
|
|
|
+import dataset
|
|
|
+import requests
|
|
|
+import json
|
|
|
|
|
|
app = fastapi.FastAPI()
|
|
|
|
|
@@ -13,6 +14,9 @@ app = fastapi.FastAPI()
|
|
|
line_bot_api = LineBotApi("SJT7VPT4RMQFLcS27jQBy3FcC24gtDrkcwJWZ5Xzqesr5T78LOKudHEJzt0k3b2S7n4KPwf27J7DVz2c8NQ4plSaaQylEeB1cYrfejaE/RPG/lCIQBYe4iBTzo26s4i2PcmT89837per/lTyvhVIKAdB04t89/1O/w1cDnyilFU=")
|
|
|
handler = WebhookHandler("411ae3ef7e766739ed2c2c27b249d010")
|
|
|
|
|
|
+# db connect
|
|
|
+db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/arkcard?charset=utf8mb4')
|
|
|
+table = db['users']
|
|
|
|
|
|
# callback event
|
|
|
@app.post("/callback")
|
|
@@ -26,55 +30,62 @@ async def callback(request: fastapi.Request):
|
|
|
@handler.add(FollowEvent)
|
|
|
def handle_follow(event):
|
|
|
# get user id when follow
|
|
|
- user_id = event.source.user_id
|
|
|
-
|
|
|
- # db connect
|
|
|
- conn = mysql.connector.connect(
|
|
|
- host="db.ptt.cx",
|
|
|
- port=3306,
|
|
|
- user="choozmo",
|
|
|
- password="pAssw0rd",
|
|
|
- database='arkcard')
|
|
|
- cursor = conn.cursor()
|
|
|
- search_id = """SELECT * FROM users WHERE id = %s"""
|
|
|
- add_id = 'INSERT INTO users (id) VALUES (%s)'
|
|
|
+ rand_num = '_test_000_000_000'
|
|
|
+ user_id = event.source.user_id+rand_num
|
|
|
|
|
|
- cursor.execute(search_id, (user_id,))
|
|
|
- values = cursor.fetchall()
|
|
|
- value = [user_id]
|
|
|
- print(values)
|
|
|
-
|
|
|
- for v in values:
|
|
|
- print("uaserid found")
|
|
|
- else:
|
|
|
- cursor.execute(add_id, value)
|
|
|
- conn.commit()
|
|
|
- conn.close()
|
|
|
- print("userid add")
|
|
|
-
|
|
|
- print("歡迎")
|
|
|
+ # create user account
|
|
|
+ url = 'https://nft-api-staging.joyso.io/api/v1/accounts'
|
|
|
+ headers = {'Authorization': 'Basic bmZ0OmMxOTEzOWMzYjM3YjdjZWU3ZmY3OTFiZGU3NzdjZWNl'}
|
|
|
+ # setup for temp use (unique 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']
|
|
|
+ # write in sql
|
|
|
+ data = dict(userid=user_id, useraddress=user_address)
|
|
|
+ table.insert(data)
|
|
|
print(event.source.user_id)
|
|
|
line_bot_api.reply_message(
|
|
|
event.reply_token,
|
|
|
TextSendMessage(text='歡迎加入好友'))
|
|
|
|
|
|
-# message handler
|
|
|
+# message handler
|
|
|
@handler.add(MessageEvent, message=TextMessage)
|
|
|
-def echo(event):
|
|
|
+def message(event):
|
|
|
if '我要發送' in event.message.text:
|
|
|
+ button_template_message = ButtonsTemplate(
|
|
|
+ title=' ',
|
|
|
+ text='點擊並打開發送頁面,便可以選擇要發送的NFT給對方!',
|
|
|
+ actions=[
|
|
|
+ URITemplateAction(
|
|
|
+ label='打開發送頁面',
|
|
|
+ uri='http://q.ptt.cx/a1/index-line.html?userid=' + event.source.user_id),])
|
|
|
line_bot_api.reply_message(
|
|
|
event.reply_token,
|
|
|
- TextSendMessage(text="發送操作"))
|
|
|
+ TemplateSendMessage(
|
|
|
+ alt_text="Receive",
|
|
|
+ template=button_template_message))
|
|
|
+
|
|
|
elif '我要接收' in event.message.text:
|
|
|
+ button_template_message = ButtonsTemplate(
|
|
|
+ title=' ',
|
|
|
+ text='點擊並打開接收頁面,即可分享接收地址給對方!',
|
|
|
+ actions=[
|
|
|
+ URITemplateAction(
|
|
|
+ label='打開接收頁面',
|
|
|
+ uri='http://q.ptt.cx/a1/index-line.html?userid=' + event.source.user_id),])
|
|
|
line_bot_api.reply_message(
|
|
|
event.reply_token,
|
|
|
- TextSendMessage(text=('接收操作')))
|
|
|
+ TemplateSendMessage(
|
|
|
+ alt_text="Receive",
|
|
|
+ template=button_template_message))
|
|
|
+
|
|
|
else:
|
|
|
line_bot_api.reply_message(
|
|
|
event.reply_token,
|
|
|
- TextSendMessage(text="更多的服務內容,歡迎請上我們的官網!")
|
|
|
- )
|
|
|
-
|
|
|
+ TextSendMessage(text="更多的服務內容,歡迎請上我們的官網!(http://ark.cards)"))
|
|
|
|
|
|
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=5000, reload=True)
|