davidchua / pymessenger

A Python Wrapper for the FB Messenger Bot API (Send/Receive API)
MIT License
459 stars 183 forks source link

sometime why it's giving replay 2-3 times ? #63

Open iamanam opened 6 years ago

iamanam commented 6 years ago

it was working good. But on heavy load it gives same replay 3-4 times. i am new to python, for a library i needed to rewrite the whole thing from Nodejs to python. Now, the response is slow in heavy load, and sometime i see same response 2-3 times. Can anyone help to let me know what's wrong im doing? Or any suggestion to make it better?

def hello():
    if request.method == 'GET':
        if request.args.get("hub.verify_token") == VERIFY_TOKEN:
            return request.args.get("hub.challenge")
        else:
            return 'Invalid verification token'
    try:
        if request.method == 'POST':
            output = request.get_json()
            print(output)
            for event in output['entry']:
                if event.get("messaging"):
                    messaging = event['messaging']
                    for x in messaging:
                        if x.get('message'):
                            recipient_id = x['sender']['id']
                            if x['message'].get('text'):
                                message = emoji.demojize(
                                    x['message']['text'])
                                message = message[1:300]  # slice character
                                #----------------------some other code----------------
                                if  condition
                                    similar = findSimilar(message)
                                    if similar != False:
                                        bot.send_text_message(recipient_id,
                                                              "Unkonwn words. Some similar words - \n => "+similar)
                                    else:
                                        bot.send_text_message(
                                            recipient_id, "Unknown word/sentence. No similar words found")
                                else:
                                    bot.send_text_message(
                                        recipient_id, translation)
                                if x['message'].get('attachments'):
                                    bot.send_text_message(
                                        recipient_id, "No result!!")
                        else:
                            return "Success"
                return "Success"
        return "Success"
Teejirapat commented 2 years ago

you need to use 'else' on that 'if event.get("messaging"):' line