Tlapbot/tlapbot/owncast_webhooks.py

45 lines
1.7 KiB
Python
Raw Normal View History

2022-08-24 13:39:40 +02:00
from flask import Flask,request,json,Blueprint
from sqlite3 import Error
from tlapbot.db import get_db
2022-08-24 13:47:55 +02:00
from tlapbot.owncast_helpers import user_exists, add_user_to_database, send_chat
2022-08-24 13:45:13 +02:00
bp = Blueprint('owncast_webhooks', __name__)
@bp.route('/owncastWebhook',methods=['POST'])
2022-08-24 13:45:13 +02:00
def owncast_webhook():
data = request.json
db = get_db()
if data["type"] == "USER_JOINED":
user_id = data["eventData"]["user"]["id"]
2022-08-24 13:45:13 +02:00
add_user_to_database(user_id, db)
elif data["type"] == "CHAT":
display_name = data["eventData"]["user"]["displayName"]
print("New chat message:")
print(f'from {display_name}:')
print(f'{data["eventData"]["body"]}')
2022-08-24 13:39:40 +02:00
user_id = data["eventData"]["user"]["id"]
if "!points" in data["eventData"]["body"]:
2022-08-24 13:45:13 +02:00
if not user_exists(user_id, db):
add_user_to_database(user_id, db)
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
(user_id,)
)
2022-08-17 17:44:25 +02:00
message = "{}'s points: {}".format(display_name, cursor.fetchone()[0])
print(message)
2022-08-24 13:45:13 +02:00
send_chat(message)
except Error as e:
print("Error occured reading points:", e.args[0])
print("To user:", user_id)
else: # DEBUG: give points for message
try:
db.execute(
"UPDATE points SET points = points + 10 WHERE id = ?",
(user_id,)
)
db.commit()
except Error as e:
print("Error occured giving DEBUG points:", e.args[0])
print("To user:", user_id)
2022-08-17 17:44:25 +02:00
return data