【问题标题】:How to show personalized 'Welcome Message' in facebook messenger?如何在 Facebook Messenger 中显示个性化的“欢迎信息”?
【发布时间】:2016-10-23 23:19:39
【问题描述】:

我想显示“欢迎信息”,例如“嗨罗伯特,欢迎使用我的应用程序”。 所以我需要发送:

  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. 使用第一个请求中的“first_name”,发送“https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token=PAGE_ACCESS_TOKEN”请求以设置“欢迎消息”。

但是,我需要在第一次请求之前知道 user_id。

我的问题:

  1. 创建“欢迎信息”的步骤是什么?
  2. 如何在用户打开 facebook messenger 窗口时显示“欢迎消息”?

我查看了https://developers.facebook.com/docs/messenger-platform 文档,但仍有疑问。

【问题讨论】:

    标签: facebook facebook-graph-api facebook-messenger


    【解决方案1】:

    你不需要它。只需按照以下步骤,

    1- 转到与您的 Messenger api 相关的页面。

    2-设置(FB页面设置)

    3- 从选项卡中选择消息传递

    4- 在底部的消息选项卡中,您将看到“显示 Messenger Greeting”将其从“否”更改为“是”。比你可以自定义它;)

    注意:要查看您的问候语,您应该删除您之前与您设置问候语的页面的对话。然后与您应该看到问候消息的页面开始新的对话。

    【讨论】:

    • 谢谢你的回答,我也试过这个选项。但这对我不起作用。我输入我的消息并设置“是”以显示并单击“保存按钮”。不幸的是,它不起作用(。也许我错过了一些设置?
    • 您的应用是否获得批准?如果现在没有,则只有管理员、测试人员和开发人员可以看到该消息。而且您可以像这样检查它,在 Messenger 应用程序设置中将朋友添加到您的测试组。打开对话窗口后应该会出现消息。
    • 您的说明的第 1 步完全不清楚您的意思。我在developers.facebook 中的App Dashboard 中搜索了所有页面......我没有找到Greeting 的设置:(
    • 例如我的页面名称是:KayzerSoze-1795503650697564 设置网址:facebook.com/KayzerSoze-1795503650697564/settings/… 只需将“KayzerSoze-1795503650697564”更改为您的页面用户名。
    • @iBobb 不在 App Dashboard 中,在 FB Page Settings 中
    【解决方案2】:

    此时创建问候文本一点也不难。我花了一些时间找出如何做到这一点,结果很简单。我重构了我在示例中找到的 POST 请求的一种方法,并且我正在使用 NodeJS 顺便说一句。

    function createGreetingApi(data) {
    request({
    uri: 'https://graph.facebook.com/v2.6/me/thread_settings',
    qs: { access_token: PAGE_ACCESS_TOKEN },
    method: 'POST',
    json: data
    
    }, function (error, response, body) {
    if (!error && response.statusCode == 200) {
      console.log("Greeting set successfully!");
    } else {
      console.error("Failed calling Thread Reference API", response.statusCode,     response.statusMessage, body.error);
    }
    });  
    }
    

    然后我有另一种方法来保持代码可读性:

    function setGreetingText() {
    var greetingData = {
    setting_type: "greeting",
    greeting:{
    text:"Hi {{user_first_name}}, welcome!"
    }
    };
    createGreetingApi(greetingData);
    }
    

    那么你在app.listen中使用这个方法如下:

    app.listen(app.get('port'), function() {
    console.log('Node app is running on port', app.get('port'));
    setGreetingText();
    });
    

    【讨论】:

      【解决方案3】:

      所以我猜在 messenger 中,第一条消息就会发生这种情况。如果您删除消息并重新开始,则不会触发。在 Facebook 消息中,您会看到它在工作。

      【讨论】:

        【解决方案4】:

        从 9 月 12 日起,您可以设置在用户点击“开始”之前显示的问候文本,其中包括在文本呈现时将被替换的这些标记:

        • {{user_first_name}}
        • {{user_last_name}}
        • {{user_full_name}}

        查看 Messenger 平台文档的此页面: https://developers.facebook.com/docs/messenger-platform/thread-settings/greeting-text

        【讨论】:

          【解决方案5】:

          因此,您可以使用"get started" 按钮进行此操作。仅当用户第一次向机器人发送消息时,此按钮才会出现。

          通过这个命令你可以设置按钮:

          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"      
          

          如您所见,如果按钮被按下,您的 Webhook 会收到“回发”

          这是你得到的回调:

          {
            "sender":{
              "id":"USER_ID"
            },
            "recipient":{
              "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;
          
            // 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");
                 }
          

          我的 startedConv() 看起来像这样

          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 ? ")
                    }
                });
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-10-20
            • 2020-07-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-09-23
            • 1970-01-01
            相关资源
            最近更新 更多