2017-04-25 92 views
0

我正在使用api.ai和webhook visual studo 2015 c#。如何在c中使用webhook獲取用戶信息#

我爲某些意圖創建了一些操作,現在我正在尋找名爲「welcome.input」的操作。我想獲取用戶的用戶名。 如果用戶第一次開始與機器人進行對話,我想給他提供查看幫助菜單或標準菜單的可能性, 並且當用戶在機器人中重新輸入時我想發送文本:歡迎回來{用戶名}並向他展示標準菜單。

你有什麼想法如何做到這一點。

我正在閱讀https://github.com/Microsoft/BotBuilder-Samples/tree/master/CSharp/core-State這個例子......但我不能在我的項目中作爲webhook添加適配。

using Newtonsoft.Json; 
using Newtonsoft.Json.Linq; 
using System; 
using System.Collections.Generic; 
using System.Diagnostics.Contracts; 
using System.Linq; 
using System.Net; 
using System.Net.Http; 
using System.Threading.Tasks; 
using System.Web; 
using System.Web.Http; 
using System.Web.Mvc; 
using WebhookReceiver.Models; 

    namespace FBReceiver.Controllers 
    { 

public class facebookController : ApiController 
{ 

    public string Get() 
    { 
     return "OK"; 
    } 


    public int Get(int id) 
    { 
     return id; 
    } 



    public ApiAiResponse Post([FromBody]JObject jsonRequest) 
    { 
     using (FbReceiverModelDataContext ctx = new 
    FbModelDataContext()) 
     { 
      ctx.spTblTransactions_CreateNew("xyz", "Request", 
    jsonRequest.ToString(), HttpContext.Current.User.Identity.Name); 

      ApiAiRequest request = jsonRequest.ToObject<ApiAiRequest>(); 

      ApiAiResponse response = new ApiAiResponse(); 

      JObject jObject = JObject.Parse(request.result.parameters.ToString()); 
      string xyznumber = (string)jObject["xyznumber"] != null ? (string)jObject["xyznumber"] : ""; 

      string otherparameter = (string)jObject["otherparameter"] != null ? (string)jObject["otherparameter"] : ""; 

      if (("action1".Equals(request.result.action.ToLower()))) 
      { 
       tbla a= new tbla(); 
       a= ctx.tblAa.SingleOrDefault(u => u.a.ToLower() == a.ToLower()); 

       if (a!= null) 
       { 
        response.speech = "a with number " + xyznumber+ " " + a.aaaa; 

        response.source = "aaa"; 
       } 
       else if (!string.IsNullOrEmpty(xyznumber)) 
       { 
        response.speech = "Generic info about " + xyznumber; 
        response.displayText = "Generic info about " + xyznumber; 
        response.source = "aaaa"; 
       } 
       else 
       { 
        response.speech = "No info"; 
        response.displayText = "No info"; 
        response.source = "Parcels"; 
       } 
      } 



      else if (("pay.info".Equals(request.result.action.ToLower()))) 
      { 
       ///yyyyyyyyyyyyyyyyyyyyyyyyyyyyy 
      } 


      else if (("welcome.input".Equals(request.result.action.ToLower()))) 
      { 

       // to do 

      } 


      else 
      { 
       response.speech = "something is wrong ????"; 
       response.displayText = "something is wrong ????"; 
       response.source = "None"; 
      } 




      ctx.spTblTransactions_CreateNew("aaaa", "Response", JsonConvert.SerializeObject(response), HttpContext.Current.User.Identity.Name); 

      return response; 
     } 
    } 


    } 
} 

請幫幫我。我曾多次尋找關於這個話題

+0

目前還不清楚有什麼問題。你使用bot框架嗎?爲什麼你不能使用註釋的代碼? –

+0

我使用Api.ai和webhook我使用visual studio 2015,C#。評論的代碼是不好的...我想你的幫助確實得到用戶bot的用戶名,並在消息中使用它 – user2454923

+0

所以你沒有使用bot框架?仍然不清楚。代碼在哪裏運行?如果它不是一個殭屍框架的問題,請刪除標籤 –

回答

0

所以你的問題是有點模糊,但在概念層面是什麼,你需要做的是

  1. 存儲一些獨特的用戶或會話ID從消息集成未來像facebook messenger
  2. 使用某種鍵值映射或用於數據庫中更可行的生產選項的userID保存關聯名稱。請注意,如果您想要檢查自上次消息開始經過一段時間後是否根據消息的時間間隔做了某種「歡迎消息」,則還需要存儲最新消息的時間戳
  3. 在消息上, ,檢查用戶是否存在,如果不是它的一個全新的用戶並提示輸入名稱,如果是看跑時間戳檢查,如果一些時間遠離

這裏後,回來在JavaScript的一個例子來說明這一方法本身,在那裏你在收到來自用戶的消息並且在將其傳遞給API.ai之前將呼叫該功能:

 function setSessionAndUser(messageEvent, callback) { 
 
      var senderID = messageEvent.sender.id; 
 
      var firstTimeUser = false 
 
      if (!sessionIds.has(senderID)) { 
 
       sessionIds.set(senderID, uuid.v1()); 
 
      } 
 

 
      if (!usersMap.has(senderID)) { 
 
       firstTimeUser = true 
 
       database.userData(function (user) { 
 
        usersMap.set(senderID, user); 
 
        //Note this is not touching the database and is instead temporarily storing users as a Map in server memory, it's a tradeoff of waiting to touch the DB before replying to every message vs how permanent you need the data to be (and how much data you'll be dealing with) 
 
        callback(messageEvent, firstTimeUser) 
 
       }, senderID); 
 
      } else{ 
 
       callback(messageEvent, firstTimeUser) 
 
      } 
 
     }

編輯補充算例 '捕' 消息事件,然後把它們解析:

//Post to webhook to catch messages 
 
app.post('/webhook/', function (req, res) { 
 
    var data = req.body; 
 

 
    // Make sure this is a page subscription 
 
    if (data.object == 'page') { 
 
     // Iterate over each entry 
 
     // There may be multiple if batched 
 
     data.entry.forEach(function (pageEntry) { 
 
      var pageID = pageEntry.id; 
 
      var timeOfEvent = pageEntry.time; 
 

 
      // Iterate over each messaging event 
 
      pageEntry.messaging.forEach(function (messagingEvent) { 
 
       if (messagingEvent.message) { 
 
        receivedMessage(messagingEvent); 
 
       } 
 
       //Catch all 
 
       else { 
 
        console.log("Webhook received unknown messagingEvent: ", messagingEvent); 
 
       } 
 
      }); 
 
     }); 
 

 
     //Must return a 200 status 
 
     res.sendStatus(200); 
 
    } 
 
}); 
 

 
// Parsing the messageEvent 
 
function receivedMessage(event) { 
 
    var senderID = event.sender.id; 
 
    //... 
 
}

+0

我可以在其他方面使用此功能if( 「welcome.input」.Equals(request.result.action.ToLower()))) { }或者我可以在這裏調用這個函數?對不起,但我在編程,所以我提出如此基本的問題begginer。 – user2454923

+0

對不起,我還不清楚如何使用以及如何調用這個函數來獲取用戶名:( – user2454923

+0

所以你的用戶名或任何唯一的用戶標識符,你想存儲提供的名稱將最終來自郵件的發件人嗎?也就是說Facebook Messenger會將用戶的唯一ID號碼與他們信息的實際主體一起發送出去,所以首先你需要'捕捉'這些信息,然後從身體中解析出ID。 我正在編輯添加的答案示例 –

相關問題