Facebook chat bot sending same message multiple times (Python) -
i working on facebook mini-chat bot , encountering problem consists on bot receive same message on , on though has answered message.
it keeps receiving same text fb , replying on , over
def message_handler(request): data = json.loads(request.body.decode('utf-8')) if data , data['object'] == 'page': pageentry in data['entry']: print "nombre de message", len(pageentry['messaging']) messagingevent in pageentry['messaging']: if messagingevent.get('optin'): print "optin", messagingevent receivedauthentication(messagingevent) elif messagingevent.get('message'): print "message", messagingevent receivedmessage(messagingevent) elif messagingevent.get('delivery'): print "delivery", messagingevent receiveddeliveryconfirmation(messagingevent) elif messagingevent.get('postback'): print "postback", messagingevent receivedpostback(messagingevent) else: print "unhandled" return httpresponse(status=200) def receivedmessage(event): senderid = event.get('sender').get('id') message = event.get('message') messagetext = message.get('text') messageattachments = message.get('attachments') if messagetext: if messagetext == 'image': sendimagemessage(senderid) elif messagetext == 'button': sendbuttonmessage(senderid) elif messagetext == 'generic': sendgenericmessage(senderid) elif messagetext == 'receipt': sendreceiptmessage(senderid) elif messagetext == 'hey': sendtextmessage(senderid, "get it. gimme moment process :). in moment") send_seen() send_typing() words = words_gen() sendtextmessage(senderid, words) def callsendapi(messagedata): requests.post( url='https://graph.facebook.com/v2.6/me/messages?access_token=' + config.page_token, data=json.dumps(messagedata), headers={"content-type":"application/json"} )
i need send status 200 every time, did still receiving same text on , over
here events subscribed to
conversations, message_deliveries, message_reads, messages, messaging_optins, messaging_postbacks, picture
i removed messaging_echoes because thought problem turned out not
i have resolved issue writing function , checking duplicate messages in web api service.
here generating message unique id either payload or message received facebook user clicks or types , comparing earlier stored unique value concurrent dictionary.
_messageuniquekeysbysender concurrentdictionary , caching values sender id 30 minutes.
private bool isduplicate(messaging messaging) { var messageuniqueid = string.empty; var messagemessaging = messaging messagemessaging; if (messagemessaging != null) messageuniqueid = messagemessaging.message.id + messagemessaging.message.sequencenumber; else if (messaging postbackmessaging) messageuniqueid = ((postbackmessaging)messaging).postback.payload + ((postbackmessaging)messaging).timestampunix; if (string.isnullorempty(messageuniqueid)) return false; string existinguniqueid; if (_messageuniquekeysbysender.trygetvalue(messaging.sender.id, out existinguniqueid)) { if (existinguniqueid == messageuniqueid) { return true; } else { _messageuniquekeysbysender.tryupdate(messaging.sender.id, messageuniqueid, existinguniqueid); return false; } } _messageuniquekeysbysender.tryadd(messaging.sender.id, messageuniqueid); return false; }
and checking in main code
try { if (!isduplicate(messaging)) { var conversation = _conversationrepository[messaging.sender.id] ?? new conversation(messaging.sender.id); message = await _bot.respondtomessagingasync(conversation, messaging); _conversationrepository[messaging.sender.id] = conversation; _logger.forcontext("facebookmessage", messagingjson).logduration("processing facebook message", sw); } else _logger.forcontext("facebookmessage", messagingjson).warning("duplicate message skipped"); } catch (exception ex) { _logger.forcontext("facebookmessage", messagingjson).error(ex, "failed process message"); message = new textmessage(resources.error); haserror = true; }
Comments
Post a Comment