-
-
Save vvalorous/a7b9c389fa787bd970848b11c181e6df to your computer and use it in GitHub Desktop.
Send new eventbrite orders to a Slack channel (Django view function)
This file contains hidden or 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