a year ago
Here are my views.py file
@csrfexempt def linewebhook(request):
if request.method == "POST":
try:
# create json object from the request body
data = json.loads(request.body.decode("utf-8"))
eventtype = data['events'][0]['type'] if eventtype == "message":
userid = data['events'][0]['source']['userId'] message = data['events'][0]['message']['text'] print(f"User ID: {userid}")
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 getuserid(request, userid): print(f"User ID: {userid}")
if request.method == "GET":
chanelaccesstoken = config('CHANELACCESSTOKEN')
if chanelaccesstoken:
url = "https://api.line.me/v2/bot/message/push"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {chanelaccesstoken}"
}
data = {
"to": userid, "messages": [ { "type": "text", "text": userid
}
]
}
response = requests.post(url, headers=headers, data=json.dumps(data))
print(response.statuscode, response.text) if response.statuscode == 200:
return HttpResponse(status=200, content="Success")
else:
return HttpResponse(status=400, content="Failed")
else:
return HttpResponse(status=400, content="CHANELACCESSTOKEN not found in environment")
else:
return HttpResponse(status=405, content="Method Not Allowed")
and here are my urls.py file
appname="webhookmanager"
urlpatterns = [
path('linewebhook/', views.linewebhook, name='linewebhook'), path('getuserid//', views.getuserid, name='getuser_id'),
]
The problem is when I run linewebhook on my function it works well the linewebhook will redirect to getuserid but when I deploy to railway it won't redirect to getuserid and it won't produce any error either.
11 Replies
a year ago
I don't why the code look messy in the thread there.
a year ago
Make sure you are using https.
if config('DEPLOYMENT', cast=bool) == True:
print("run deployment")
return HttpResponseRedirect('https://' + config('RAILWAYURL', default=…..production.up.railway.app') + reverse('webhookmanager:getuserid', kwargs={'userid': userid}))
print("run local")
return redirect('webhookmanager:getuserid', userid=user_id)
I managed to use this condition but it still doesn't work
a year ago
What is it? I'm sorry I'm new to deploying backend stuff :)
a year 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.
a year ago
I tried to set USEXFORWARDEDHOST = True, SECUREPROXYSSLHEADER = ('HTTPXFORWARDEDPROTO', 'https') in django and web: gunicorn flarewatcher.wsgi:application --forwarded-allow-ips="" --proxy-allow-from="" in Procfile but it still doesn't work.
a year ago
The thing is I don't know where to look because all of the function worked well on my local nachine.