|
@@ -5,6 +5,7 @@ from linebot import LineBotApi, WebhookHandler
|
|
from linebot.models import (
|
|
from linebot.models import (
|
|
MessageEvent, TextMessage, TextSendMessage, FollowEvent,
|
|
MessageEvent, TextMessage, TextSendMessage, FollowEvent,
|
|
)
|
|
)
|
|
|
|
+import mysql.connector
|
|
|
|
|
|
app = fastapi.FastAPI()
|
|
app = fastapi.FastAPI()
|
|
|
|
|
|
@@ -24,11 +25,53 @@ async def callback(request: fastapi.Request):
|
|
# follow event
|
|
# follow event
|
|
@handler.add(FollowEvent)
|
|
@handler.add(FollowEvent)
|
|
def handle_follow(event):
|
|
def handle_follow(event):
|
|
|
|
+ # get user id when follow
|
|
|
|
+ user_id = event.source.user_id
|
|
|
|
+
|
|
|
|
+ # db connect
|
|
|
|
+ conn = mysql.connector.connect(
|
|
|
|
+ host="127.0.0.1",
|
|
|
|
+ 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)'
|
|
|
|
+
|
|
|
|
+ cursor.execute(search_id, (user_id,))
|
|
|
|
+ values = cursor.fetchall()
|
|
|
|
+ value = [user_id]
|
|
|
|
+ for v in values:
|
|
|
|
+ print("uaserid found")
|
|
|
|
+ else:
|
|
|
|
+ cursor.execute(add_id, value)
|
|
|
|
+ conn.commit()
|
|
|
|
+ conn.close()
|
|
|
|
+ print("userid add")
|
|
|
|
+
|
|
print(event.source.user_id)
|
|
print(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
|
|
|
|
+@handler.add(MessageEvent, message=TextMessage)
|
|
|
|
+def echo(event):
|
|
|
|
+ if '我要發送' in event.message.text:
|
|
|
|
+ line_bot_api.reply_message(
|
|
|
|
+ event.reply_token,
|
|
|
|
+ TextSendMessage(text="發送操作"))
|
|
|
|
+ elif '我要接收' in event.message.text:
|
|
|
|
+ line_bot_api.reply_message(
|
|
|
|
+ event.reply_token,
|
|
|
|
+ TextSendMessage(text=('接收操作')))
|
|
|
|
+ else:
|
|
|
|
+ line_bot_api.reply_message(
|
|
|
|
+ event.reply_token,
|
|
|
|
+ TextSendMessage(text="更多的服務內容,歡迎請上我們的官網!")
|
|
|
|
+ )
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
if __name__ == '__main__':
|
|
- uvicorn.run("main:app", host="127.0.0.1", port=5000, reload=True)
|
|
|
|
|
|
+ uvicorn.run("main:app", host="0.0.0.0", port=8080, reload=True)
|