From 6b807a54d8d3d113cd7140298a3b02e51dd85a09 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Thu, 18 Mar 2021 23:53:53 +0900 Subject: [PATCH 01/37] Restart with my github repo --- gettingstarted/settings.py | 17 ++++++ gettingstarted/urls.py | 2 + hello/views.py | 10 +++- requirements.txt | 4 +- webhook/__init__.py | 0 webhook/main.py | 106 +++++++++++++++++++++++++++++++++++++ 6 files changed, 137 insertions(+), 2 deletions(-) create mode 100644 webhook/__init__.py create mode 100644 webhook/main.py diff --git a/gettingstarted/settings.py b/gettingstarted/settings.py index 9ea552a0c..fb64e1705 100644 --- a/gettingstarted/settings.py +++ b/gettingstarted/settings.py @@ -50,6 +50,7 @@ "django.contrib.auth.middleware.AuthenticationMiddleware", "django.contrib.messages.middleware.MessageMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware", + 'request_logging.middleware.LoggingMiddleware', ] ROOT_URLCONF = "gettingstarted.urls" @@ -95,6 +96,22 @@ {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"}, ] +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'handlers': { + 'console': { + 'class': 'logging.StreamHandler', + }, + }, + 'loggers': { + 'django.request': { + 'handlers': ['console'], + 'level': 'DEBUG', # change debug level as appropiate + 'propagate': False, + }, + }, +} # Internationalization # https://docs.djangoproject.com/en/2.0/topics/i18n/ diff --git a/gettingstarted/urls.py b/gettingstarted/urls.py index 13b759582..da8ea7304 100644 --- a/gettingstarted/urls.py +++ b/gettingstarted/urls.py @@ -5,6 +5,7 @@ admin.autodiscover() import hello.views +import webhook.main # To add a new path, first import the app: # import blog @@ -17,5 +18,6 @@ urlpatterns = [ path("", hello.views.index, name="index"), path("db/", hello.views.db, name="db"), + path("webhook/", webhook.main.index, name="webhook"), path("admin/", admin.site.urls), ] diff --git a/hello/views.py b/hello/views.py index c2480724b..48059f547 100644 --- a/hello/views.py +++ b/hello/views.py @@ -2,11 +2,19 @@ from django.http import HttpResponse from .models import Greeting +import os + +import requests # Create your views here. def index(request): # return HttpResponse('Hello from Python!') - return render(request, "index.html") + #return render(request, "index.html") + # r = requests.get('http://httpbin.org/status/418') + # print(r.text) + # return HttpResponse('
' + r.text + '
') + times = int(os.environ.get('TIMES',3)) + return HttpResponse('Hello! '*times) def db(request): diff --git a/requirements.txt b/requirements.txt index 4af6a3765..91effc46f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ django gunicorn -django-heroku \ No newline at end of file +django-heroku +requests +django-request-logging \ No newline at end of file diff --git a/webhook/__init__.py b/webhook/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/webhook/main.py b/webhook/main.py new file mode 100644 index 000000000..ea0620dce --- /dev/null +++ b/webhook/main.py @@ -0,0 +1,106 @@ +from django.shortcuts import render +from django.http import HttpResponse +from django.views.decorators.csrf import csrf_exempt + +import os +import requests +import threading +import json +import random + + +@csrf_exempt +def index(request): + headers = request.headers + print("header: " + str(headers)) + + if request.method == 'GET': + return HttpResponse('Received GET') + elif request.method == 'POST': + print("Received POST") + + body_unicode = request.body.decode('utf-8') + data = json.loads(body_unicode) + print("data: " + str(data)) + + category = data['category'] + app_id = data['app_id'] + channel_url = data['channel']['channel_url'] + + print("app_id: " + app_id) + print("channel_url: " + channel_url) + + thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) + thread.start() + + return HttpResponse('Hello webhook!') + + +def sendAdminMessage(category, app_id, channel_url): + URL = "https://api-" + app_id + ".sendbird.com/v3/group_channels/" + channel_url + "/messages" + headers = {"Content-Type": "application/json; charset=utf8", + "Api-Token": "4a75b7fb787a3f6b57c4e63050003d619febd82c"} + if category == "group_channel:create": + (quote, author) = selectQuote() + data = {"message_type": "ADMM", "message": quote, "data": "Author: " + author} + res = requests.post(URL, headers=headers, data=json.dumps(data)) + print("response: " + res.text) + elif category == "group_channel:message_delete": + data = {"message_type": "ADMM", "message": "Message deleted"} + res = requests.post(URL, headers=headers, data=json.dumps(data)) + print("response: " + res.text) + + +def selectQuote(): + quotes = [ + ["You know, Hobbes, some days even my lucky rocketship underpants don't help.", "Bill Watterson"], + ["Even if we don't have the power to choose where we come from, we can still choose where we go from there.", + "Stephen Chbosky"], + ["And now that you don't have to be perfect, you can be good.", "John Steinbeck"], + [ + "Outside the windows the day was bright: golden sunshine, blue sky, pleasant wind... I wanted to punch " + "the happy day in the face, grab it by the hair, and beat it until it told me what the hell it was so " + "happy about.", + "Ilona Andrews"], + [ + "I haven't had a very good day. I think I might still be hungover and everyone's dead and my root beer's " + "gone.", + "Holly Black"], + [ + "It is impossible to live without failing at something, unless you live so cautiously that you might as " + "well not have lived at all - in which case, you fail by default.", + "J.K. Rowling"], + ["But I am very poorly today & very stupid & I hate everybody & everything. One lives only to make blunders.", + "Charles Darwin"], + [ + "We are cups, constantly and quietly being filled. The trick is knowing how to tip ourselves over and let " + "the beautiful stuff out.", + "Ray Bradbury"], + [ + "You may encounter many defeats, but you must not be defeated. In fact, it may be necessary to encounter " + "the defeats, so you can know who you are, what you can rise from, how you can still come out of it.", + "Maya Angelou"], + ["Life is never fair, and perhaps it is a good thing for most of us that it is not.", "Oscar Wilde"], + [ + "Maybe it’s not about having a beautiful day, but about finding beautiful moments. Maybe a whole day is " + "just too much to ask. I could choose to believe that in every day, in all things, no matter how dark and " + "ugly, there are shards of beauty if I look for them.", + "Anna White"], + ["I've had the sort of day that would make St. Francis of Assisi kick babies.", "Douglas Adams"], + ["Don't cry because it's over. Smile because it happened.", "Dr. Seuss"], + ["Everything is hard before it is easy.", "Johann Wolfgang von Goethe"], + ["Anyone who has never made a mistake has never tried anything new.", "Albert Einstein"], + [ + "These worst mornings with cold floors and hot windows and merciless light ? the soul's certainty that " + "the day will have to be not traversed but sort of climbed, vertically, and then that going to sleep " + "again at the end of it will be like falling, again, off something tall and sheer.", + "David Foster Wallace"], + ["Don't let your happiness depend on something you may lose.", "C.S. Lewis"], + [ + "It's so hard to forget pain, but it's even harder to remember sweetness. We have no scar to show for " + "happiness. We learn so little from peace.", + "Chuck Palahniuk"], + ["We are all broken, that's how the light gets in.", "Ernest Hemingway"], + ["Monsters are real, ghosts are real, too. They live inside us, and sometimes they win.", "Stephen King"] + ] + return quotes[random.randint(0, 19)] From 82a8000240c148a539dd4569e25cf399fc60f039 Mon Sep 17 00:00:00 2001 From: BrianSang Date: Fri, 19 Mar 2021 00:01:08 +0900 Subject: [PATCH 02/37] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9969eac2f..8621777a5 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ This application supports the [Getting Started with Python on Heroku](https://de Make sure you have Python 3.7 [installed locally](http://install.python-guide.org). To push to Heroku, you'll need to install the [Heroku CLI](https://devcenter.heroku.com/articles/heroku-cli), as well as [Postgres](https://devcenter.heroku.com/articles/heroku-postgresql#local-setup). ```sh -$ git clone https://github.com/heroku/python-getting-started.git +$ git clone https://github.com/BrianSang/python-getting-started.git $ cd python-getting-started $ python3 -m venv getting-started From 58d9b9eae29657b0bcf3431b13f7331cb4e8355d Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:04:53 +0900 Subject: [PATCH 03/37] update --- webhook/main.py | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index ea0620dce..40e8b5fa1 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -8,6 +8,10 @@ import json import random +from __future__ import unicode_literals +import hashlib, hmac + +API_TOKEN = b'4a75b7fb787a3f6b57c4e63050003d619febd82c' @csrf_exempt def index(request): @@ -20,26 +24,37 @@ def index(request): print("Received POST") body_unicode = request.body.decode('utf-8') - data = json.loads(body_unicode) - print("data: " + str(data)) + webhook_payload = json.loads(body_unicode) + print("webhook_payload: " + str(webhook_payload)) - category = data['category'] - app_id = data['app_id'] - channel_url = data['channel']['channel_url'] + category = webhook_payload['category'] + app_id = webhook_payload['app_id'] + channel_url = webhook_payload['channel']['channel_url'] print("app_id: " + app_id) print("channel_url: " + channel_url) + validate_X_Sendbird_Signature(webhook_payload) + thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() return HttpResponse('Hello webhook!') +def validate_X_Sendbird_Signature(webhook_payload): + signature_to_compare = hmac.new( + key=API_TOKEN, + msg=bytes(webhook_payload.encode('utf8')), + digestmod=hashlib.sha256).hexdigest() + + assert signature_to_compare == 'x_sendbird_signature', "x_sendbird_signature is NOT correct!" + + def sendAdminMessage(category, app_id, channel_url): URL = "https://api-" + app_id + ".sendbird.com/v3/group_channels/" + channel_url + "/messages" - headers = {"Content-Type": "application/json; charset=utf8", - "Api-Token": "4a75b7fb787a3f6b57c4e63050003d619febd82c"} + headers = {"Content-Type": "application/json; charset=utf8", "Api-Token": API_TOKEN} + if category == "group_channel:create": (quote, author) = selectQuote() data = {"message_type": "ADMM", "message": quote, "data": "Author: " + author} From 4cb85f8703ad1421616e5cce2bab0c1cdaa2f294 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:07:19 +0900 Subject: [PATCH 04/37] update --- webhook/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index 40e8b5fa1..a9dd64fec 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from django.shortcuts import render from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt @@ -8,7 +10,6 @@ import json import random -from __future__ import unicode_literals import hashlib, hmac API_TOKEN = b'4a75b7fb787a3f6b57c4e63050003d619febd82c' From 058d844f65879720944de2bd502b593466fd9628 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:15:46 +0900 Subject: [PATCH 05/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index a9dd64fec..3fcafe07a 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -46,7 +46,7 @@ def index(request): def validate_X_Sendbird_Signature(webhook_payload): signature_to_compare = hmac.new( key=API_TOKEN, - msg=bytes(webhook_payload.encode('utf8')), + msg=bytes(json.dumps(webhook_payload).encode('utf8')), digestmod=hashlib.sha256).hexdigest() assert signature_to_compare == 'x_sendbird_signature', "x_sendbird_signature is NOT correct!" From 125301e4db4d920765c0d88c204ab07dbc628979 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:26:46 +0900 Subject: [PATCH 06/37] update --- webhook/main.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 3fcafe07a..225c7081e 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -35,7 +35,9 @@ def index(request): print("app_id: " + app_id) print("channel_url: " + channel_url) - validate_X_Sendbird_Signature(webhook_payload) + x_sendbird_signature = headers['X-Sendbird-Signature'] + print("x_sendbird_signature: " + x_sendbird_signature) + validate_X_Sendbird_Signature(x_sendbird_signature, webhook_payload) thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() @@ -43,13 +45,13 @@ def index(request): return HttpResponse('Hello webhook!') -def validate_X_Sendbird_Signature(webhook_payload): +def validate_X_Sendbird_Signature(x_sendbird_signature, webhook_payload): signature_to_compare = hmac.new( key=API_TOKEN, msg=bytes(json.dumps(webhook_payload).encode('utf8')), digestmod=hashlib.sha256).hexdigest() - assert signature_to_compare == 'x_sendbird_signature', "x_sendbird_signature is NOT correct!" + assert signature_to_compare == x_sendbird_signature, "x_sendbird_signature is NOT correct!" def sendAdminMessage(category, app_id, channel_url): From 452075afbab9c7ad4a810b502e5710604f92254b Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:29:45 +0900 Subject: [PATCH 07/37] update --- webhook/main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/webhook/main.py b/webhook/main.py index 225c7081e..e20f5ee76 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -51,6 +51,8 @@ def validate_X_Sendbird_Signature(x_sendbird_signature, webhook_payload): msg=bytes(json.dumps(webhook_payload).encode('utf8')), digestmod=hashlib.sha256).hexdigest() + print("signature_to_compare: " + signature_to_compare) + assert signature_to_compare == x_sendbird_signature, "x_sendbird_signature is NOT correct!" From 390ed17e8465d8131c1fb13a48a5e3044a4fdc4c Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:35:46 +0900 Subject: [PATCH 08/37] update --- webhook/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index e20f5ee76..a4bac30a6 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -37,7 +37,7 @@ def index(request): x_sendbird_signature = headers['X-Sendbird-Signature'] print("x_sendbird_signature: " + x_sendbird_signature) - validate_X_Sendbird_Signature(x_sendbird_signature, webhook_payload) + validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode) thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() @@ -48,7 +48,7 @@ def index(request): def validate_X_Sendbird_Signature(x_sendbird_signature, webhook_payload): signature_to_compare = hmac.new( key=API_TOKEN, - msg=bytes(json.dumps(webhook_payload).encode('utf8')), + msg=bytes(webhook_payload.encode('utf8')), digestmod=hashlib.sha256).hexdigest() print("signature_to_compare: " + signature_to_compare) From da2aa23ad237aac51af4c8f26ffb46807c971281 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:40:33 +0900 Subject: [PATCH 09/37] update --- webhook/main.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index a4bac30a6..c0685d144 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -45,10 +45,11 @@ def index(request): return HttpResponse('Hello webhook!') -def validate_X_Sendbird_Signature(x_sendbird_signature, webhook_payload): +def validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode): signature_to_compare = hmac.new( key=API_TOKEN, - msg=bytes(webhook_payload.encode('utf8')), + #msg=bytes(body_unicode.encode('utf8')), + msg=bytes(body_unicode), digestmod=hashlib.sha256).hexdigest() print("signature_to_compare: " + signature_to_compare) From d8eb668b0fb6f99f27f7c42638cec1c7ff270f53 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 06:43:36 +0900 Subject: [PATCH 10/37] update --- webhook/main.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index c0685d144..7ade41bbe 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -25,12 +25,12 @@ def index(request): print("Received POST") body_unicode = request.body.decode('utf-8') - webhook_payload = json.loads(body_unicode) - print("webhook_payload: " + str(webhook_payload)) + body_json = json.loads(body_unicode) + print("webhook_payload: " + str(body_json)) - category = webhook_payload['category'] - app_id = webhook_payload['app_id'] - channel_url = webhook_payload['channel']['channel_url'] + category = body_json['category'] + app_id = body_json['app_id'] + channel_url = body_json['channel']['channel_url'] print("app_id: " + app_id) print("channel_url: " + channel_url) @@ -48,8 +48,7 @@ def index(request): def validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode): signature_to_compare = hmac.new( key=API_TOKEN, - #msg=bytes(body_unicode.encode('utf8')), - msg=bytes(body_unicode), + msg=bytes(body_unicode.encode('utf8')), digestmod=hashlib.sha256).hexdigest() print("signature_to_compare: " + signature_to_compare) From a4c5f2a047253a858e5346bf2cdae178691a24d7 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 07:31:10 +0900 Subject: [PATCH 11/37] update --- webhook/main.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 7ade41bbe..2ad23721b 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -1,15 +1,12 @@ from __future__ import unicode_literals -from django.shortcuts import render from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt -import os import requests import threading import json import random - import hashlib, hmac API_TOKEN = b'4a75b7fb787a3f6b57c4e63050003d619febd82c' @@ -53,7 +50,7 @@ def validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode): print("signature_to_compare: " + signature_to_compare) - assert signature_to_compare == x_sendbird_signature, "x_sendbird_signature is NOT correct!" + assert signature_to_compare == x_sendbird_signature, "signature_to_compare is NOT correct!" def sendAdminMessage(category, app_id, channel_url): From adde5ffd6ab7f5f9f2a4a6abef00e6f76e45bd17 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 18:41:18 +0900 Subject: [PATCH 12/37] update --- webhook/main.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 2ad23721b..410b0d3ee 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -8,8 +8,10 @@ import json import random import hashlib, hmac +import os -API_TOKEN = b'4a75b7fb787a3f6b57c4e63050003d619febd82c' +#API_TOKEN = b'4a75b7fb787a3f6b57c4e63050003d619febd82c' +API_TOKEN = os.environ.get("API_TOKEN") @csrf_exempt def index(request): @@ -50,7 +52,7 @@ def validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode): print("signature_to_compare: " + signature_to_compare) - assert signature_to_compare == x_sendbird_signature, "signature_to_compare is NOT correct!" + assert signature_to_compare == x_sendbird_signature, "x_sendbird_signature is different!" def sendAdminMessage(category, app_id, channel_url): From 8853f0d36c7147754e33728ca84e1670fd9d370b Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 18:50:07 +0900 Subject: [PATCH 13/37] update --- webhook/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 410b0d3ee..07626db60 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -9,9 +9,9 @@ import random import hashlib, hmac import os +from boto.s3.connection import S3Connection -#API_TOKEN = b'4a75b7fb787a3f6b57c4e63050003d619febd82c' -API_TOKEN = os.environ.get("API_TOKEN") +s3 = S3Connection(os.environ['API_TOKEN']) @csrf_exempt def index(request): From 55eeb27476679764a44555168ffe33727328b78f Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 18:54:47 +0900 Subject: [PATCH 14/37] update --- requirements.txt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 91effc46f..a74a85e3e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ django gunicorn django-heroku requests -django-request-logging \ No newline at end of file +django-request-logging +boto \ No newline at end of file From 82a3c9d2476bcaaf77ea45169c7dac102dfd8c63 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 19:01:15 +0900 Subject: [PATCH 15/37] update --- webhook/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 07626db60..3b092258d 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -9,9 +9,8 @@ import random import hashlib, hmac import os -from boto.s3.connection import S3Connection -s3 = S3Connection(os.environ['API_TOKEN']) +API_KEY = os.getenv('API_KEY') @csrf_exempt def index(request): From b910081f4ca03dc34ee4509d3e6135d1a8275e73 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 19:02:49 +0900 Subject: [PATCH 16/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index 3b092258d..d3b8a4eba 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -10,7 +10,7 @@ import hashlib, hmac import os -API_KEY = os.getenv('API_KEY') +API_TOKEN = os.getenv('API_TOKEN') @csrf_exempt def index(request): From b5a1ec2a52663c5ebf200d30d1fcba722c429155 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 19:05:05 +0900 Subject: [PATCH 17/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index d3b8a4eba..9986e0869 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -10,7 +10,7 @@ import hashlib, hmac import os -API_TOKEN = os.getenv('API_TOKEN') +API_TOKEN = os.getenv('API_TOKEN').encode() @csrf_exempt def index(request): From 18b21cfe0046e0a20a83fd4d151dab5c0bcc1823 Mon Sep 17 00:00:00 2001 From: BrianSang Date: Fri, 19 Mar 2021 19:13:11 +0900 Subject: [PATCH 18/37] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 8621777a5..38b430753 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,12 @@ Your app should now be running on [localhost:5000](http://localhost:5000/). $ heroku create $ git push heroku main +Set API_TOKEN environment variable +$ heroku config:set API_TOKEN= + +Unset API_TOKEN environment variable if needed +$ heroku config:unset API_TOKEN + $ heroku run python manage.py migrate $ heroku open ``` From cef3148592cae1ef98d4053d4094ede08f6fca71 Mon Sep 17 00:00:00 2001 From: BrianSang Date: Fri, 19 Mar 2021 19:13:53 +0900 Subject: [PATCH 19/37] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 38b430753..6251e6909 100644 --- a/README.md +++ b/README.md @@ -31,10 +31,10 @@ Your app should now be running on [localhost:5000](http://localhost:5000/). $ heroku create $ git push heroku main -Set API_TOKEN environment variable +# Set API_TOKEN environment variable $ heroku config:set API_TOKEN= -Unset API_TOKEN environment variable if needed +# Unset API_TOKEN environment variable if needed $ heroku config:unset API_TOKEN $ heroku run python manage.py migrate From 2bb6d0b3755215691c8e4a3f89bdfba7902de7c0 Mon Sep 17 00:00:00 2001 From: BrianSang Date: Fri, 19 Mar 2021 19:15:29 +0900 Subject: [PATCH 20/37] Update README.md --- README.md | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 6251e6909..db4bb4585 100644 --- a/README.md +++ b/README.md @@ -31,11 +31,9 @@ Your app should now be running on [localhost:5000](http://localhost:5000/). $ heroku create $ git push heroku main -# Set API_TOKEN environment variable -$ heroku config:set API_TOKEN= +$ heroku config:set API_TOKEN= # Set API_TOKEN environment variable -# Unset API_TOKEN environment variable if needed -$ heroku config:unset API_TOKEN +$ heroku config:unset API_TOKEN # Unset API_TOKEN environment variable if needed $ heroku run python manage.py migrate $ heroku open From 1ed952b49c9750532eab8307be6b8ae506b0be3d Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 19:22:47 +0900 Subject: [PATCH 21/37] update --- webhook/main.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 9986e0869..b60e94038 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -24,17 +24,13 @@ def index(request): body_unicode = request.body.decode('utf-8') body_json = json.loads(body_unicode) - print("webhook_payload: " + str(body_json)) + print("body_json: " + str(body_json)) category = body_json['category'] app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] - print("app_id: " + app_id) - print("channel_url: " + channel_url) - x_sendbird_signature = headers['X-Sendbird-Signature'] - print("x_sendbird_signature: " + x_sendbird_signature) validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode) thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) From f882fd6f05c42013a9303756c7f3cda8f99b178a Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 19:33:13 +0900 Subject: [PATCH 22/37] update --- webhook/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index b60e94038..d078e8423 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -22,7 +22,7 @@ def index(request): elif request.method == 'POST': print("Received POST") - body_unicode = request.body.decode('utf-8') + body_unicode = request.body.decode() body_json = json.loads(body_unicode) print("body_json: " + str(body_json)) @@ -42,7 +42,7 @@ def index(request): def validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode): signature_to_compare = hmac.new( key=API_TOKEN, - msg=bytes(body_unicode.encode('utf8')), + msg=bytes(body_unicode.encode()), digestmod=hashlib.sha256).hexdigest() print("signature_to_compare: " + signature_to_compare) From 8ff9e87cd876fda3ce436f0f1f2c47056975c9b2 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Fri, 19 Mar 2021 19:36:14 +0900 Subject: [PATCH 23/37] update --- requirements.txt | 3 +-- webhook/main.py | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/requirements.txt b/requirements.txt index a74a85e3e..91effc46f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,5 +2,4 @@ django gunicorn django-heroku requests -django-request-logging -boto \ No newline at end of file +django-request-logging \ No newline at end of file diff --git a/webhook/main.py b/webhook/main.py index d078e8423..b2f13d730 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -26,13 +26,13 @@ def index(request): body_json = json.loads(body_unicode) print("body_json: " + str(body_json)) + x_sendbird_signature = headers['X-Sendbird-Signature'] + validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode) + category = body_json['category'] app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] - x_sendbird_signature = headers['X-Sendbird-Signature'] - validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode) - thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() From a9de1bc4403b8a08dbbeab8ea778024c83107b56 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sat, 20 Mar 2021 16:49:52 +0900 Subject: [PATCH 24/37] update --- webhook/main.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index b2f13d730..c8fc86bad 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -33,9 +33,12 @@ def index(request): app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] - thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) - thread.start() - + if category == "group_cahnnel:create": + thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) + thread.start() + elif category == "group_channel:message_delete": + thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url)) + thread.start() return HttpResponse('Hello webhook!') @@ -59,12 +62,17 @@ def sendAdminMessage(category, app_id, channel_url): data = {"message_type": "ADMM", "message": quote, "data": "Author: " + author} res = requests.post(URL, headers=headers, data=json.dumps(data)) print("response: " + res.text) - elif category == "group_channel:message_delete": - data = {"message_type": "ADMM", "message": "Message deleted"} + + +def sendMessage(category, app_id, channel_url): + URL = "https://api-" + app_id + ".sendbird.com/v3/group_channels/" + channel_url + "/messages" + headers = {"Content-Type": "application/json; charset=utf8", "Api-Token": API_TOKEN} + + if category == "group_channel:message_delete": + data = {"message_type": "MESG", "message": "[ Message deleted ]"} res = requests.post(URL, headers=headers, data=json.dumps(data)) print("response: " + res.text) - def selectQuote(): quotes = [ ["You know, Hobbes, some days even my lucky rocketship underpants don't help.", "Bill Watterson"], From 6c7d337605581f0701d4699c4005e31550145345 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sat, 20 Mar 2021 16:54:04 +0900 Subject: [PATCH 25/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index c8fc86bad..15978337c 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -33,7 +33,7 @@ def index(request): app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] - if category == "group_cahnnel:create": + if category == "group_channel:create": thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() elif category == "group_channel:message_delete": From a77e8fff53348f0c47a6f13bf401abcc77362eee Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sat, 20 Mar 2021 17:00:33 +0900 Subject: [PATCH 26/37] update --- webhook/main.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 15978337c..037d7571f 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -32,12 +32,13 @@ def index(request): category = body_json['category'] app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] + user_id = body_json['sender']['user_id'] if category == "group_channel:create": thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() elif category == "group_channel:message_delete": - thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url)) + thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url, user_id)) thread.start() return HttpResponse('Hello webhook!') @@ -64,12 +65,12 @@ def sendAdminMessage(category, app_id, channel_url): print("response: " + res.text) -def sendMessage(category, app_id, channel_url): +def sendMessage(category, app_id, channel_url, user_id): URL = "https://api-" + app_id + ".sendbird.com/v3/group_channels/" + channel_url + "/messages" headers = {"Content-Type": "application/json; charset=utf8", "Api-Token": API_TOKEN} if category == "group_channel:message_delete": - data = {"message_type": "MESG", "message": "[ Message deleted ]"} + data = {"message_type": "MESG", "user_id": user_id, "message": "Message deleted"} res = requests.post(URL, headers=headers, data=json.dumps(data)) print("response: " + res.text) From 3f09217892428d4bc2e2170db8ca30e452857f43 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sat, 20 Mar 2021 17:05:31 +0900 Subject: [PATCH 27/37] update --- webhook/main.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 037d7571f..5489d26fe 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -32,13 +32,12 @@ def index(request): category = body_json['category'] app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] - user_id = body_json['sender']['user_id'] if category == "group_channel:create": thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() elif category == "group_channel:message_delete": - thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url, user_id)) + thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url)) thread.start() return HttpResponse('Hello webhook!') @@ -70,7 +69,7 @@ def sendMessage(category, app_id, channel_url, user_id): headers = {"Content-Type": "application/json; charset=utf8", "Api-Token": API_TOKEN} if category == "group_channel:message_delete": - data = {"message_type": "MESG", "user_id": user_id, "message": "Message deleted"} + data = {"message_type": "MESG", "user_id": "admin", "message": "Message deleted"} res = requests.post(URL, headers=headers, data=json.dumps(data)) print("response: " + res.text) From c7fdade43c61bf0abddc7e97f353a39f191d7437 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sat, 20 Mar 2021 17:05:48 +0900 Subject: [PATCH 28/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index 5489d26fe..224fbaec5 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -64,7 +64,7 @@ def sendAdminMessage(category, app_id, channel_url): print("response: " + res.text) -def sendMessage(category, app_id, channel_url, user_id): +def sendMessage(category, app_id, channel_url): URL = "https://api-" + app_id + ".sendbird.com/v3/group_channels/" + channel_url + "/messages" headers = {"Content-Type": "application/json; charset=utf8", "Api-Token": API_TOKEN} From 70508e23101eff64e81b72d5e78d8a3c7173227d Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sat, 20 Mar 2021 17:15:10 +0900 Subject: [PATCH 29/37] update --- webhook/main.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index 224fbaec5..01869385e 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -15,16 +15,18 @@ @csrf_exempt def index(request): headers = request.headers - print("header: " + str(headers)) if request.method == 'GET': - return HttpResponse('Received GET') + print("Received GET") + print("header: " + str(headers)) + return HttpResponse('Hellow GET') elif request.method == 'POST': print("Received POST") + print("Request headers: " + str(headers)) body_unicode = request.body.decode() body_json = json.loads(body_unicode) - print("body_json: " + str(body_json)) + print("Request body: " + str(body_json)) x_sendbird_signature = headers['X-Sendbird-Signature'] validate_X_Sendbird_Signature(x_sendbird_signature, body_unicode) @@ -32,12 +34,13 @@ def index(request): category = body_json['category'] app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] + user_id = body_json['sender']['user_id'] if category == "group_channel:create": thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() elif category == "group_channel:message_delete": - thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url)) + thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url, user_id)) thread.start() return HttpResponse('Hello webhook!') @@ -64,12 +67,12 @@ def sendAdminMessage(category, app_id, channel_url): print("response: " + res.text) -def sendMessage(category, app_id, channel_url): +def sendMessage(category, app_id, channel_url, user_id): URL = "https://api-" + app_id + ".sendbird.com/v3/group_channels/" + channel_url + "/messages" headers = {"Content-Type": "application/json; charset=utf8", "Api-Token": API_TOKEN} if category == "group_channel:message_delete": - data = {"message_type": "MESG", "user_id": "admin", "message": "Message deleted"} + data = {"message_type": "MESG", "user_id": user_id, "message": "[Message deleted]"} res = requests.post(URL, headers=headers, data=json.dumps(data)) print("response: " + res.text) From 34567ddaa3a630242c92e4214706d1bfc0888a2a Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sat, 20 Mar 2021 17:17:23 +0900 Subject: [PATCH 30/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index 01869385e..ada631596 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -34,12 +34,12 @@ def index(request): category = body_json['category'] app_id = body_json['app_id'] channel_url = body_json['channel']['channel_url'] - user_id = body_json['sender']['user_id'] if category == "group_channel:create": thread = threading.Thread(target=sendAdminMessage, args=(category, app_id, channel_url)) thread.start() elif category == "group_channel:message_delete": + user_id = body_json['sender']['user_id'] thread = threading.Thread(target=sendMessage, args=(category, app_id, channel_url, user_id)) thread.start() return HttpResponse('Hello webhook!') From 313796fd1a036a72a52a48229ad097c0e510e06a Mon Sep 17 00:00:00 2001 From: BrianSang Date: Sat, 20 Mar 2021 17:21:53 +0900 Subject: [PATCH 31/37] Update README.md --- README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/README.md b/README.md index db4bb4585..dad400a2d 100644 --- a/README.md +++ b/README.md @@ -31,9 +31,8 @@ Your app should now be running on [localhost:5000](http://localhost:5000/). $ heroku create $ git push heroku main -$ heroku config:set API_TOKEN= # Set API_TOKEN environment variable - $ heroku config:unset API_TOKEN # Unset API_TOKEN environment variable if needed +$ heroku config:set API_TOKEN= # Set API_TOKEN environment variable $ heroku run python manage.py migrate $ heroku open From 3dac986206ac360764d142eb56e85d2b7d1d7f97 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sun, 21 Mar 2021 13:39:56 +0900 Subject: [PATCH 32/37] update --- webhook/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/webhook/main.py b/webhook/main.py index ada631596..447e62a31 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -62,9 +62,9 @@ def sendAdminMessage(category, app_id, channel_url): if category == "group_channel:create": (quote, author) = selectQuote() - data = {"message_type": "ADMM", "message": quote, "data": "Author: " + author} + data = {"message_type": "ADMM", "message": quote, "data": "{\"Author\": \"" + author + "\""} res = requests.post(URL, headers=headers, data=json.dumps(data)) - print("response: " + res.text) + print("Response: " + res.text) def sendMessage(category, app_id, channel_url, user_id): @@ -74,7 +74,7 @@ def sendMessage(category, app_id, channel_url, user_id): if category == "group_channel:message_delete": data = {"message_type": "MESG", "user_id": user_id, "message": "[Message deleted]"} res = requests.post(URL, headers=headers, data=json.dumps(data)) - print("response: " + res.text) + print("Response: " + res.text) def selectQuote(): quotes = [ From 904fe19d1c063d98297b394feff35232b0612624 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Sun, 21 Mar 2021 13:41:46 +0900 Subject: [PATCH 33/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index 447e62a31..bcb8a4385 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -62,7 +62,7 @@ def sendAdminMessage(category, app_id, channel_url): if category == "group_channel:create": (quote, author) = selectQuote() - data = {"message_type": "ADMM", "message": quote, "data": "{\"Author\": \"" + author + "\""} + data = {"message_type": "ADMM", "message": quote, "data": "{\"Author\": \"" + author + "\"}"} res = requests.post(URL, headers=headers, data=json.dumps(data)) print("Response: " + res.text) From ebac66409fc6854f4a54dcb4d2971c5ef6def421 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Mon, 22 Mar 2021 11:58:50 +0900 Subject: [PATCH 34/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index bcb8a4385..4ceaa8b22 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -19,7 +19,7 @@ def index(request): if request.method == 'GET': print("Received GET") print("header: " + str(headers)) - return HttpResponse('Hellow GET') + return HttpResponse('Hello GET') elif request.method == 'POST': print("Received POST") print("Request headers: " + str(headers)) From 3f103287c0d9480db0359aedcca198a78e2b0173 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Mon, 22 Mar 2021 13:00:14 +0900 Subject: [PATCH 35/37] update --- webhook/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook/main.py b/webhook/main.py index 4ceaa8b22..e11bfb6c0 100644 --- a/webhook/main.py +++ b/webhook/main.py @@ -18,7 +18,7 @@ def index(request): if request.method == 'GET': print("Received GET") - print("header: " + str(headers)) + print("Request headers: " + str(headers)) return HttpResponse('Hello GET') elif request.method == 'POST': print("Received POST") From c0b1777bd4d4b21bb8ab64124d30270327372399 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Mon, 22 Mar 2021 13:04:08 +0900 Subject: [PATCH 36/37] update --- hello/views.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/hello/views.py b/hello/views.py index 48059f547..794ea49d2 100644 --- a/hello/views.py +++ b/hello/views.py @@ -8,13 +8,7 @@ # Create your views here. def index(request): - # return HttpResponse('Hello from Python!') - #return render(request, "index.html") - # r = requests.get('http://httpbin.org/status/418') - # print(r.text) - # return HttpResponse('
' + r.text + '
') - times = int(os.environ.get('TIMES',3)) - return HttpResponse('Hello! '*times) + return render(request, "index.html") def db(request): From 8407205f31892787290fa95f9eab84efeef7e7d9 Mon Sep 17 00:00:00 2001 From: Brian Sang Date: Mon, 22 Mar 2021 13:45:10 +0900 Subject: [PATCH 37/37] update --- hello/views.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/hello/views.py b/hello/views.py index 794ea49d2..c2480724b 100644 --- a/hello/views.py +++ b/hello/views.py @@ -2,12 +2,10 @@ from django.http import HttpResponse from .models import Greeting -import os - -import requests # Create your views here. def index(request): + # return HttpResponse('Hello from Python!') return render(request, "index.html")