代码之家  ›  专栏  ›  技术社区  ›  xRobot

Facebook Messenger:如何使用nodejs发送多条消息

  •  0
  • xRobot  · 技术社区  · 7 年前

    这是我的代码(我在下面隐藏了PSID):

    app.get('/helloguys', function (req, res) {
    
      var messageData = {
        batch: [
          {recipient: {id: "..."}},{recipient: {id: "..."}}
        ],
        message: {
          text: "Hi guys :)",
          metadata: "DEVELOPER_DEFINED_METADATA"
        }
      };
    
      request({
        uri: 'https://graph.facebook.com/v2.6/me/messages',
        qs: { access_token: token },
        method: 'POST',
        json: messageData
    
      }, function (error, response, body) {
           if (!error && response.statusCode == 200) {
             console.log("Ok", response.statusCode);
           } else {
             console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
           }
      });  
    
      res.send('Hi :)')
    
    })
    

    Ok 200
    

    但是用户没有收到消息。

    为什么?

    编辑Lix:

    body: 
    2017-10-18T13:38:43.538998+00:00 app[web.1]:    [ { code: 400,
    2017-10-18T13:38:43.538999+00:00 app[web.1]:        headers: [Object],
    2017-10-18T13:38:43.538999+00:00 app[web.1]:        body: '{"error":{"message":"Unsupported get request. Please read the Graph API documentation at https:\\/\\/developers.facebook.com\\/docs\\/graph-api","type":"GraphMethodException","code":100,"error_subcode":33,"fbtrace_id":"Dd6+kHN7Tl+"}}' },
    

    CBroe编辑:

      var messageData = {
        batch: [
          {method:"POST", message: "Hello", recipient: {id: "1552389158161227"}},{method:"POST", message: "Hello", recipient: {id: "1419003191530571"}}
        ]
      };
    

    它不起作用

    CBroe 2的编辑:

    app.get('/helloguys', function (req, res) {
    
      var batch = [
          {method:"POST", body: "message=Test status update&recipient=..."},
          {method:"POST", body: "message=Test status update&recipient=..."}
        ];
    
      request({
        uri: 'https://graph.facebook.com/v2.6/me/messages',
        qs: { access_token: token },
        method: 'POST',
        json: batch
    
      }, function (error, response, body) {
           if (!error && response.statusCode == 200) {
             console.log("ok", response.statusCode, response.statusMessage, response);
             res.send('hi')
           } else {
             console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
           }
      });  
    
    })
    

    2017-10-18T15:36:05.981999+00:00 app[web.1]: Failed calling Send API 400 Bad Request { message: '(#100) The parameter recipient is required',
    2017-10-18T15:36:05.982009+00:00 app[web.1]:   type: 'OAuthException',
    2017-10-18T15:36:05.982010+00:00 app[web.1]:   code: 100,
    2017-10-18T15:36:05.982011+00:00 app[web.1]:   fbtrace_id: 'EJLQgP9UoMT' }
    
    2 回复  |  直到 7 年前
        1
  •  1
  •   razdva12    7 年前

    FB dev docs (他们在文件中的某个地方进行了说明,但不是很明显)

    对于body参数

    此外,多个 岗位 请求(批量请求):

    虽然GET和DELETE操作只能有一个相对url和一个方法字段,但POST和PUT操作可能包含一个可选的url 领域

    和gt&燃气轮机;

    应将其格式化为 原始HTTP POST正文字符串

    此外,此类请求应作为多部分/表单数据进行。

    然后,批量请求要求是: (1) 是多部分/表格数据; (2) 由URL编码字符串组成 '正文' 参数 (3) 批处理请求应为原始HTTP POST正文字符串。

    节点。js公司 要求 表单数据 模块)。看见 docs

    因此,您的代码应该是这样的>&燃气轮机;

    app.get('/helloguys', function (req, res) {
    
      var batchUrl = 'https://graph.facebook.com';
      var r = request.post(batchUrl, function(error, response, body) {
        if (error) {return console.log("error\n", error)};
        console.log("successfull\n", body) //but fb sends error in body message, so check body.error for any errors
      });
    
      var form = r.form();
      var multipleMessages = [];
      var message = "message=" + encodeURIComponent(JSON.stringify(
              {
                "text": "​Hi guys :)"
              }
      ));
    
      //loop throught user IDs (PSIDs)
      for (var i=0; i<users; i++) {
        var recipient = "recipient=" + encodeURIComponent(JSON.stringify({"id": users[i].id}));
    
        var batchMessage = {
          "method": "POST",
          "relative_url":"v2.6/me/messages",
          "body": recipient + "&" + message
        };
        multipleMessages.push(batchMessage);
      }
    
      form.append("access_token", token)
      form.append("batch", JSON.stringify(multipleMessages)); 
    
      res.send('Hi :)')
    
    })
    
        2
  •  -1
  •   VMois    7 年前

     function (error, response, body) {
           if (!error && response.statusCode == 200) {
             console.log("Ok", response.statusCode);
             res.send('Hi');
           } else {
             console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error);
           }
      });