|
|
@@ -12,63 +12,16 @@ import random
|
|
|
ocb = Blueprint('webhooks', __name__)
|
|
|
|
|
|
|
|
|
+followers = []
|
|
|
+donations = []
|
|
|
+subscribers = []
|
|
|
+
|
|
|
+
|
|
|
def format(rawjson): # Make data legible
|
|
|
formatted_data = json.dumps(rawjson, indent=4)
|
|
|
return formatted_data
|
|
|
|
|
|
|
|
|
-@ocb.route('/kofiHook', methods=["POST"])
|
|
|
-def kofiHook():
|
|
|
- current_app.logger.info(f'----------------------------------------------------------------------------')
|
|
|
- current_app.logger.info(f'Kofi request')
|
|
|
- if request.content_type == 'application/x-www-form-urlencoded':
|
|
|
- raw_data = request.form.get('data') # Get the kofi data
|
|
|
- if raw_data:
|
|
|
- raw_data = json.loads(raw_data)
|
|
|
- is_authed = raw_data['verification_token']
|
|
|
- if is_authed == current_app.config['KOFI_TOKEN']:
|
|
|
- type = raw_data['type']
|
|
|
- is_public = raw_data['is_public']
|
|
|
- new_sub = raw_data['is_first_subscription_payment']
|
|
|
- message = raw_data['message']
|
|
|
- shop_items = raw_data['shop_items']
|
|
|
- from_name = raw_data['from_name']
|
|
|
- email = raw_data['email']
|
|
|
- amount = raw_data['amount']
|
|
|
- sub_payment = raw_data['is_subscription_payment']
|
|
|
- first_sub = raw_data['is_first_subscription_payment']
|
|
|
- tier_name = raw_data['tier_name']
|
|
|
- if type == 'Shop Order':
|
|
|
- current_app.logger.info(f'{from_name} purchased {format(shop_items)}\nMessage: {message}\n')
|
|
|
- if type == 'Donation':
|
|
|
- donation_info = [is_public, from_name, email, amount, message]
|
|
|
- donation_points = current_app.config['KOFI_SETTINGS']['donation_points']
|
|
|
- accept_donation(donation_info, donation_points)
|
|
|
- if type == 'Subscription':
|
|
|
- if current_app.config['KOFI_SETTINGS']['subs']: # Check that subscriptions are enabled
|
|
|
- if first_sub:
|
|
|
- if tier_name:
|
|
|
- current_app.logger.info(f'{from_name} <{email}> subscribed as a {tier_name} tier member.')
|
|
|
- else:
|
|
|
- current_app.logger.info(f'{from_name} <{email}> subscribed.')
|
|
|
- else:
|
|
|
- if tier_name:
|
|
|
- current_app.logger.info(f'{from_name} <{email}> renewed their {tier_name} tier membership.')
|
|
|
- else:
|
|
|
- current_app.logger.info(f'{from_name} <{email}> renewed their membership.')
|
|
|
-
|
|
|
- sub_info = [is_public, from_name, email, amount, message, first_sub, tier_name]
|
|
|
- sub_points = current_app.config['KOFI_SETTINGS']['sub_points']
|
|
|
- accept_sub(sub_info, sub_points)
|
|
|
- else:
|
|
|
- current_app.logger.info(f'Kofi membership received, but subscriptions are not enabled. Doing nothing.')
|
|
|
-
|
|
|
- return jsonify({'status': 'success'}), 200
|
|
|
- else:
|
|
|
- current_app.logger.info(f'Token invalid. Rejecting.')
|
|
|
- return jsonify({'status': 'unauthorized'}), 401
|
|
|
-
|
|
|
-
|
|
|
@ocb.route('/chatHook', methods=['POST'])
|
|
|
def chat_hook():
|
|
|
prefix = current_app.config['PREFIX']
|
|
|
@@ -141,16 +94,107 @@ def chat_hook():
|
|
|
|
|
|
elif lowercase_msg.startswith(f'{prefix}'): # Send to handle rewards
|
|
|
do_reward(lowercase_msg, user_id)
|
|
|
-
|
|
|
return data
|
|
|
|
|
|
|
|
|
-@ocb.route('/newFollow', methods=['POST']) # Nothing implemented here, YET.
|
|
|
-def new_follow():
|
|
|
+@ocb.route('/followHook', methods=['POST']) # Called by Owncast when someone follows
|
|
|
+def follow_hook():
|
|
|
data = request.json
|
|
|
- current_app.logger.info(f'\n\n{format(data)}\n\n')
|
|
|
- db = get_db()
|
|
|
- user_id = data['eventData']['id']
|
|
|
- display_name = data['eventData']['name']
|
|
|
-
|
|
|
+ current_app.logger.debug(f'\n\n_______________\n/newFollow triggered!\n_______________')
|
|
|
+ followers.append(data)
|
|
|
return jsonify({'status': 'success'}), 200
|
|
|
+
|
|
|
+
|
|
|
+@ocb.route('/kofiHook', methods=["POST"])
|
|
|
+def kofi_hook():
|
|
|
+ current_app.logger.info(f'----------------------------------------------------------------------------')
|
|
|
+ current_app.logger.info(f'Kofi request')
|
|
|
+ if request.content_type == 'application/x-www-form-urlencoded':
|
|
|
+ raw_data = request.form.get('data') # Get the kofi data
|
|
|
+ if raw_data:
|
|
|
+ raw_data = json.loads(raw_data)
|
|
|
+ is_authed = raw_data['verification_token']
|
|
|
+ if is_authed == current_app.config['KOFI_TOKEN']:
|
|
|
+ type = raw_data['type']
|
|
|
+ is_public = raw_data['is_public']
|
|
|
+ new_sub = raw_data['is_first_subscription_payment']
|
|
|
+ message = raw_data['message']
|
|
|
+ shop_items = raw_data['shop_items']
|
|
|
+ from_name = raw_data['from_name']
|
|
|
+ email = raw_data['email']
|
|
|
+ amount = raw_data['amount']
|
|
|
+ sub_payment = raw_data['is_subscription_payment']
|
|
|
+ first_sub = raw_data['is_first_subscription_payment']
|
|
|
+ tier_name = raw_data['tier_name']
|
|
|
+ if type == 'Shop Order':
|
|
|
+ current_app.logger.info(f'{from_name} purchased {format(shop_items)}\nMessage: {message}\n')
|
|
|
+ if type == 'Donation':
|
|
|
+ donation_info = [is_public, from_name, email, amount, message]
|
|
|
+ donation_points = current_app.config['KOFI_SETTINGS']['donation_points']
|
|
|
+ accept_donation(donation_info, donation_points)
|
|
|
+ if is_public:
|
|
|
+ alert_info = {'name': from_name, 'amount': amount}
|
|
|
+ else:
|
|
|
+ alert_info = {'name': 'Anonymous Hero', 'amount': amount}
|
|
|
+ donations.append(alert_info) # Append info to be displayed in alert
|
|
|
+ if type == 'Subscription':
|
|
|
+ if current_app.config['KOFI_SETTINGS']['subs']: # Check that subscriptions are enabled
|
|
|
+ if first_sub:
|
|
|
+ if tier_name:
|
|
|
+ current_app.logger.info(f'{from_name} <{email}> subscribed as a {tier_name} tier member.')
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'{from_name} <{email}> subscribed.')
|
|
|
+ else:
|
|
|
+ if tier_name:
|
|
|
+ current_app.logger.info(f'{from_name} <{email}> renewed their {tier_name} tier membership.')
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'{from_name} <{email}> renewed their membership.')
|
|
|
+
|
|
|
+ sub_info = [is_public, from_name, email, amount, message, first_sub, tier_name]
|
|
|
+ sub_points = current_app.config['KOFI_SETTINGS']['sub_points']
|
|
|
+ accept_sub(sub_info, sub_points)
|
|
|
+ if is_public:
|
|
|
+ alert_info = {'name': from_name, 'tiername': tier_name}
|
|
|
+ else:
|
|
|
+ alert_info = {'name': 'Anonymous Hero', 'teirname': tier_name}
|
|
|
+ subscribers.append(alert_info) # Append info to be displayed in alert
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'Kofi membership received, but subscriptions are not enabled. Doing nothing.')
|
|
|
+
|
|
|
+ return jsonify({'status': 'success'}), 200
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'Token invalid. Rejecting.')
|
|
|
+ return jsonify({'status': 'unauthorized'}), 401
|
|
|
+
|
|
|
+
|
|
|
+@ocb.route('/checkFollows') # Polled by follower.html template to check for new followers
|
|
|
+def check_follows():
|
|
|
+ if followers:
|
|
|
+ current_app.logger.debug(f'\n\n{format(followers[0])}\n\n')
|
|
|
+ last_follower = followers.pop(0)
|
|
|
+ return jsonify(last_follower)
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'No new followers')
|
|
|
+ return jsonify(None)
|
|
|
+
|
|
|
+
|
|
|
+@ocb.route('/checkDonations') # Polled by follower.html template to check for new followers
|
|
|
+def check_donations():
|
|
|
+ if donations:
|
|
|
+ current_app.logger.info(f'\n\n{format(donations[0])}\n\n')
|
|
|
+ last_donation = donations.pop(0)
|
|
|
+ return jsonify(last_donation)
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'No new donations')
|
|
|
+ return jsonify(None)
|
|
|
+
|
|
|
+
|
|
|
+@ocb.route('/checkSubscribers') # Polled by subscriber.html template to check for new followers
|
|
|
+def check_subscribers():
|
|
|
+ if subscribers:
|
|
|
+ current_app.logger.info(f'\n\n{format(subscribers[0])}\n\n')
|
|
|
+ last_subscriber = subscribers.pop(0)
|
|
|
+ return jsonify(last_subscriber)
|
|
|
+ else:
|
|
|
+ current_app.logger.info(f'No new subscribers')
|
|
|
+ return jsonify(None)
|