Created
March 7, 2015 18:08
-
-
Save simonw/5e75c1a67e2a4fce735e to your computer and use it in GitHub Desktop.
Send new eventbrite orders to a Slack channel (Django view function)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponse | |
from django.views.decorators.csrf import csrf_exempt | |
import requests, json | |
# Get an OAuth token from https://www.eventbrite.com/myaccount/apps/ | |
# Create a new app, then expand the expandy-thingy and copy out the | |
# 'Your personal OAuth token' value. | |
EVENTBRITE_OAUTH_TOKEN = '...' | |
# Should look something like https://yoursite.slack.com/services/hooks/incoming-webhook?token=xxx...xxx | |
SLACK_URL = 'https://...' | |
SLACK_CHANNEL = '#webhooks_demo' | |
SLACK_USERNAME = 'Eventbrite' | |
SLACK_ICON_URL = 'https://dl.dropboxusercontent.com/u/2434252/2014/eventbrite_e_icon_ff8000_gradient.png' | |
def send_to_slack(order_info): | |
requests.post(SLACK_URL, { | |
'payload': json.dumps({ | |
'channel': SLACK_CHANNEL, | |
'username': SLACK_USERNAME, | |
'icon_url': SLACK_ICON_URL, | |
'text': message_from_order_details(order_info, include_event=True) | |
}) | |
}) | |
def message_from_order_details(order_info, include_event=False): | |
message = '%s ordered %d ticket%s' % ( | |
order_info['email'], | |
len(order_info['attendees']), | |
's' if len(order_info['attendees']) > 1 else '' | |
) | |
if include_event: | |
message += ' for <%(event_url)s|%(event_title)s>' % { | |
'event_url': order_info['event']['url'], | |
'event_title': order_info['event']['name']['text'], | |
} | |
return message | |
@csrf_exempt | |
def webhook_receive(request): | |
message = json.loads(request.body) | |
api_url = message['api_url'] | |
api_url = '%s?token=%s&expand=event,event.ticket_classes,attendees' % (api_url, EVENTBRITE_OAUTH_TOKEN) | |
order_info = requests.get(api_url).json() | |
send_to_slack(order_info) | |
return HttpResponse('Got order: %s' % repr(order_info)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How do you use this script? Could I get a step by step on how to set this up?