- Status: Pending
My "topic title" game is strong, I know... Leaving you all there with just a WHAT, and you're thinking I just may have forgot to type out the rest of it? Come on peeps, you should know me by now.
Ok... onward, So I have this really cool Python Script (and so help me god, if someone says why didn't you do it this way... and they show me a place in the Admin CP... I'll be like that 80's mom in Kmart with the kids!).
OMG!!! I know I am boring myself at this point... But you guys are my therapist, so I have to get it all of that brain stuff out there. Ok I am done... So I created a little simple python script:
import os
import sys
import json
from flask import Flask, request, jsonify
import requests
import logging
# Dynamically set the working directory to the script's directory
BASE_DIR = os.path.dirname(os.path.abspath(sys.argv[0]))
# Load Configuration
CONFIG_PATH = os.path.join(BASE_DIR, 'config', 'config.json')
try:
with open(CONFIG_PATH, 'r') as config_file:
config = json.load(config_file)
except FileNotFoundError:
print(f" Configuration file not found at {CONFIG_PATH}. Please ensure 'config.json' exists in the 'config' folder.")
exit(1)
except json.JSONDecodeError:
print(" Failed to parse 'config.json'. Please check the file for JSON formatting errors.")
exit(1)
# Extract Configurations
SETTINGS = config.get('settings', {})
IPS_API_KEY = SETTINGS.get('ips_api_key')
IPS_API_URL = f"{SETTINGS.get('ips_api_url', 'https://codenamejessica.com/api/core/messages')}?key={IPS_API_KEY}"
ADMIN_ID = SETTINGS.get('author_id')
ADMIN_NAME = SETTINGS.get('author_name')
LOG_FILE = os.path.join(BASE_DIR, SETTINGS.get('log_file', 'logs/send_welcome.log'))
DEBUG = SETTINGS.get('debug', False)
# Ensure log directory exists
os.makedirs(os.path.dirname(LOG_FILE), exist_ok=True)
# Logging setup
logging.basicConfig(filename=LOG_FILE, level=logging.DEBUG if DEBUG else logging.INFO, format="%(asctime)s - %(message)s")
app = Flask(__name__)
# Function to send a Private Message
def send_welcome_pm(user_id, username):
payload = {
"title": f" Welcome to CodenameJessica, {username}! ",
"to[]": str(user_id),
"body": (
f"We're thrilled to have you here, {username}! <br><br>"
"<strong style='color: hsl(0, 70%, 60%);'>Getting Started:</strong>"
"<ul style='list-style: none; padding-left: 0;'>"
" <li> <strong style='color: hsl(100, 70%, 50%);'>Introduce yourself</strong> in the [<a href='/forums/forum/27-meet-and-greet/'>Meet and Greet</a>].</li>"
" <li> <strong style='color: hsl(30, 70%, 60%);'>Join the conversation</strong> in [<a href='/forums/forum/25-the-lounge/'>The Lounge</a>]. Answer fun question starters or create your own!</li>"
" <li> <strong style='color: hsl(210, 90%, 60%);'>Test your skills</strong> with our [<a href='/forums/forum/9-programming-challenges/'>Programming Challenges</a>].</li>"
" <li> <strong style='color: white;'>Share your Feedback or Suggestions</strong> in the [<a href='/forums/forum/63-feedback-suggestions/'>Feedback & Suggestion Forum</a>].</li>"
" <li> <strong style='color: hsl(260, 90%, 70%);'>Explore & join</strong> clubs that match your interests! </li>"
"</ul><br>"
" <strong style='color: hsl(30, 70%, 60%);'>Get Involved:</strong>"
"<ul style='list-style: none; padding-left: 0;'>"
" <li> <strong style='color: hsl(100, 70%, 50%);'>Get or share</strong> [<a href='/forums/forum/3-programming/'>Programming Help</a>]</li>"
" <li> <strong style='color: hsl(30, 70%, 60%);'>Discover and share</strong> [<a href='/forums/forum/10-server-administration/'>Server Administration Tips</a>]</li>"
" <li> <strong style='color: hsl(0, 70%, 60%);'>Stay updated</strong> with the latest [<a href='/forums/forum/17-security/'>Best Security Practices</a>]</li>"
" <li> <strong style='color: hsl(210, 90%, 60%);'>Advance your career</strong> with [<a href='/forums/forum/74-professional-development/'>Professional Development</a>] resources</li>"
"</ul>"
"Or simply connect with a community of passionate, driven individuals, we've got you covered!<br><br>"
"At <strong>CodenameJessica</strong>, we celebrate diversity and foster meaningful connections within the Tech and Enterprise communities. "
"If you ever need help , feel free to tag me or any of our moderators. "
"We can't wait to see the amazing things you'll bring to our community!<br><br>"
"- <strong style='color: hsl(260, 90%, 70%);'>Jessica</strong>, <span data-i-color='soft'>Founder of CodenameJessica</span>"
),
"from": ADMIN_ID,
"author_name": ADMIN_NAME
}
logging.info(f" Sending payload to API: {payload}")
try:
response = requests.post(IPS_API_URL, data=payload)
if response.status_code in [200, 201]:
logging.info(f" Private message sent to {username} (User ID: {user_id})")
else:
logging.error(f" Failed to send PM: {response.status_code} - {response.text}")
except Exception as e:
logging.error(f" Exception while sending PM: {e}")
# Webhook listener for new user registrations
@app.route("/welcome", methods=["POST"])
def register_webhook():
try:
data = request.get_json()
logging.info(f" Received webhook data: {data}")
user_id = data.get("id") # Invision's User ID
username = data.get("name", "")
if user_id and username:
send_welcome_pm(user_id, username)
return jsonify({"message": " Welcome PM sent"}), 200
return jsonify({"error": " Invalid request, missing user_id or username"}), 400
except Exception as e:
logging.error(f" Webhook processing error: {e}")
return jsonify({"error": " Internal server error"}), 500
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
It is a very simple script to listen on port 5000 for a webhook from IPS, IPC, Invision (What the hell is it really called, I've seen like 5 names), start the action and poof... The new user has a Welcome Private Message... Nice pretty, formatted, perfect. Just like... (you thought I was going to say, me didn't you, nope... I was going to say YOU - awe, how gross)...
OK, so this all works perfectly, so where is the bug you're asking? When the new user logs in, there is no notification that they have a new message. The message is there, but no notification.
TADA! Told you we would get there... Now is this a bug or a feature?
DISCLAIMER!!!! This was freaking hilarious in my head with a Morgan Freeman dramatic role voice... "And that, dear reader, is where Jessica realized... the notification was never coming."
Recommended Comments