Tlapbot/main.py

67 lines
2.2 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.")
elif data["type"] == "CHAT":
user_id = data["eventData"]["user"]["id"]
if "!points" in data["body"]:
try:
cursor = db.execute(
"SELECT points FROM points WHERE id = ?",
(user_id),
)
print("{}'s points: {}".format(user_id, cursor.fetchone()))
2022-08-01 10:13:27 +02:00
else: # DEBUG: give points for message
try:
db.execute(
"UPDATE points SET points = points + 10 WHERE id = ?",
(user_id),
)
db.commit()
except sqlite3.Error as e:
print("Error occured:", e.args[0])
print("To user:", user_id)
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)