使用 facebook messenger bot 示例教程代码时,我收到消息:'(#100) No matching user found'

Posted

技术标签:

【中文标题】使用 facebook messenger bot 示例教程代码时,我收到消息:\'(#100) No matching user found\'【英文标题】:I am getting message: '(#100) No matching user found', when using the facebook messenger bot sample tutorial code使用 facebook messenger bot 示例教程代码时,我收到消息:'(#100) No matching user found' 【发布时间】:2016-11-10 08:32:49 【问题描述】:

使用相同的 Facebook 入门指南 (https://developers.facebook.com/docs/messenger-platform/quickstart)

在输入消息并将其发送到机器人时,我在 node.js 控制台上遇到错误。

    error: 
       message: '(#100) No matching user found',
        type: 'OAuthException',
        code: 100,
        fbtrace_id: 'BLguK1o+VsH'    

我有最新版本的 node.js 并且正在使用 Ngrok 来公开我的本地主机。

我的 app.post 例程如下:(它是入门指南的精确副本)

router.post('/webhook', function (req, res) 
  var data = req.body;

  // Make sure this is a page subscription
  if (data.object == 'page') 
    // Iterate over each entry
    // There may be multiple if batched
    data.entry.forEach(function(pageEntry) 
      var pageID = pageEntry.id;
      var timeOfEvent = pageEntry.time;

      // Iterate over each messaging event
      pageEntry.messaging.forEach(function(messagingEvent) 
        if (messagingEvent.optin) 
          receivedAuthentication(messagingEvent);
         else if (messagingEvent.message) 
          receivedMessage(messagingEvent);
         else if (messagingEvent.delivery) 
          receivedDeliveryConfirmation(messagingEvent);
         else if (messagingEvent.postback) 
          receivedPostback(messagingEvent);
         else 
          console.log("Webhook received unknown messagingEvent: ", messagingEvent);
        
      );
    );

    // Assume all went well.
    //
    // You must send back a 200, within 20 seconds, to let us know you've 
    // successfully received the callback. Otherwise, the request will time out.
  //  res.sendStatus(200);
  
); 

这里是被调用的 receivedMessage 函数

function receivedMessage(event) 
  var senderID = event.sender.id;
  var recipientID = event.recipient.id;
  var timeOfMessage = event.timestamp;
  var message = event.message;

  console.log("Received message for user %d and page %d at %d with message:", 
    senderID, recipientID, timeOfMessage);
    console.log(JSON.stringify(message));


  var messageId = message.mid;

  // You may get a text or attachment but not both
  var messageText = message.text;
  var messageAttachments = message.attachments;

  if (messageText) 

    // If we receive a text message, check to see if it matches any special
    // keywords and send back the corresponding example. Otherwise, just echo
    // the text we received.
    switch (messageText) 
      case 'image':
        sendImageMessage(senderID);
        break;

      case 'button':
        sendButtonMessage(senderID);
        break;

      case 'generic':
        sendGenericMessage(senderID);
        break;

      case 'receipt':
        sendReceiptMessage(senderID);
        break;

      default:
        //getArticles(function(err,articles)
        //  sendTextMessage(senderID, articles[0].text);
        //);     
        sendTextMessage(senderID, messageText);

       /* client.converse('my-user-session-42', messageText, )
            .then((data) => 
                console.log('the question asked :' + messageText);
            sendTextMessage(senderID, data.msg);    
            console.log('Yay, got Wit.ai response: ' + JSON.stringify(data));
        ).catch(console.error);
        */


    
   else if (messageAttachments) 
    sendTextMessage(senderID, "Message with attachment received");
  

更多这里是我的要求声明:

const bodyParser = require('body-parser');
var express = require('express');
var request = require('request');
var router = express.Router();
const fetch = require('node-fetch');
const Wit, log = require('node-wit');

以下是使用的其余函数:

function sendTextMessage(recipientId, messageText) 
  var messageData = 
    recipient: 
      id: recipientId
    ,
    message: 
      text: messageText
    
  ;

  callSendAPI(messageData);


function callSendAPI(messageData) 
  request(
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs:  access_token: PAGE_ACCESS_TOKEN ,
    method: 'POST',
    json: messageData

  , function (error, response, body) 
    if (!error && response.statusCode == 200) 
      var recipientId = body.recipient_id;
      var messageId = body.message_id;

      console.log("Successfully sent generic message with id %s to recipient %s", 
        messageId, recipientId);
     else 
      console.error("Unable to send message.");
      console.error(response);
      console.error(error);
    
  );  

所以我在某处读到这一切都与页面 ID 或 facebook 处理整数的方式有关,但我有点困惑。

任何帮助将不胜感激。

【问题讨论】:

我收到了同样的错误信息,几天前这里的一切都正常了。也找不到任何有关重大更改的消息。太不稳定了。 实在是太郁闷了,官方教程代码好像下不了,想看懂继续工作 突然那些奇怪的错误消失了,我能够再次成功发送/接收消息。你有同样的经历吗? 有时它可以工作,但现在我不断收到我输入的消息的回声。似乎在某个地方我可以找到一个循环。 我今天早些时候有同样的经历,并发现这是由于 Facebook Messenger 机器人的新回显功能导致它循环然后抛出如上所述的错误(因为它最终尝试发送到页面id 而不是用户 id)。我的解决办法是检查message.is_echo。如果为真,请记录它并且不执行任何进一步的操作。发生这种情况是因为 Facebook 将回声视为消息,即除了 is_echo 字段外,它们在任何地方都没有标志(请参阅:developers.facebook.com/docs/messenger-platform/…) 【参考方案1】:

出现此错误是因为使用了不正确的令牌将消息发送回机器人。确保你有正确的(即使一开始是硬编码),可以从你的 developers.facebook.com 页面的 Messenger -> Settings -> Token Generation 下复制。

【讨论】:

令牌偶尔会改变。在应用程序设置中,我们必须选择关联的 FB 页面并生成令牌。之后,我们必须像这样重新订阅应用程序:developers.facebook.com/docs/messenger-platform/…【参考方案2】:

你只需要绕过你自己的消息。

您的 Bot 具有唯一 ID(不是 App ID),因此您可以在 POST 逻辑中尝试此解决方法:

var myID = '...' ;
.....
event = req.body.entry[0].messaging[i];
sender = event.sender.id;
if (event.message && event.message.text && sender != myID) 
.....

您可以通过查看您的消息来获取您的 Bot id:

"sender":
    "id":"USER_ID"
  ,

Message Received reference

JSON 示例:

even.message: 
 "sender":"id":"**yourBotID**",
   "recipient":"id":"**clientID**",
   "timestamp":1468241667962,
   "message":   "**is_echo":true**,        
   "app_id":**appID**,
   "mid":"mid....",
   "seq":617,"text":"..."

提示:要识别您的 BotID,只需在消息负载中查找 "message": "is_echo": true,..

问候

【讨论】:

嗨,谢谢,我对 node.js 和机器人还很陌生,所以我不太明白 myId 是什么。 我得到这样的发件人ID - function receivedMessage(event) var senderID = event.sender.id.toString(); ..... 您的解决方案会修复以下消息:'(#100) No matching user found',问题吗? 是的,当然。我为我自己的机器人 @myRolloBot facebook.com/Myrollobot 编写了这个解决方法 对不起,我对此有点陌生,你能告诉我如何获得 myID 吗?我知道 sender.id 来自哪里。【参考方案3】:

我去了 Facebook Messenger 设置 -> Webhooks -> 编辑事件并从订阅的事件中删除了 messenger_echoes。这解决了问题。

【讨论】:

完全有效!希望我能多次投票【参考方案4】:

我遵循了这个指南http://x-team.com/2016/04/how-to-get-started-with-facebook-messenger-bots/

一切都奏效了:

我在下面发布了我的代码:

'use strict'
const bodyParser = require('body-parser');
var express = require('express');
var request = require('request');
var app = express();
const fetch = require('node-fetch');
const Wit, log = require('node-wit');


app.use(bodyParser.urlencoded(extended: false))

var PAGE_ACCESS_TOKEN =  '********'
var MY_TOKEN = '********';

//  Wit.ai code 
const client = new Wit(accessToken: '*********');


//  GET home page. 
app.get('/', function(req, res, next) 
  res.render('index',  title: 'Express' );
);

app.get('/webhook/', function(req, res) 
  if (req.query['hub.mode'] === 'subscribe' &&
      req.query['hub.verify_token'] === 'hello_token_success') 
    console.log("Validating webhook");
    res.status(200).send(req.query['hub.challenge']);
   else 
    console.error("Failed validation. Make sure the validation tokens match.");
    res.sendStatus(403);          
    
);

//** Receive Messages **

app.post('/webhook/', function (req, res) 

  var data = req.body; 

  var events = req.body.entry[0].messaging;

  for (var i = 0; i < events.length ; i++) 


    var event = events[i];

    if (event.message && event.message.text && !event.message.is_echo) 
        var text = event.message.text;

        sendTextMessage(event.sender.id, "Text received, echo: "+ text.substring(0, 200));
    

  


    // Assume all went well.
    //
    // You must send back a 200, within 20 seconds, to let us know you've 
    // successfully received the callback. Otherwise, the request will time out.
    res.sendStatus(200);

);



function receivedMessage(event) 
  var senderID = event.sender.id.toString();
  var recipientID = event.recipient.id.toString();
  var timeOfMessage = event.timestamp;
  var message = event.message;

  console.log("Received message for user %d and page %d at %d with message:", 
    senderID, recipientID, timeOfMessage);
  console.log(JSON.stringify(message));

  var messageId = message.mid;

  // You may get a text or attachment but not both
  var messageText = message.text;
  var messageAttachments = message.attachments;

  if (messageText && !message.is_echo) 

    // If we receive a text message, check to see if it matches any special
    // keywords and send back the corresponding example. Otherwise, just echo
    // the text we received.
    switch (messageText) 
      case 'image':
        sendImageMessage(senderID);
        break;

      case 'button':
        sendButtonMessage(senderID);
        break;

      case 'generic':
        sendGenericMessage(senderID);
        break;

      case 'receipt':
        sendReceiptMessage(senderID);
        break;

      default:
        sendTextMessage(senderID, messageText);
    
   else if (messageAttachments) 
    sendTextMessage(senderID, "Message with attachment received");
  




function sendGenericMessage(recipientId) 
  var messageData = 
    recipient: 
      id: recipientId
    ,
    message: 
      attachment: 
        type: "template",
        payload: 
          template_type: "generic",
          elements: [
            title: "rift",
            subtitle: "Next-generation virtual reality",
            item_url: "https://www.oculus.com/en-us/rift/",               
            image_url: "http://messengerdemo.parseapp.com/img/rift.png",
            buttons: [
              type: "web_url",
              url: "https://www.oculus.com/en-us/rift/",
              title: "Open Web URL"
            , 
              type: "postback",
              title: "Call Postback",
              payload: "Payload for first bubble",
            ],
          , 
            title: "touch",
            subtitle: "Your Hands, Now in VR",
            item_url: "https://www.oculus.com/en-us/touch/",               
            image_url: "http://messengerdemo.parseapp.com/img/touch.png",
            buttons: [
              type: "web_url",
              url: "https://www.oculus.com/en-us/touch/",
              title: "Open Web URL"
            , 
              type: "postback",
              title: "Call Postback",
              payload: "Payload for second bubble",
            ]
          ]
        
      
    
  ;  

  callSendAPI(messageData);


function receivedPostback(event) 
  var senderID = event.sender.id;
  var recipientID = event.recipient.id;
  var timeOfPostback = event.timestamp;

  // The 'payload' param is a developer-defined field which is set in a postback 
  // button for Structured Messages. 
  var payload = event.postback.payload;

  console.log("Received postback for user %d and page %d with payload '%s' " + 
    "at %d", senderID, recipientID, payload, timeOfPostback);

  // When a postback is called, we'll send a message back to the sender to 
  // let them know it was successful
  sendTextMessage(senderID, "Postback called");


function sendTextMessage(recipientId, messageText) 
  var messageData = 
    recipient: 
      id: recipientId
    ,
    message: 
      text: messageText
    
  ;

  callSendAPI(messageData);


function callSendAPI(messageData) 
  request(
    uri: 'https://graph.facebook.com/v2.6/me/messages',
    qs:  access_token: PAGE_ACCESS_TOKEN ,
    method: 'POST',
    json: messageData

  , function (error, response, body) 
    if (!error && response.statusCode == 200) 
      var recipientId = body.recipient_id;
      var messageId = body.message_id;

      console.log("Successfully sent generic message with id %s to recipient %s", 
        messageId, recipientId);
     else 
      console.error("Unable to send message.");
      console.error(response);
      console.error(error);
    
  );  




module.exports = app; 

【讨论】:

您在github.com/Microsoft/BotBuilder/issues/2274之前是否完成了帐号关联【参考方案5】:

确保您使用了正确的收件人 ID。 Recipient 不是页面或应用 ID,而是(如果您想向用户发送消息)用户的 ID。

您可以配置https://requestb.in等webhook并检查来自messanger的请求,您要查找的ID是:

"sender":"id":"1499894473423552"

【讨论】:

【参考方案6】:

我删除了事件中的消息回显,只选择了消息和回发,它解决了问题

【讨论】:

考虑在您的答案中添加更多信息以便更好地理解

以上是关于使用 facebook messenger bot 示例教程代码时,我收到消息:'(#100) No matching user found'的主要内容,如果未能解决你的问题,请参考以下文章

“正在输入...”指示在 bot 框架 + facebook Messenger 中不会消失

java的Facebook Messenger bot对象结构

Facebook Messenger Bot 使用回发按钮共享结构化消息

托管 Facebook Messenger Bot 代码的最佳位置是啥? [关闭]

使用 facebook messenger bot 示例教程代码时,我收到消息:'(#100) No matching user found'

Facebook Messenger 如何与 Wit.ai Bot Engine 连接?