def webhook(): if request.method == 'POST': try: data = json.loads(request.data.decode()) sender = data['entry'][0]['messaging'][0]['sender']['id'] context = get_or_create_context(sender) # Get message if 'message' in data['entry'][0]['messaging'][0]: message = data['entry'][0]['messaging'][0]['message'] # Starting conversation if context.context == 'start': text = message['text'] # If text not in city list... chat_message = search_keyword(text) if chat_message: # if found keyword, reply with chat stuff message = send_text(sender, chat_message) send_message(message) else: payload = location_quick_reply(sender, get_message('pick-a-place')) send_message(payload) change_context(sender, 'start_location') return 'Ok' elif context.context in ['start_location', 'end_location']: if 'attachments' in message: if 'payload' in message['attachments'][0]: if 'coordinates' in message['attachments'][0]['payload']: location = message['attachments'][0]['payload']['coordinates'] latitude = location['lat'] longitude = location['long'] if context.context == 'start_location': define_location(sender, latitude=latitude, longitude=longitude, status='start') change_context(sender, 'end_location') payload = location_quick_reply(sender) send_message(payload) else: define_location(sender, latitude=latitude, longitude=longitude, status='end') estimate = estimate_price(sender, 2) if 'error' in estimate: message = send_text(sender, estimate['error']) send_message(message) return 'Ok' change_context(sender, 'start') message = send_text(sender, get_message('estimate').format(estimate)) send_message(message) else: text = message['text'] # If text not in city list... chat_message = search_keyword(text) if chat_message: # if found keyword, reply with chat stuff message = send_text(sender, chat_message) send_message(message) else: payload = location_quick_reply(sender, get_message('pick-a-place')) send_message(payload) except Exception as e: print(traceback.format_exc()) elif request.method == 'GET': if request.args.get('hub.verify_token') == os.environ.get('FB_VERIFY_TOKEN'): return request.args.get('hub.challenge') return "Wrong Verify Token" return "Nothing"
def webhook(): if request.method == 'POST': try: data = json.loads(request.data.decode()) sender = data['entry'][0]['messaging'][0]['sender']['id'] print(data) if 'message' in data['entry'][0]['messaging'][0]: message = data['entry'][0]['messaging'][0]['message'] if 'postback' in data['entry'][0]['messaging'][0]: # Action when user first enters the chat payload = data['entry'][0]['messaging'][0]['postback'][ 'payload'] if payload == 'begin_button': message = send_text(sender, 'Olá, tudo bem? Vamos começar?') send_message(message) payload = location_quick_reply(sender) send_message(payload) return 'Ok' # Resend the location button if payload == 'do_it_again': payload = location_quick_reply(sender) send_message(payload) if 'attachments' in message: if 'payload' in message['attachments'][0]: if 'coordinates' in message['attachments'][0]['payload']: location = message['attachments'][0]['payload'][ 'coordinates'] latitude = location['lat'] longitude = location['long'] send_weather_info(sender, latitude=latitude, longitude=longitude) if _return == 'error': message = send_text(sender, get_message('error')) send_message(message) payload = location_quick_reply(sender) send_message(payload) else: text = message['text'] for city in CITIES: if text.lower() in city: _return = send_weather_info(sender, city_name=text) if _return == 'error': message = send_text(sender, get_message('error')) send_message(message) # Send location button payload = location_quick_reply(sender) send_message(payload) return 'Ok' # If text not in city list... chat_message = search_keyword(text) if chat_message: # if found keyword, reply with chat stuff message = send_text(sender, chat_message) send_message(message) else: message = send_text(sender, get_message('not-a-city')) send_message(message) # Send location button payload = location_quick_reply(sender) send_message(payload) except Exception as e: print(traceback.format_exc()) elif request.method == 'GET': if request.args.get('hub.verify_token') == os.environ.get( 'FB_VERIFY_TOKEN'): return request.args.get('hub.challenge') return "Wrong Verify Token" return "Nothing"