Spaces:
Running
Running
andykr1k commited on
Commit ·
0980190
1
Parent(s): 88e66b3
Added support and reported content handling
Browse files- main.py +52 -1
- requirements.txt +2 -1
main.py
CHANGED
|
@@ -11,6 +11,7 @@ from fastapi import FastAPI
|
|
| 11 |
import uvicorn
|
| 12 |
import threading
|
| 13 |
import asyncio
|
|
|
|
| 14 |
|
| 15 |
app = FastAPI()
|
| 16 |
|
|
@@ -19,6 +20,7 @@ load_dotenv()
|
|
| 19 |
SUPABASE_ID = os.getenv('supabaseID')
|
| 20 |
SUPABASE_URL = os.getenv('supabaseUrl')
|
| 21 |
SUPABASE_KEY = os.getenv('supabaseAnonKey')
|
|
|
|
| 22 |
|
| 23 |
# Supabase client creation for each function
|
| 24 |
def get_client():
|
|
@@ -92,10 +94,51 @@ def send_notification(title, body, token, id, post_id, notif_type):
|
|
| 92 |
except requests.exceptions.RequestException as e:
|
| 93 |
print(f'Failed to send notification: {e}')
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
def handle_tags_update(event_payload):
|
| 96 |
print('Received update in post tags:')
|
| 97 |
print(event_payload)
|
| 98 |
-
|
| 99 |
record = event_payload['record']
|
| 100 |
tags_str = record.get('tags', '[]')
|
| 101 |
try:
|
|
@@ -353,6 +396,14 @@ def setup_likes_subscription(SUPABASE_ID, SUPABASE_KEY):
|
|
| 353 |
channel_posts_for_tags.join().on(
|
| 354 |
"INSERT", lambda payload: executor.submit(handle_tags_update, payload))
|
| 355 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 356 |
s.listen()
|
| 357 |
|
| 358 |
except Exception as e:
|
|
|
|
| 11 |
import uvicorn
|
| 12 |
import threading
|
| 13 |
import asyncio
|
| 14 |
+
from slack_sdk import WebClient
|
| 15 |
|
| 16 |
app = FastAPI()
|
| 17 |
|
|
|
|
| 20 |
SUPABASE_ID = os.getenv('supabaseID')
|
| 21 |
SUPABASE_URL = os.getenv('supabaseUrl')
|
| 22 |
SUPABASE_KEY = os.getenv('supabaseAnonKey')
|
| 23 |
+
slackclient = WebClient(token=os.getenv('slack_bot_oauth'))
|
| 24 |
|
| 25 |
# Supabase client creation for each function
|
| 26 |
def get_client():
|
|
|
|
| 94 |
except requests.exceptions.RequestException as e:
|
| 95 |
print(f'Failed to send notification: {e}')
|
| 96 |
|
| 97 |
+
def send_slack_message(event_type: str, category: str, username: str, post_id: str = None, list_id: str = None):
|
| 98 |
+
try:
|
| 99 |
+
print("reach 1")
|
| 100 |
+
|
| 101 |
+
if event_type == "report":
|
| 102 |
+
target = f"post {post_id}" if post_id is not None else f"collection {list_id}"
|
| 103 |
+
message = f":rotating_light: A report has been submitted by *{username}* for {target}."
|
| 104 |
+
else:
|
| 105 |
+
message = f":information_source: A support ticket has been submitted by *{username}*."
|
| 106 |
+
|
| 107 |
+
print("reach 2")
|
| 108 |
+
|
| 109 |
+
response = slackclient.chat_postMessage(
|
| 110 |
+
channel="C092DLFAFSR",
|
| 111 |
+
text=message,
|
| 112 |
+
username="Support Bot",
|
| 113 |
+
icon_emoji=":robot_face:"
|
| 114 |
+
)
|
| 115 |
+
print("reach 3")
|
| 116 |
+
|
| 117 |
+
if not response["ok"]:
|
| 118 |
+
print(f"Slack API error: {response}")
|
| 119 |
+
return response
|
| 120 |
+
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print("Failed to send Slack message")
|
| 123 |
+
return None
|
| 124 |
+
|
| 125 |
+
def handle_support(event_payload: dict):
|
| 126 |
+
print("Received update in Support or reportedContent:")
|
| 127 |
+
print(event_payload)
|
| 128 |
+
|
| 129 |
+
record = event_payload.get("record", {})
|
| 130 |
+
username = getUsername(record.get("user_id")).lower()
|
| 131 |
+
post_id = record.get("post_id")
|
| 132 |
+
list_id = record.get("list_id")
|
| 133 |
+
category = record.get("category", "uncategorized")
|
| 134 |
+
|
| 135 |
+
event_type = "report" if post_id is not None or list_id is not None else "support"
|
| 136 |
+
send_slack_message(event_type, category, username, post_id, list_id)
|
| 137 |
+
|
| 138 |
def handle_tags_update(event_payload):
|
| 139 |
print('Received update in post tags:')
|
| 140 |
print(event_payload)
|
| 141 |
+
|
| 142 |
record = event_payload['record']
|
| 143 |
tags_str = record.get('tags', '[]')
|
| 144 |
try:
|
|
|
|
| 396 |
channel_posts_for_tags.join().on(
|
| 397 |
"INSERT", lambda payload: executor.submit(handle_tags_update, payload))
|
| 398 |
|
| 399 |
+
channel_support = s.set_channel("realtime:public:Support")
|
| 400 |
+
channel_support.join().on(
|
| 401 |
+
"INSERT", lambda payload: executor.submit(handle_support, payload))
|
| 402 |
+
|
| 403 |
+
channel_report = s.set_channel("realtime:public:reportedContent")
|
| 404 |
+
channel_report.join().on(
|
| 405 |
+
"INSERT", lambda payload: executor.submit(handle_support, payload))
|
| 406 |
+
|
| 407 |
s.listen()
|
| 408 |
|
| 409 |
except Exception as e:
|
requirements.txt
CHANGED
|
@@ -25,4 +25,5 @@ typing_extensions==4.12.2
|
|
| 25 |
websockets==11.0.3
|
| 26 |
requests
|
| 27 |
fastapi
|
| 28 |
-
uvicorn
|
|
|
|
|
|
| 25 |
websockets==11.0.3
|
| 26 |
requests
|
| 27 |
fastapi
|
| 28 |
+
uvicorn
|
| 29 |
+
slack_sdk
|