Dooratre commited on
Commit
227a198
·
verified ·
1 Parent(s): 3848078

Create admin_messages.py

Browse files
Files changed (1) hide show
  1. admin_messages.py +67 -0
admin_messages.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import json
3
+ from msg import fetch_json_from_github, update_user_json_file, fetch_authenticity_token_and_commit_oid
4
+
5
+ def extract_admin_message(ai_response):
6
+ """
7
+ Extract admin message from AI response and return both the admin message
8
+ and the cleaned response (without admin tags).
9
+ """
10
+ admin_pattern = r'<admin>(.*?)</admin>'
11
+ admin_messages = re.findall(admin_pattern, ai_response, re.DOTALL)
12
+
13
+ # Clean the response by removing admin tags and their content
14
+ cleaned_response = re.sub(admin_pattern, '', ai_response)
15
+
16
+ if admin_messages:
17
+ return admin_messages[0].strip(), cleaned_response.strip()
18
+ else:
19
+ return None, ai_response
20
+
21
+ def save_admin_message(page_id, message, user_profile_img):
22
+ """
23
+ Save admin message to the GitHub JSON file.
24
+ """
25
+ try:
26
+ # Fetch current JSON data
27
+ github_response = fetch_json_from_github()
28
+ if not github_response["success"]:
29
+ print(f"Error fetching JSON data: {github_response['message']}")
30
+ return False
31
+
32
+ # Get the current data
33
+ data = github_response["data"]
34
+
35
+ # Initialize the page_id entry if it doesn't exist
36
+ if page_id not in data:
37
+ data[page_id] = []
38
+
39
+ # Add the new message
40
+ new_message = {
41
+ "message": message,
42
+ "img_profile": user_profile_img
43
+ }
44
+
45
+ data[page_id].append(new_message)
46
+
47
+ # Convert the updated data to JSON string
48
+ new_content = json.dumps(data, indent=2)
49
+
50
+ # Get authentication tokens for GitHub
51
+ authenticity_token, commit_oid = fetch_authenticity_token_and_commit_oid()
52
+ if not authenticity_token or not commit_oid:
53
+ print("Failed to get GitHub authentication tokens")
54
+ return False
55
+
56
+ # Update the file on GitHub
57
+ update_result = update_user_json_file(authenticity_token, commit_oid, new_content)
58
+ if update_result["success"]:
59
+ print("Admin message saved successfully")
60
+ return True
61
+ else:
62
+ print(f"Error saving admin message: {update_result['message']}")
63
+ return False
64
+
65
+ except Exception as e:
66
+ print(f"Error saving admin message: {str(e)}")
67
+ return False