代码之家  ›  专栏  ›  技术社区  ›  realnsleo

使用Dialogflow代理从Messenger Webview检索回发

  •  0
  • realnsleo  · 技术社区  · 6 年前

    我被困在这里了。我试图从Facebook Messenger聊天机器人中打开的一个网络视图中获得回帖,我正在使用NodeJS开发DialoFlow的实现库。

    我可以发送一个有效负载,打开一个特定的URL,如下所示:

    {
      "facebook": {
        "attachment": {
          "type": "template",
          "payload": {
            "template_type": "button",
            "text": "So you want to open the webview huh?",
            "buttons": [{
                "type": "web_url",
                "url": "https://somewebsiteurlwithdataiwanttoget.como",
                "title": "Open Website",
                "messenger_extensions": true  // To get psid and close window event
              }]
          }
        }
      }
    }
    

    在我的webview中,我可以提交一个表单并从该表单中获取数据 使用jQuery Ajax:

    let jqxhr = $.ajax({
        url: '/webhook',  // Fires my webhook
        data: { var1: 'Hello', var2: 'World' }, // Sent to my webhook
        dataType: 'json'
    });
    

    在我的webook中,我初始化我的代理,并使用自定义事件将这些数据发送回Messenger Bot(注:我正在使用Express)。

    // The webhook that receives post data from the form in my webview
    router.post('/', function (req, res, next) {
      // Initialize Agent
      const agent = new WebhookClient({ request: req, response: res })
    
      // Handle the intent
      let intentMap = new Map()
    
      // Set default handle if there are no intents
      intentMap.set(null, handle)
    
      // Handle stuff from the form
      agent.handleRequest(intentMap)
    
      function handle (agent) {
        agent.add(`Just a test to see if this message gets to messenger`)
      }
    })
    

    但是,我的控制台中出现了一个错误 This request is not a valid Dialogflow request ". 我不确定我做错了什么,我希望有人能帮我。

    非常感谢。

    0 回复  |  直到 6 年前
        1
  •  1
  •   Sajid Rahman    5 年前

    发送json { var1: 'Hello', var2: 'World' } 问题就在这里。 WebhookClient要求'req'参数遵循一个架构,可以如下所示:

     {
        "responseId": "e72a8020-1051-489d-acb4-95c9ebeadcb7-ee1dc704",
        "queryResult": {
        "queryText": "view appointment",
        "parameters": {
        },
        "allRequiredParamsPresent": true,
        "fulfillmentText": "Hi here you go.",
        "fulfillmentMessages": [
         .
         .
        ],
        "intent": {
          "name": "projects\/proj1\/agent\/intents\/77f38791-f2da-41bd-b44f-cef190d26fd9",
          "displayName": "2-show-appointments"
        },
        "intentDetectionConfidence": 1,
        "languageCode": "en"
      },
      "originalDetectIntentRequest": {
        "source": "GOOGLE_TELEPHONY",
        "payload": {
          "telephony": {
            "caller_id": "Anonymous"
          }
        }
      },
      "session": "projects\/proj1\/iKawldQ1RFSBIckQfGKww"
    }
    

    它包含有关接收到的消息、检测到的意图和实现负载的信息。您无权更改架构。

    而且,这种方法是不可行的。根据 this ,dialogflow没有messenger平台中webview事件的回调。

    推荐文章