Skip to content

Page.handle_message is called before matching function decorated with Page.callback #64

Description

@shackra

I've discovered that the Page.handle_message callback gets called before any function decorated with @Page.callback. I would expect that this function is called last and after other callback is executed no other callbacks are called including unless this callback returns True or something.

Here is a minimal example:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import logging
from os import getenv

from fbmq import Page, QuickReply
from flask import Flask, request

app = Flask(__name__)
page = Page(getenv('FB_PAGE_TOKEN'))

page.show_starting_button('HOLA')
logging.basicConfig(
    format='%(asctime)s - %(name)s - %(levelname)s: %(message)s',
    level=logging.DEBUG)


@app.route('/webhook', methods=['GET', 'POST'])
def webhook():
    if request.method == 'GET':
        if request.args.get('hub.verify_token') == getenv('FB_VERIFY_TOKEN'):
            return request.args.get('hub.challenge') or 'FAIL'
        return 'FB_VERIFY_TOKEN does not match.'
    elif request.method == 'POST':
        # Maneja las solicitudes enviadas desde Facebook
        if "facebookexternalhit" in request.headers.get('User-Agent'):
            page.handle_webhook(request.get_data(as_text=True))
        return 'OK'


@page.callback(['HOLA'])
def start_callback(payload, event):
    replies = [QuickReply("Hello World!", "PAYLOAD")]
    page.send(event.sender_id, "Say:", replies)


@page.callback(['PAYLOAD'])
def callback_qr_vitrina(payload, event):
    print("message proceed")


@page.handle_message
def message_handler(event):
    print("message was not processed?!")

When this example is ran, the following happens:

127.0.0.1 - - [23/Mar/2018 20:07:59] "POST /webhook HTTP/1.0" 200 -
2018-03-23 20:07:59,961 - werkzeug - INFO: 127.0.0.1 - - [23/Mar/2018 20:07:59] "POST /webhook HTTP/1.0" 200 -
message was not processed?!
2018-03-23 20:08:02,554 - urllib3.connectionpool - DEBUG: Starting new HTTPS connection (1): graph.facebook.com
2018-03-23 20:08:04,484 - urllib3.connectionpool - DEBUG: https://graph.facebook.com:443 "POST /v2.6/me/messages?access_token=<access token> HTTP/1.1" 200 85
message proceed

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions