Skip to content

Instantly share code, notes, and snippets.

@Mte90

Mte90/staged-users.py

Last active Apr 16, 2021
Embed
What would you like to do?
discourse-email-staged-users.py
#!/usr/bin/env python
# Requirements: API key from discourse with Global permission and webhook configured
# Dependencies: pip install flask pydiscourse yaml
# If the webhook is called and the author's post is a staged user send an email with the link of the thread with an invite to register
from flask import Flask, request, Response
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
import json
import pydiscourse
import requests
import smtplib
import yaml
app = Flask(__name__)
@app.route('/webhook', methods=['POST'])
def respond():
data = request.json
if data is not None and data.has_key('post'):
username = 'your-user'
key = 'your-key'
domain = 'https://your-domain/'
client = pydiscourse.DiscourseClient(
(domain),
api_username=(username),
api_key=(key),
)
user_info = client.user(data['post']['username'])
if user_info['staged'] == True:
port = 587
with open(r'/var/discourse/containers/app.yml') as file:
discourse_settings = yaml.load(file)
smtp_server = discourse_settings['env']['DISCOURSE_SMTP_ADDRESS']
sender_email = discourse_settings['env']['DISCOURSE_SMTP_USER_NAME']
password = discourse_settings['env']['DISCOURSE_SMTP_PASSWORD']
message = 'Your thread <a href="' + domain + 't/' + data['post']['topic_slug'] + '/' + str(data['post']['topic_id']) + '">' + data['post']['topic_title'] + '</a> was created in our forum.<br> To join register an account!<br>Your lovely community'
server = smtplib.SMTP(smtp_server, port)
server.starttls()
msg = MIMEText(message, 'html')
msg["Subject"] = "Thanks for your message"
msg["From"] = sender_email
msg["To"] = user_info['email']
try:
server.login(msg["From"], password)
print(msg)
server.sendmail(sender_email, msg['To'], msg.as_string())
finally:
server.quit()
return Response(status=200)
return Response(status=200)
app.run(host='0.0.0.0', port=13336)
@FranzMari

This comment has been minimized.

Copy link

@FranzMari FranzMari commented Apr 16, 2021

I guess GThanks should be Thanks ;-)

@Mte90

This comment has been minimized.

Copy link
Owner Author

@Mte90 Mte90 commented Apr 16, 2021

I guess GThanks should be Thanks ;-)

Ops on cleaning up the data and text of our forum I lost some "bytes" 😂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment