如何在Facebook Messenger中展示个性化的“欢迎讯息”? [英] How to show personalized 'Welcome Message' in facebook messenger?

查看:179
本文介绍了如何在Facebook Messenger中展示个性化的“欢迎讯息”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想向欢迎来到我的应用程序显示欢迎信。
所以我需要发送:


  1. https://graph.facebook.com/v2.6/USER_ID?fields=first_name ,last_name,profile_pic,locale,时区,性别& access_token = PAGE_ACCESS_TOKEN


  2. 使用第一个请求中的first_name,发送 https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token= PAGE_ACCESS_TOKEN 请求设置'欢迎消息'。


但是,我需要先了解user_id请求。



我的问题:


  1. 创建欢迎消息'?

  2. 当用户打开facebook信使窗口时,如何显示欢迎讯息?

我revi已 https://developers.facebook.com/docs/messenger-platform 文档,以及我仍然有问题。

解决方案

所以你可以用开始按钮。这个按钮只有在用户第一次输入机器人时才会出现。



使用此命令可以设置按钮:

  curl  - X POST -HContent-Type:application / json-d'{
setting_type:call_to_actions,
thread_state:new_thread,
call_to_actions
{
payload:USER_DEFINED_PAYLOAD
}
]
}'https://graph.facebook.com/v2.6/me/thread_settings ?access_token = PAGE_ACCESS_TOKEN

正如您可以看到按钮是否按下您的Webhook收到回发



这是您收到的回调:

  {
发件人:{
id:USER_ID
},
收件人:{
id:PAGE_ID
},
timestamp:1458692752478,
postback:{
payload:USER_DEFINED_PAYLOAD
}
}

所以这是从你可以得到用户id



现在你可以为这个。
我的样子如下:

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

//'payload'参数是一个开发者定义的字段,它设置在结构化消息的回发
//按钮中。
var payload = event.postback.payload;

console.log(收到用户%d的回发和带有负载%s的页面%d%$ d,发件人ID,收件人ID,有效载荷,timeOfPostback);


如果(有效负载){

//当回调被调用时,我们将发送一条消息给发件人
//让他们知道这是成功的
switch(payload){
case'USER_DEFINED_PAYLOAD':
startedConv(senderID);
break;

默认值:
sendTextMessage(senderID,Postback called);
}

我的启动方式()看起来像这样



  function startedConv(recipientId){
var name;

请求({
url:'https://graph.facebook.com/v2.6/'+ recipientId +'?fields = first_name',
qs:{ access_token:PAGE_ACCESS_TOKEN},
方法:'GET'
},function(error,response,body){
if(error){
console.log(' :',error);
} else if(response.body.error){
console.log('Error:',response.body.error);
} else {
name = JSON.parse(body);
sendTextMessage(recipientId,Hello+ name.first_name +,我该如何帮你?)
}
});
}


I would like to show 'welcome message' like 'Hi Robert, welcome to my application'. So I need to send:

  1. "https://graph.facebook.com/v2.6/USER_ID?fields=first_name,last_name,profile_pic,locale,timezone,gender&access_token=PAGE_ACCESS_TOKEN"

  2. using 'first_name' from 1st request, send "https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token=PAGE_ACCESS_TOKEN" request to set 'welcome message'.

However, I need to know user_id before first request.

My questions:

  1. What are steps to create 'Welcome message'?
  2. How to show 'Welcome message', when user opened facebook messenger window?

I reviewed https://developers.facebook.com/docs/messenger-platform document, and I have still questions.

解决方案

So you can make this with a "get started" Button. This button is only there if the User Messages the bot for the first time.

With this command you can set the button:

curl -X POST -H "Content-Type: application/json" -d '{
  "setting_type":"call_to_actions",
  "thread_state":"new_thread",
  "call_to_actions":[
    {
      "payload":"USER_DEFINED_PAYLOAD"
    }
  ]
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=PAGE_ACCESS_TOKEN"      

As you can see if the Button is pressed your Webhook recieves a "postback"

this is the callback you get:

{
  "sender":{
    "id":"USER_ID"
  },
  "recipient":{
    "id":"PAGE_ID"
  },
  "timestamp":1458692752478,
  "postback":{
    "payload":"USER_DEFINED_PAYLOAD"
  }
}  

So this is from where you can get User id

Now you can code a Function for this. My looks like this:

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);


  if (payload) {

    // When a postback is called, we'll send a message back to the sender to 
    // let them know it was successful
      switch (payload) {
        case 'USER_DEFINED_PAYLOAD':
          startedConv(senderID);
          break;

       default:
         sendTextMessage(senderID, "Postback called");
       }

My startedConv() looks like this

function startedConv(recipientId){
var name;

request({
          url: 'https://graph.facebook.com/v2.6/'+ recipientId +'?fields=first_name',
          qs: {access_token: PAGE_ACCESS_TOKEN},
          method: 'GET'
      }, function(error, response, body) {
          if (error) {
              console.log('Error sending message: ', error);
          } else if (response.body.error) {
              console.log('Error: ', response.body.error);
          }else{
              name = JSON.parse(body);
              sendTextMessage(recipientId, "Hello "+ name.first_name+", how can i help you ? ")
          }
      });
}

这篇关于如何在Facebook Messenger中展示个性化的“欢迎讯息”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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