Commit 2ddff238 authored by Daniel W Bond's avatar Daniel W Bond
Browse files

posts message to Slack every 50 new users when API key is provided

parent c831dcd0
......@@ -9,6 +9,7 @@ from django.contrib import messages
import requests
# imports from your apps
from .models import Student, Major
from settings.slack import slack_post
def pfparse(pf_name_result):
......@@ -89,6 +90,15 @@ def create_user(tree):
if user_created:
print("Created user object %s." % username)
# notify developers of user signup milestones
num_users = User.objects.all().count()
if num_users % 50:
message = "Congratulations! We've passed %d user registrations!" % num_users
slack_post(text=message,
channel='#roomlist',
username='Roomlist User Milestones',
icon_emoji=':tada:')
# set and save the user's email
email_str = "%s@%s" % (username, settings.ORGANIZATION_EMAIL_DOMAIN)
user.email = email_str
......
# standard library imports
from __future__ import absolute_import, print_function
# standard library imports
import json
# third party imports
import requests
webhook_url = 'https://hooks.slack.com/services/xxxxxxxxx/xxxxxxxxx/xxxxxxxxxxxxxxxxxxxxxxxx'
def slack_post(text, channel, username, icon_emoji):
try:
payload = {'text': text, 'channel': channel,
'username': username, 'icon_emoji': icon_emoji, }
r = requests.post(webhook_url, data=json.dumps(payload))
r.raise_for_status()
print(r.status_code)
except request.exceptions.RequestException as e:
print("Error posting to Slack:", e)
# Examples
# slack_post(text="A terrible, awful thing happened on the server!",
# channel='#bot-test',
# username='Roomlist Server Errors',
# icon_emoji=':fire:')
# slack_post(text="Congratulations! You've passed 150 users registrations!",
# channel='#bot-test',
# username='Roomlist User Milestones',
# icon_emoji=':tada:')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment