Skip to content

Instantly share code, notes, and snippets.

@Ningensei848
Last active May 4, 2018 15:16
Show Gist options
  • Save Ningensei848/33b11f43f660733d9cee43e6d021eb0f to your computer and use it in GitHub Desktop.
Save Ningensei848/33b11f43f660733d9cee43e6d021eb0f to your computer and use it in GitHub Desktop.
DjangoでherokuにデプロイしEvents API(slack)のVerificationを通す ref: https://qiita.com/Ningensei848/items/c83c2d8e3f11ebba1fd4
(myvenv) $ heroku config:set SLACK_BOT_USER_TOKEN="xoxb-111122223333-iEUserT0kenhHfM53HtxHSl0" \
SLACK_CLIENT_SECRET="40ji7secreta665294secrete97y3r7O" \
SLACK_VERIFICATION_TOKEN="verificationgedXbv9gehow" \
SLACK_CLIENT_ID="212341111111.287651111111"
(myvenv) $ heroku config
'''
SLACK_BOT_USER_TOKEN: "xoxb-111122223333-iEUserT0kenhHfM53HtxHSl0"
SLACK_CLIENT_SECRET: "40ji7secreta665294secrete97y$r7O"
SLACK_VERIFICATION_TOKEN: "verificationgedXbv9gehow"
SLACK_CLIENT_ID: "212341111111.287651111111"
'''
mysite
├── manage.py
└── mysite
├── __init__.py
├── settings.py
├── urls.py
└── wsgi.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
DEBUG = True
web: gunicorn mysite.wsgi
python-3.6.4
/ ~~~~~~~~~~~~~~~~~~ 中略 ~~~~~~~~~~~~~~~~~~~~ /
import dj_database_url
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
STATIC_ROOT = 'staticfiles'
DEBUG = False
try:
from .local_settings import *
except ImportError:
pass # <= ここが最終行です
from django.contrib import admin
from django.urls import path, include
from tool_manage.views import Events
from django.views.decorators.csrf import csrf_exempt
urlpatterns = [
path('', include('tool_manage.urls')), # <= add
path('admin/', admin.site.urls), # <= default
path('events/', csrf_exempt(Events.as_view())), # <= add
# csrf無効についての参考:https://bit.ly/2juuJpa
]
import json
from django.conf import settings
from django.shortcuts import render
from django.http import HttpResponse
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import status
from slackclient import SlackClient
SLACK_VERIFICATION_TOKEN = getattr(settings, 'SLACK_VERIFICATION_TOKEN', None)
SLACK_BOT_USER_TOKEN = getattr(settings,'SLACK_BOT_USER_TOKEN', None)
Client = SlackClient(SLACK_BOT_USER_TOKEN)
class Events(APIView):
def post(self, request, *args, **kwargs):
slack_message = request.data
# verification challenge
if slack_message.get('type') == 'url_verification':
return Response(data=slack_message,
status=status.HTTP_200_OK)
########################### Pages ###################################
def index(request):
# index.html
return HttpResponse('インデックスページ')
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment