예제 #1
0
def handle_first_time_user(users, user):
    user_id = user['user_id']
    token = app.config['PAT']

    hi = "%s %s, nice to meet you :)"%(NLP.sayHiTimeZone(user), user['first_name'])
    FB.send_message(token, user_id, hi)

    FB.send_picture(app.config['PAT'], user_id, 'https://monosnap.com/file/I6WEAs2xvpZ5qTNmVauNguEzcaRrnI.png')
    
    handle_help(user_id)
    FB.send_message(app.config['PAT'], user_id, "Next time just tell me \"help\" to view this again :D")
예제 #2
0
def handle_first_time_user(users, user):
    user_id = user['user_id']
    token = app.config['PAT']

    hi = "%s %s, nice to meet you :)"%(NLP.sayHiTimeZone(user), user['first_name'])
    FB.send_message(token, user_id, hi)

    FB.send_picture(app.config['PAT'], user_id, 'https://monosnap.com/file/I6WEAs2xvpZ5qTNmVauNguEzcaRrnI.png')
    
    handle_help(user_id)
    FB.send_message(app.config['PAT'], user_id, "Next time just tell me \"help\" to view this again :D")
예제 #3
0
def handle_messages():
    # print "Handling Messages"
    payload = request.get_data()
    if app.config['PRINT_INCOMING_PAYLOAD']:
        print payload
    token = app.config['PAT']

    webhook_type = get_type_from_payload(payload)

    # Handle Postback
    # Developer-defined postbacks
    # Currently in use: Help button (in Persistent Menu), and Getting Started button (first-time users will see this)
    if webhook_type == 'postback':
        for sender_id, postback_payload in postback_events(payload):
            if postback_payload == 'OPTIMIST_HELP':
                handle_help(sender_id)

            elif postback_payload == 'OPTIMIST_GET_STARTED':
                if not Mongo.user_exists(users, sender_id):
                    g.user = Mongo.get_user_mongo(users, sender_id)
                    return handle_first_time_user(users, g.user)

    # Handle messages
    elif webhook_type == 'message':
        for sender_id, message in messaging_events(payload):
            # Only process message in here
            if not message:
                return "ok"

            # Handle Facebook's bug when receiving long audio
            # The bug: The app keeps receiving the same POST request
            # This acts as a rescue exit signal
            global temp_message_id 
            mid = message['message_id']
            if mid == temp_message_id:
                return 'ok'
            temp_message_id = mid

            # Start processing valid requests
            if app.config['PRINT_INCOMING_MESSAGE']:
                print "User ID: %s\nMessage:%s" % (sender_id, message)
            try:
                FB.show_typing(token, sender_id)
                response = processIncoming(sender_id, message)
                FB.show_typing(token, sender_id, 'typing_off')

                if response is not None and response != 'pseudo':
                    # 'pseudo' is an "ok" signal for functions that sends response on their own
                    # without returning anything back this function
                    print response
                    FB.send_message(token, sender_id, response)

                elif response != 'pseudo':
                    if NLP.randOneIn(7):
                        FB.send_message(token, sender_id, NLP.oneOf(NLP.no_response))
                        FB.send_picture(token, sender_id, 'https://monosnap.com/file/I6WEAs2xvpZ5qTNmVauNguEzcaRrnI.png')
            except Exception, e:
                print e
                traceback.print_exc()
                FB.send_message(app.config['PAT'], sender_id, NLP.oneOf(NLP.error))
                Mongo.pop_context(users, g.user)
                if NLP.randOneIn(7):
                    FB.send_picture(app.config['PAT'], sender_id, 'https://monosnap.com/file/3DnnKT60TkUhF93dwjGbNQCaCUK9WH.png')
예제 #4
0
def handle_messages():
    # print "Handling Messages"
    payload = request.get_data()
    if app.config['PRINT_INCOMING_PAYLOAD']:
        print payload
    token = app.config['PAT']

    webhook_type = get_type_from_payload(payload)

    # Handle Postback
    # Developer-defined postbacks
    # Currently in use: Help button (in Persistent Menu), and Getting Started button (first-time users will see this)
    if webhook_type == 'postback':
        for sender_id, postback_payload in postback_events(payload):
            if postback_payload == 'OPTIMIST_HELP':
                handle_help(sender_id)

            elif postback_payload == 'OPTIMIST_GET_STARTED':
                if not Mongo.user_exists(users, sender_id):
                    g.user = Mongo.get_user_mongo(users, sender_id)
                    return handle_first_time_user(users, g.user)

    # Handle messages
    elif webhook_type == 'message':
        for sender_id, message in messaging_events(payload):
            # Only process message in here
            if not message:
                return "ok"

            # Handle Facebook's bug when receiving long audio
            # The bug: The app keeps receiving the same POST request
            # This acts as a rescue exit signal
            global temp_message_id
            mid = message['message_id']
            if mid == temp_message_id:
                return 'ok'
            temp_message_id = mid

            # Start processing valid requests
            if app.config['PRINT_INCOMING_MESSAGE']:
                print "User ID: %s\nMessage:%s" % (sender_id, message)
            try:
                FB.show_typing(token, sender_id)
                response = processIncoming(sender_id, message)
                FB.show_typing(token, sender_id, 'typing_off')

                if response is not None and response != 'pseudo':
                    # 'pseudo' is an "ok" signal for functions that sends response on their own
                    # without returning anything back this function
                    print response
                    FB.send_message(token, sender_id, response)

                elif response != 'pseudo':
                    if NLP.randOneIn(7):
                        FB.send_message(token, sender_id,
                                        NLP.oneOf(NLP.no_response))
                        FB.send_picture(
                            token, sender_id,
                            'https://monosnap.com/file/I6WEAs2xvpZ5qTNmVauNguEzcaRrnI.png'
                        )
            except Exception, e:
                print e
                traceback.print_exc()
                FB.send_message(app.config['PAT'], sender_id,
                                NLP.oneOf(NLP.error))
                Mongo.pop_context(users, g.user)
                if NLP.randOneIn(7):
                    FB.send_picture(
                        app.config['PAT'], sender_id,
                        'https://monosnap.com/file/3DnnKT60TkUhF93dwjGbNQCaCUK9WH.png'
                    )