2017-04-25 112 views
1

我製作了一個Api.Ai機器人並將它與Slack和Facebook Messenger集成在一起。當我寫信給它時,它回答了在Api.Ai中爲Slack和Facebook Messenger設置的響應,但在履行部分,當Api.Ai打電話給我的服務時,它在Slack中工作正常,但是我得到Facebook Messenger沒有迴應。無法讓我的Facebook messenger機器人響應我的服務回覆

我從我的服務回報消息的格式:

{ 
"contextOut": [ 
    { 
     "lifespan": 2, 
     "name": "weather", 
     "parameters": { 
      "city": "Rome" 
     } 
    } 
], 
"data": { 
    "facebook": { 
     "message": { 
      "text": "Great success!" 
     }, 
     "recipient": { 
      "id": "1454102654663349" 
     } 
    }, 
    "slack": { 
     "attachments": [ 
      { 
       "color": "#00A399", 
       "title": "Hello world!", 
       "title_link": "https://www.mywebsite.se" 
      } 
     ], 
     "text": "Horray! Great success! :)" 
    } 
}, 
"displayText": "Whatever!!", 
"followupEvent": { 
    "followupEvent": { 
     "data": { 
      "parameter": "<parameter_value>" 
     }, 
     "name": "<event_name>" 
    } 
}, 
"source": "mywebsite.se", 
"speech": "Whatever!?" 
} 

Facebook的收件人ID來自於我的服務提出的要求。

request.result.contexts[0].parameters.Facebook_sender_id 

我已經在Facebook應用程序的產品設置選項卡下驗證了我的webhook。
我已經使用我的頁面訪問令牌將我的應用程序訂閱到該頁面。
我已經在webhooks中檢查了以下事件:messages,messaging_postbacks
我以Facebook的應用程序的管理員用戶身份登錄。

我出來的想法,必須有我錯過了什麼?

編輯: 我設置了一個Azure函數作爲我的webhook用於測試目的。

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log) 
{ 
    var request = await req.Content.ReadAsAsync<ApiAiMessage>(); 

    log.Info($"Incoming: {JsonConvert.SerializeObject(request)}"); 

    var slack_message = new { 
     text = $"Horray! Great success! :)", 
     attachments = new[] { 
      new { 
        title = "Hello world!", 
        title_link = "https://www.mywebsite.se", 
        color = "#00A399" 
       } 
      } 
     }; 

    var facebook_message = new { 
      recipient = new { 
       id = $"{request.result.contexts[0].parameters.Facebook_sender_id}" 
       }, 
      message = new { 
        text = "Great success!" 
       } 
      }; 

    var response = new 
    { 
     data = new 
      { 
       facebook = facebook_message, 
       slack = slack_message 
      }, 
     speech = "Whatever!?", 
     displayText = "Whatever!!", 
     contextOut = new[] { 
      new { 
        name = "weather", 
        lifespan = 2, 
        parameters = new { 
         city = "Rome" 
        } 
       } 
      }, 
     source = "mywebsite.se", 
     followupEvent = new { 
      followupEvent = new { 
       name = "<event_name>", 
       data = new { 
        parameter = "<parameter_value>" 
       } 
      } 
     } 
    }; 

    log.Info($"Outgoing: {JsonConvert.SerializeObject(response)}"); 

    return req.CreateResponse(HttpStatusCode.OK, response, new MediaTypeHeaderValue("application/json")); 
} 
+0

你是如何實際發出請求給Facebook與API.ai迴應?如果問題在於您正在使用一些未粘貼的代碼將響應發送給FB並且發生了中斷,那麼以上代碼對於診斷問題不是很有幫助。 另外,你有沒有在郵差測試?你確定你正在發送帶有正確的FB頁面令牌和參數的請求嗎? –

+0

用郵差發送請求可以正常工作,並且與Slack一樣。 Bot得到了貼在我的問題上的答覆。我將傳出的消息記錄在我的服務中,並可以在發送來自Facebook Messenger的請求時看到相同的響應。唯一的區別是Facebook的收件人ID顯然不是在那裏做其他來源比Facebook的請求。 我用我正在使用的Azure函數編輯了我的問題。 – mrapan

+0

你在哪裏發送FB令牌給信使? –

回答

0

你究竟在哪裏發送API.ai回覆給Facebook Messenger?這需要一些FB特定的內容,比如'頁面令牌'和它對Slack起作用的事實,但不是FB讓我相信這只是一些簡單的事情而已。下面是什麼JavaScript的一個例子調用看起來像

function callSendAPI(messageData) { 
 
\t request({ 
 
\t \t uri: 'https://graph.facebook.com/v2.6/me/messages', 
 
\t \t qs: { 
 
\t \t \t access_token: config.FB_PAGE_TOKEN 
 
\t \t }, 
 
\t \t method: 'POST', 
 
\t \t json: messageData 
 

 
\t }, function (error, response, body) { 
 
\t \t if (!error && response.statusCode == 200) { 
 
\t \t \t var recipientId = body.recipient_id; 
 
\t \t \t var messageId = body.message_id; 
 

 
\t \t \t if (messageId) { 
 
\t \t \t \t console.log("Successfully sent message with id %s to recipient %s", 
 
\t \t \t \t \t messageId, recipientId); 
 
\t \t \t } else { 
 
\t \t \t \t console.log("Successfully called Send API for recipient %s", 
 
\t \t \t \t \t recipientId); 
 
\t \t \t } 
 
\t \t } else { 
 
\t \t \t console.error("Failed calling Send API", response.statusCode, response.statusMessage, body.error); 
 
\t \t } 
 
\t }); 
 
}

+0

這是向Facebook API發佈POST的格式。我的問題是關於通過API.AI從Facebook API接收一篇文章到我的服務,並形成正確的響應。對不起,如果我不清楚這一點。 – mrapan

相關問題