2 years ago
Here are my views.py file
@csrf_exempt
def line_webhook(request):
if request.method == "POST":
try:
# create json object from the request body
data = json.loads(request.body.decode("utf-8"))
event_type = data['events'][0]['type']
if event_type == "message":
user_id = data['events'][0]['source']['userId']
message = data['events'][0]['message']['text']
print(f"User ID: {user_id}")
print(f"Message: {message}")
if user_id and message == 'UserId':
LineWebhook.objects.create(
user_id=user_id,
event_type=event_type,
)
try:
return HttpResponseRedirect(reverse('webhook_manager:get_user_id', kwargs={'user_id': user_id}))
except Exception as e:
print(f"Error redirecting: {e}")
return HttpResponse(status=500, content="Error redirecting")
except Exception as e:
print(f"Error processing webhook: {e}")
return HttpResponse(status=400, content="Error processing webhook")
else:
return HttpResponse(status=405, content="Method Not Allowed")def get_user_id(request, user_id): print(f"User ID: {user_id}") if request.method == "GET": chanel_access_token = config('CHANEL_ACCESS_TOKEN') if chanel_access_token: url = "https://api.line.me/v2/bot/message/push" headers = { "Content-Type": "application/json", "Authorization": f"Bearer {chanel_access_token}" } data = { "to": user_id, "messages": [ { "type": "text", "text": user_id } ] } response = requests.post(url, headers=headers, data=json.dumps(data)) print(response.status_code, response.text) if response.status_code == 200: return HttpResponse(status=200, content="Success") else: return HttpResponse(status=400, content="Failed") else: return HttpResponse(status=400, content="CHANEL_ACCESS_TOKEN not found in environment") else: return HttpResponse(status=405, content="Method Not Allowed")
and here are my urls.py file
app_name="webhook_manager" urlpatterns = [ path('line_webhook/', views.line_webhook, name='line_webhook'), path('get_user_id//', views.get_user_id, name='get_user_id'), ]
The problem is when I run line_webhook on my function it works well the line_webhook will redirect to get_user_id but when I deploy to railway it won't redirect to get_user_id and it won't produce any error either.
11 Replies
2 years ago
I don't why the code look messy in the thread there.
2 years ago
Make sure you are using https.
if config('DEPLOYMENT', cast=bool) == True:
print("run deployment")
return HttpResponseRedirect('https://' + config('RAILWAY_URL', default=.....production.up.railway.app') + reverse('webhook_manager:get_user_id', kwargs={'user_id': user_id}))
print("run local")
return redirect('webhook_manager:get_user_id', user_id=user_id)
I managed to use this condition but it still doesn't work
2 years ago
What is it? I'm sorry I'm new to deploying backend stuff :)
2 years ago
On Railway your app sits behind a proxy that handles https and routing, please make sure you have set gunicorn to trust the proxy.
2 years ago
I tried to set USE_X_FORWARDED_HOST = True, SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') in django and web: gunicorn flare_watcher.wsgi:application --forwarded-allow-ips="" --proxy-allow-from="" in Procfile but it still doesn't work.
2 years ago
The thing is I don't know where to look because all of the function worked well on my local nachine.