当用户与聊天机器人交互时获取 Facebook 用户详细信息
我是 Dialogflow 的新手,我想使用 webhooks 将其集成到 Facebook Messenger。问题是我不知道 GRAPH API 是如何工作的。我已经创建了我的聊天机器人并在控制台上对其进行了测试。它的工作原理如下:
User: book appointment
Bot: Ask for credentials (username, password, etc.)
验证后将其保存到我的数据库中。这样做的问题是,聊天机器人在请求更新约会状态时会询问用户密码。
我想集成到 Facebook Messenger,因为聊天机器人不会不断询问用户密码,因为它将使用 FB:ID
来验证用户帐户。
知道如何将其转换为代码吗?我正在使用nodejs来编写我的代码?
I'm new to Dialogflow and I wanted to integrate it to Facebook Messenger using webhooks. Problem is that I don't know how GRAPH API works. I already created my chatbot and tested it on the console. It works like this:
User: book appointment
Bot: Ask for credentials (username, password, etc.)
After validation it then saves it to my database. The problem with this is that the chatbot will ask the user password when asking an update for appointment status.
I wanted to integrate to Facebook Messenger because the chatbot won't be constantly asking for the user password as it will use the FB:ID
to verify the user account.
Any idea how to translate it to code? I'm using nodejs to write my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将答案发布为社区 wiki,因为此解决方案已经解决了该问题。基于 Graph Api 的解决方案发布在此答案上:
您需要致电Facebook Graph API 以获取用户的个人资料。
Facebook 为此提供了一些 SDK,但他们的官方 JavaScript SDK 更适合于 Web 客户端,而不是服务器。他们在该链接上提到了一些第三方 Node.js 库。我特别使用
fbgraph
(在撰写本文时,它是只有一个似乎是“某种”维护的)。因此,您需要一个页面令牌才能进行调用。在开发时,您可以从这里获取一个:
https://developers.facebook.com/apps//messenger/settings/
以下是一些示例代码:
请注意,您并不总是有权访问发件人 ID,并且在如果您这样做,您并不总是有权访问该个人资料。对于某些字段(例如
电子邮件
),您需要请求特殊权限。如果用户是发起对话的人,则通常可以使用姓名和个人资料图片等常规字段。更多信息请点击此处。编辑
Promise 而不是 async:
Post an answer as community wiki, as this solution has solved the issue. The solution based on Graph Api posted on this answer:
You need to make a call to Facebook Graph API in order to get user's profile.
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).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/
Here's some example code:
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.Edit
Promise instead of async: