Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
shackra opened this issue Mar 24, 2018 · 1 comment

Comments

@shackra
Copy link

shackra commented Mar 24, 2018

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
@shackra shackra changed the title Page.handle_message is called before matching function decorated with Page.callback Page.handle_message is called before matching function decorated with Page.callback Mar 24, 2018
@hwonyo
Copy link
Contributor

hwonyo commented Aug 29, 2018

See related code line in fbmq
This issue occurred because QuickReply is a type of message event. And fbmq adopts a strategy to call both callback and event handler.

You means that message handler should not activated when QuickReply callback handler defines. right? It is more intuitive way to use :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants