首页 文章

如何在facebook messenger bot中设置“开始使用”按钮以及何时发送欢迎信息

提问于
浏览

9 回答

  • 2

    对API JSON主体进行POST调用,如下所示 .

    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"
    

    参考:Facebook messenger get started button

    Postman screentshot

  • 2

    您可以成功设置它,但是没有看到它,因为您已经与Facebook页面进行了现有对话 .

    成功设置“Get Started”主题后,只有在删除现有会话主题并开始新主题时才会看到它 .

    只有在您第一次与Facebook页面交互时才会显示“开始”按钮,因此,如果您之前已向该页面发送消息,除非您从Facebook Messenger客户端删除该主题,否则您将无法看到“开始使用”(无论是移动还是桌面) .

    来自FB Messenger Docs:

    在某些条件下可以看到欢迎屏幕和“入门”按钮:它们仅在用户第一次与“页面上的信息交互”呈现时应用程序的管理员/开发者/测试人员可以在应用程序处于开发模式时看到它您的应用必须订阅您的webhook上的回发

  • 2

    目前的格式是,https://graph.facebook.com/v2.6/me/messenger_profile?access_token=PAGE_ACCESS_TOKEN

    {“get_started”:{“payload”:“GET_STARTED_PAYLOAD”}}

  • 0

    感谢有 Value 的评论,经过一些解决方法发现这个解决方案工作,根据Facebook指南

    需要仅向ONCE发送一个独立的POST请求到此URL

    https://graph.facebook.com/v2.6/PAGE_ID/thread_settings?access_token=PAGE_ACCESS_TOKEN

    用邮递员发送这样的请求here

    如果成功设置了“入门”按钮,您将收到以下响应

    {
      "result": "Successfully added new_thread's CTAs"
    }
    
  • 0

    你必须运行一个适当的curl命令来设置它 . 检查这个链接,看看他们的例子 . https://developers.facebook.com/docs/messenger-platform/implementation#send_api

  • 4

    npm中有一个库包含POST / DELETE操作的功能:https://www.npmjs.com/package/fb-get-started-button

    $ npm install -g fb-get-started-button
    
    $ fb-get-started-button add <YOUR PAGE ACCESS TOKEN>
    Adding "Get Started" button with the payload "GET_STARTED"
    Successfully added new_thread's CTAs
    
    $ fb-get-started-button remove <YOUR PAGE ACCESS TOKEN>
    Removing "Get Started" button
    Successfully deleted all new_thread's CTAs
    
  • 3

    使用您的页面访问令牌发送帖子请求

    https://graph.facebook.com/v2.6/me/messenger_profile?access_token=YOUR-TOKEN
    

    以下数据

    { 
      "get_started":{
         "payload":"<GET_STARTED_PAYLOAD>"
       }
    }
    

    Facebok Docs: Get Started Button

    希望这种新方法能解决您的问题 . 不要忘记首先使用Facebook Web删除已发送的消息以查看操作中的按钮 .

  • 14

    在我看来,更好的解决方案是使用Microsoft Bot Framework并使用其/ firstRun发送messenger get started按钮

    function firstRun(session) {
      console.log('This user is running our bot the first time')
      createUser(session)
      platforms.firstRun(session.message.user.id, session.message.address.channelId)
        .then((values) => {
          for (let value of values) {
            if (value.data.firstName && value.data.lastName) {
              session.userData.user.profile = value.data
            }
          }
        })
        .catch((errors => {
          console.log(errors);
        }))
      reply(session)
      session.endDialog()
    }
    

    platforms.firstRun看起来如下所示

    platforms.firstRun = function (userId, channel) {
        switch (channel) {
            case platforms.channels.emulator:
                return Promise.reject('none')
            case platforms.channels.facebook:
                return platforms.facebook.firstRun(userId)
            case platforms.channels.skype:
                return Promise.reject('none')
            default:
                return Promise.reject('none')
        }
    }
    

    这反过来调用platforms.facebook.firstRun

    platforms.facebook.firstRun = function (userId) {
        return Promise.all([
            platforms.facebook.sendThread(facebookTemplates.greet(), 'Greeting'),
            platforms.facebook.sendThread(facebookTemplates.getStarted(), 'Get Started'),
            platforms.facebook.sendThread(facebookTemplates.getPersistentMenu(), 'Persistent Menu'),
            platforms.facebook.sendThread(facebookTemplates.getDomainWhitelisting(), 'Domain Whitelisting'),
            platforms.facebook.getProfile(userId)
        ])
    }
    

    platforms.facebook.sendThread如下所示//调用Facebook图形api来更改机器人设置

    platforms.facebook.sendThread = function (template, cmd) {
    
        return new Promise((resolve, reject) => {
            // Start the request
            request({
                url: platforms.facebook.GRAPH_BASE_URI + '/me/thread_settings?access_token=' + endpoints.FACEBOOK_PAGE_ACCESS_TOKEN,
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                form: template
            },
                function (error, response, body) {
                    if (!error && response.statusCode == 200) {
                        // Print out the response body
                        resolve({ status: response.statusCode, data: body })
                    } else {
                        // TODO: Handle errors
                        reject({ status: response.statusCode, data: error })
                    }
                });
        })
    }
    

    注意facebookTemplates.getStarted(),它实际上有json for get started,如下所示

    templates.getStarted = function () {
        return {
            setting_type: "call_to_actions",
            thread_state: "new_thread",
            call_to_actions: [
                {
                    payload: payloads.FACEBOOK_GET_STARTED
                }
            ]
        }
    }
    

    完全可插拔的代码体系结构,用于在所有聊天机器人平台上执行首次运行操作 . 在我的机器人上完美运行HERE

  • 4

    在我们的例子中,以下工作:

    • 点击 thread_settings API

    https://graph.facebook.com/v2.6/me/thread_settings?access_token=<YOU FACEBOOK PAGE'S PAGE ACCESS TOKEN>

    • 传递了以下示例JSON
    {
          "setting_type": "call_to_actions",
          "thread_state": "new_thread",
          "call_to_actions": [
            {
              "payload": "Start"
            }
          ]
        }
    
    • API应给出以下结果:
    {
            "result": "Successfully added new_thread's CTAs"
        }
    

相关问题