Tlapbot/main.py

47 lines
1.4 KiB
Python
Raw Normal View History

2022-06-08 14:22:58 +02:00
from flask import Flask,request,json,g
from tlapbot.db import get_db
2022-05-25 13:24:55 +02:00
import requests
2022-05-24 15:28:00 +02:00
app = Flask(__name__)
2022-05-25 13:24:55 +02:00
@app.route('/owncastWebhook',methods=['POST'])
def owncastWebhook():
data = request.json
2022-06-08 14:22:58 +02:00
db = get_db()
2022-05-25 13:24:55 +02:00
if data["type"] == "CHAT":
print("New chat message:")
print(f'from {data["eventData"]["user"]["displayName"]}:')
print(f'{data["eventData"]["body"]}')
2022-06-08 14:22:58 +02:00
elif data["type"] == "USER_JOINED":
user_id = data["eventData"]["user"]["id"]
try:
cursor = db.execute(
"SELECT username FROM points WHERE id = ?",
(user_id)
)
if cursor.fetchone() == None:
username = data["eventData"]["user"]["displayName"]
cursor.execute(
"INSERT INTO points(id, username, points) VALUES(?, ?, 0)",
(user_id, username)
)
db.commit()
elif data["type"] == "NAME_CHANGED":
user_id = data["eventData"]["user"]["id"]
new_username = data["eventData"]["user"]["newName"]
try:
db.execute(
"UPDATE points SET username = ? WHERE id = ?",
(new_username, user_id),
)
db.commit()
except db.IntegrityError:
print("Integrity Error.")
print(f"User ID {user_id} probably does not exist.")
2022-05-25 13:24:55 +02:00
return data
2022-05-24 15:28:00 +02:00
if __name__ == '__main__':
app.run(debug=True)