我正在开发一个Facebook迷你聊天机器人,我遇到一个问题,即机器人一遍又一遍地接收相同的消息,即使它已经回复了消息。
it keeps receiving the same text from FB and replying to it over and over
def message_handler(request):
data = json.loads(request.body.decode('utf-8'))
if data and data['object'] == 'page':
for pageEntry in data['entry']:
print "nombre de message", len(pageEntry['messaging'])
for 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 a moment to process it :). Will get back to you in a 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"}
)
我知道我需要每次都发送一个状态200,我做了但仍然一遍又一遍地收到相同的文字
以下是我订阅的活动
对话,message_deliveries,message_reads,messages,messaging_optins,messaging_postbacks,picture
我删除了 messaging_echoes ,因为我认为问题原因是
答案 0 :(得分:2)
我通过编写函数并检查Web API服务中的重复消息来解决此问题。
这里我通过有效载荷或从Facebook收到的消息生成消息唯一ID,用户点击或输入,然后与先前存储的并发字典中的唯一值进行比较。
_messageUniqueKeysBySender是ConcurrentDictionary,我按发件人ID缓存值30分钟。
private bool IsDuplicate(Messaging messaging)
{
var messageUniqueId = string.Empty;
var messageMessaging = messaging as MessageMessaging;
if (messageMessaging != null)
messageUniqueId = messageMessaging.Message.Id + messageMessaging.Message.SequenceNumber;
else if (messaging is 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;
}
然后检查主要代码
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 to process message");
message = new TextMessage(Resources.Error);
hasError = true;
}