def post(self, request, *args, **kwargs): incoming_message = json.loads(self.request.body.decode('utf-8')) logger.debug(incoming_message, extra=logging_extra(self.request)) if incoming_message['object'] != 'page': return HttpResponse() for entry in incoming_message['entry']: for messaging in entry.get('messaging', []): self.handle_messaging(messaging) return HttpResponse()
def get(self, request, *args, **kwargs): if self.request.GET['hub.verify_token'] \ == settings.CHATBOT_EVENT_TDC_VERIFY_TOKEN: return HttpResponse(self.request.GET['hub.challenge']) else: logger.debug( 'Failed validation. ' 'Make sure the validation tokens match.', extra=logging_extra(self.request)) return HttpResponseForbidden()
def post_generic_message(fb_page, receiver, elements): bot = utils.init_bot(fb_page) for i in xrange(3): generic_msg_response = bot.send_generic_message(receiver, elements) if 'error' not in generic_msg_response: break status = 'Error' if 'error' in generic_msg_response else 'Success' msg_tmpl = u'Receiver: {0}, Elements: {1}, Status: {2}, Response: {3}' logger.debug(msg_tmpl.format(receiver, elements, status, generic_msg_response), extra=logging_extra()) return status
def post_text_message(fb_page, receiver, message): bot = utils.init_bot(fb_page) for i in xrange(3): text_response = bot.send_text_message(receiver, message) if 'error' not in text_response: break status = 'Error' if 'error' in text_response else 'Success' msg_tmpl = u'Receiver: {0}, Message: {1}, Status: {2}, Response: {3}' utils.save_delivered_message(fb_page, receiver, message, text_response) logger.debug(msg_tmpl.format(receiver, message, status, text_response), extra=logging_extra()) return status
def landing(request): signed_request = request.signed_request try: app_data = json.loads(signed_request.get('app_data', '{}')) except: logger.warning('Cannot decode app_data from FB signed request.', extra=logging_extra(request)) app_data = {} user_data = signed_request.get('user', None) track_visitor(request, user_data, app_data) return _redirect('snm_tdc_hkjf17_info', app_data)
def post_button_message(fb_page, receiver, text, button): bot = utils.init_bot(fb_page) for i in xrange(3): button_response = bot.send_button_message(receiver, text, button) if 'error' not in button_response: break status = 'Error' if 'error' in button_response else 'Success' msg_tmpl = (u'Receiver: {0}, Text: {1}, Button: {2},' u' Status: {3}, Response: {4}') utils.save_delivered_message(fb_page, receiver, '%s %s' % (text, button), button_response) logger.debug(msg_tmpl.format(receiver, text, button, status, button_response), extra=logging_extra()) return status
def handle_messaging(self, messaging): messaging_map = { 'optin': handle_optin, 'message': handle_message, 'postback': handle_postback, 'delivery': handle_delivery, 'read': no_operation, } for key, func in messaging_map.items(): if key in messaging: func(messaging) break else: logger.debug('Webhook received unknown ' 'messaging event: %s', messaging, extra=logging_extra(self.request))
def canvas_landing(request, template_name='signup_mobile_tdc_hkjf17/canvas_landing.html' ): aid = request.GET.get(settings.CS_TRACKING_PARAMETER, None) if aid: app_data = { settings.CS_TRACKING_PARAMETER: aid, } query_parm = (('app_data', json.dumps(app_data)),) + \ settings.SNM_TDC_HKJF17_FB_PAGE_PARAM else: query_parm = settings.SNM_TDC_HKJF17_FB_PAGE_PARAM query_string = urllib.urlencode(query_parm) fb_page_url = '%s?%s' % (settings.SNM_TDC_HKJF17_FB_PAGE_URL, query_string) logger.debug(str(query_string), extra=logging_extra(request)) return render(request, template_name, { 'fb_page_url': fb_page_url, })
def signup_website( request, form_cls=SignupForm, network='website', mobile=False, template_name_website='signup_mobile_tdc_hkjf17/signup.html', template_name_wechat='signup_mobile_tdc_hkjf17/signup_wechat.html', template_name_mobile='signup_mobile_tdc_hkjf17/signup_mobile.html'): if network == 'website': template_name = (template_name_mobile if mobile else template_name_website) next_view = 'snm_tdc_hkjf17_thanks_website' elif network == 'wechat': template_name = template_name_wechat form_cls = WechatSignupForm next_view = 'snm_tdc_hkjf17_thanks_wechat' request.language = SIMPLIFIED_CHINESE aid = request.GET.get(settings.CS_TRACKING_PARAMETER, None) if request.method == 'GET': form = form_cls(language=request.language) return render( request, template_name, { 'CS_ACCOUNT_ID': settings.SNM_TDC_HKJF17_CS_ACCOUNT_ID, 'CS_TRACKING_PATH': settings.SNM_TDC_HKJF17_URL_ROOT, 'aid': aid, 'is_cn': True if network == 'wechat' else False, 'form': form, 'network': network, 'trackA': True, }) elif request.method == 'POST': logger.debug(str(request.POST), extra=logging_extra(request)) form = form_cls(data=request.POST, language=request.language) if not aid: aid_cookie = [ value for key, value in request.COOKIES.items() if '.'.join(('_pk_id', settings.SNM_TDC_HKFFW17_CS_ACCOUNT_ID)) in key ] aid = (aid_cookie[0].split('.')[-1] if len(aid_cookie) != 0 else None) if form.is_valid(): contact = form.save(commit=False) with transaction.atomic(): # NOTE: assume we have enough unused codes if contact.attend_jfs: code_jfs = SignupCode.objects.filter(used=False, is_jfs=True)[0] code_jfs.used = True code_jfs.save() contact.signup_code_jfs = code_jfs.value if contact.attend_idg: code_idg = SignupCode.objects.filter(used=False, is_idg=True)[0] code_idg.used = True code_idg.save() contact.signup_code_idg = code_idg.value contact_mgr = Contact.objects.filter(used_fb_mgr=True).count() if contact_mgr < settings.SNM_TDC_HKJF17_ALLOWED_FB_MGR_USER \ and ((request.language == 'chi' and contact.country == 'hk' ) or request.language == 'eng'): contact.is_show_mgr_btn = True fb_psid = form.cleaned_data['fb_psid'] contact.language = request.language contact.aid = aid contact.is_mobile = mobile for i in xrange(5): auth_str = binascii.hexlify(os.urandom(16)) if not Contact.objects.filter(auth_str=auth_str).exists(): contact.auth_str = auth_str contact.save() break if network == 'website': contact.language = request.language elif network == 'twitter': contact.language = ENGLISH contact.is_twitter = True elif network == 'wechat': contact.language = SIMPLIFIED_CHINESE contact.is_wechat = True contact.save() if fb_psid != '': event_query = Event.objects.filter( app_model_path='signup_mobile_tdc_hkjf17.Contact') for event in event_query: mark_registered(event, fb_psid) data_ref = ('signup_mobile_tdc_hkjf17.Contact|%s' % contact.auth_str) create_mgr_signups(settings.SNM_TDC_HKJF17_FB_PAGE_ID, fb_psid, data_ref) logger.debug('Contact received.', extra=logging_extra(request)) return _redirect( next_view, { 't': contact.auth_str, 'cid': contact.email, 'mgr': contact.is_show_mgr_btn, }) else: logger.debug('Invalid form data.', extra=logging_extra(request)) return render(request, template_name, { 'form': form, 'network': network, })
def signup( request, form_cls=SignupForm, mobile=False, template_name_desktop='signup_mobile_tdc_hkelfae17/signup.html', template_name_mobile='signup_mobile_tdc_hkelfae17/signup_mobile.html'): template_name = template_name_mobile if mobile else template_name_desktop next_view = ('snm_tdc_hkelfae17_thanks_mobile' if mobile else 'snm_tdc_hkelfae17_thanks') aid = request.GET.get(settings.CS_TRACKING_PARAMETER, None) if request.method == 'GET': form = form_cls(language=request.language) return render( request, template_name, { 'CS_ACCOUNT_ID': settings.SNM_TDC_HKELFAE17_CS_ACCOUNT_ID, 'CS_TRACKING_PATH': settings.SNM_TDC_HKELFAE17_URL_ROOT, 'aid': aid, 'form': form, 'trackA': True, }) elif request.method == 'POST': logger.debug(str(request.POST), extra=logging_extra(request)) form = form_cls(data=request.POST, language=request.language) if not aid: aid_cookie = [ value for key, value in request.COOKIES.items() if '.'.join(('_pk_id', settings.SNM_TDC_HKELFAE17_CS_ACCOUNT_ID)) in key ] aid = (aid_cookie[0].split('.')[-1] if len(aid_cookie) != 0 else None) if form.is_valid(): contact = form.save(commit=False) with transaction.atomic(): # NOTE: assume we have enough unused codes if contact.attend_elec: code_elec = SignupCode.objects.filter(used=False, is_elec=True)[0] code_elec.used = True code_elec.save() contact.signup_code_elec = code_elec.value if contact.attend_light: code_light = SignupCode.objects.filter(used=False, is_light=True)[0] code_light.used = True code_light.save() contact.signup_code_light = code_light.value # if request.language == 'eng': # contact.is_show_mgr_btn = True fb_psid = form.cleaned_data['fb_psid'] contact.language = request.language contact.aid = aid contact.is_mobile = mobile for i in xrange(5): auth_str = binascii.hexlify(os.urandom(16)) if not Contact.objects.filter(auth_str=auth_str).exists(): contact.auth_str = auth_str contact.save() break contact.save() if fb_psid != '': event_query = Event.objects.filter( app_model_path='signup_mobile_tdc_hkelfae17.Contact') for event in event_query: mark_registered(event, fb_psid) data_ref = ('signup_mobile_tdc_hkelfae17.Contact|%s' % contact.auth_str) create_mgr_signups(settings.SNM_TDC_HKELFAE17_FB_PAGE_ID, fb_psid, data_ref) logger.debug('Contact received.', extra=logging_extra(request)) return _redirect( next_view, { 't': contact.auth_str, 'cid': contact.email, 'mgr': contact.is_show_mgr_btn, }) else: logger.debug('Invalid form data.', extra=logging_extra(request)) return render(request, template_name, { 'form': form, })