在DialogFlow中访问Facebook Messenger用户配置文件API [英] Access Facebook Messenger User Profile API in DialogFlow

查看:87
本文介绍了在DialogFlow中访问Facebook Messenger用户配置文件API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Google的DialogFlow中建立一个跨平台的聊天机器人。我想访问Facebook用户配置文件API以了解用户的名字。

I'm building a cross-platform chatbot in Google's DialogFlow. I'd like to access the Facebook User Profile API to learn the user's first name.

我正在努力寻找有关如何(或是否)可以做到这一点的建议

I'm struggling to find advice on how (or if) I can make this happen.

https://developers.facebook.com/docs/messenger-platform/identity/user-profile/

这里有人能做到吗?

推荐答案

您需要致电 Facebook Graph API 以获得用户的个人资料。

You need to make a call to Facebook Graph API in order to get user's profile.

Facebook为此提供了一些SDK,但是他们提供了一些SDK。官方JavaScript SDK更适合用于Web客户端,而不是服务器。他们在该链接上提到了一些第三方Node.js库。我特别使用 fbgraph (在在撰写本文时,它似乎是唯一被保留的一种。)

Facebook offers some SDKs for this, but their official JavaScript SDK is more intended to be on a web client, not on a server. They mention some 3rd party Node.js libraries on that link. I'm particularly using fbgraph (at the time of writing, it's the only one that seems to be "kind of" maintained).

因此,您需要一个Page Token来进行调用。在开发时,您可以从这里获得一个:
https://developers.facebook.com/apps/<您的应用程序ID> / messenger / settings /

So, you need a Page Token to make the calls. While developing, you can get one from here: https://developers.facebook.com/apps/<your app id>/messenger/settings/

下面是一些示例代码:

const { promisify } = require('util');
let graph = require('fbgraph'); // facebook graph library

const fbGraph = {
  get: promisify(graph.get)
}

graph.setAccessToken(FACEBOOK_PAGE_TOKEN);  // <--- your facebook page token
graph.setVersion("3.2");

// gets profile from facebook
// user must have initiated contact for sender id to be available
// returns: facebook profile object, if any
async function getFacebookProfile(agent) {
  let ctx = agent.context.get('generic');
  let fbSenderID = ctx ? ctx.parameters.facebook_sender_id : undefined;
  let payload;

  console.log('FACEBOOK SENDER ID: ' + fbSenderID);

  if ( fbSenderID ) {
    try { payload = await fbGraph.get(fbSenderID) }
    catch (err) { console.warn( err ) }
  }

  return payload;
}

请注意,您并非总是可以访问发件人ID,以防万一确实如此,您并不总是有权访问该个人资料。对于电子邮件之类的某些字段,您需要请求特殊权限。如果用户是发起对话的人,则通常会使用诸如姓名和个人资料图片之类的常规字段。更多信息此处

Notice you don't always have access to the sender id, and in case you do, you don't always have access to the profile. For some fields like email, you need to request special permissions. Regular fields like name and profile picture are usually available if the user is the one who initiates the conversation. More info here.

希望有帮助。

编辑

承诺而不是异步:

function getFacebookProfile(agent) {
  return new Promise( (resolve, reject) => {
    let ctx = agent.context.get('generic');
    let fbSenderID = ctx ? ctx.parameters.facebook_sender_id : undefined;

    console.log('FACEBOOK SENDER ID: ' + fbSenderID);

    fbGraph.get( fbSenderID )
      .then( payload => {
        console.log('all fine: ' + payload);
        resolve( payload );
      })
      .catch( err => {
        console.warn( err );
        reject( err );
      });
  });
}

这篇关于在DialogFlow中访问Facebook Messenger用户配置文件API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆