Last active
May 4, 2018 15:16
-
-
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
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
(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" | |
''' |
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
mysite | |
├── manage.py | |
└── mysite | |
├── __init__.py | |
├── settings.py | |
├── urls.py | |
└── wsgi.py |
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
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 |
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
web: gunicorn mysite.wsgi |
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
python-3.6.4 |
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
/ ~~~~~~~~~~~~~~~~~~ 中略 ~~~~~~~~~~~~~~~~~~~~ / | |
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 # <= ここが最終行です |
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.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 | |
] |
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
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('インデックスページ') |
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 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