hayabhay / django-facebook-messenger-bot-tutorial

Code for my Tutorial on building a Facebook Messenger bot using Django/Python
https://abhaykashyap.com/blog/post/tutorial-how-build-facebook-messenger-bot-using-django-ngrok
MIT License
71 stars 51 forks source link

I do not get "hub.verify_token" in get request when i open url in browser #9

Open sap101094 opened 6 years ago

sap101094 commented 6 years ago

I have downloaded and setup your code and I have set up ngrok also and configured app on facebookdeveloper site but after all configuration when I am going to open https://6bc98399.ngrok.io/fb_yomamabot/66d2b8f4a09cd35cb23076a1da5d51529136a3373fd570b122/, then it will throw error.

Internal Server Error: /fb_yomamabot/66d2b8f4a09cd35cb23076a1da5d51529136a3373fd570b122/ Traceback (most recent call last): File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, callback_kwargs) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, *kwargs) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper return bound_func(args, kwargs) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, kwargs) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func return func.get(self, type(self))(*args2, *kwargs2) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/yomamabot/fb_yomamabot/views.py", line 55, in dispatch return generic.View.dispatch(self, request, args, kwargs) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch return handler(request, *args, **kwargs) File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/yomamabot/fb_yomamabot/views.py", line 48, in get if self.request.GET['hub.verify_token'] == VERIFY_TOKEN: File "/home/sagar-p/Desktop/django-facebook-messenger-bot-tutorial/bottutorial/local/lib/python2.7/site-packages/django/utils/datastructures.py", line 85, in getitem raise MultiValueDictKeyError(repr(key)) MultiValueDictKeyError: "'hub.verify_token'"

It is not getting anything in "self.request.GET"... It is getting "<QueryDict: {}>". Please help me to figureout this error.

anisbhsl commented 5 years ago

MultiValueDictKeyError can be solved using try-catch statement

from django.utils.datastructures import MultiValueDictKeyError
class weatherBotView(View):
     def get(self, request, *args, **kwargs):
        try:
            token=self.request.GET['hub.verify_token']
        except MultiValueDictKeyError:
            token=False

        if token==VERIFY_TOKEN:
            return HttpResponse(request.GET['hub.challenge'])
        else:
            return HttpResponse("Error invalid token")