ai物联网工程师_如何将Api.ai助手连接到物联网
ai物聯網工程師
If you’re keen to learn more on AI, check out our screencast Microsoft Cognitive Services and the Text Analytics API, for AI sentiment in your bot.
如果您想了解有關AI的更多信息,請查看我們的截屏視頻Microsoft Cognitive Services和Text Analytics API ,了解您機器人中的AI情緒。
The potential of a personal assistant gets exciting when it has access to personal data and the real world via the Internet of Things. New possibilities arise — from requesting your assistant turn on your lights to asking it how well you slept. We’ll be connecting your Api.ai assistant to the Jawbone Up API as an example of this.
當個人助理可以通過物聯網訪問個人數據和現實世界時,其潛力會變得令人興奮。 出現了新的可能性-從要求您的助手打開燈光到詢問您的睡眠狀況如何。 作為示例,我們將把您的Api.ai助手連接到Jawbone Up API。
Note: this article was updated in 2017 to reflect recent changes to Api.ai.
注意:本文于2017年更新,以反映對Api.ai的最新更改。
您需要什么 (What You’ll Need)
This article builds upon a variety of concepts we’ve already covered in previous articles here at SitePoint. In order to follow along with this tutorial comfortably, you’ll need the following.
本文基于SitePoint先前文章中已經介紹的各種概念。 為了舒適地遵循本教程,您需要以下內容。
An Api.ai agent connected to a simple HTML web app. See this article if you’d like to understand this process. Otherwise, you can download the code from this guide and use it too.
連接到簡單HTML Web應用程序的Api.ai代理。 如果您想了解此過程,請參閱本文 。 否則,您可以從本指南中下載代碼并使用它。
An agent that has been taught the entity of “sleep”. We created this in Empowering Your Api.ai Assistant with Entities. It should understand concepts like “how much sleep did I have last night?” and “how much REM sleep did I get?” If you’re looking to adapt this to your own IoT device, you’ll need to have created your own custom entity that understands your IoT functionality.
已被告知“睡眠”實體的代理。 我們是在通過實體為您的Api.ai助手賦權的過程中創建的。 它應該理解諸如“昨晚我睡了多少時間”之類的概念。 和“我獲得了多少REM睡眠?” 如果您想使其適應自己的物聯網設備,則需要創建自己的自定義實體,以了解您的物聯網功能。
A general knowledge of Node.js and running a Node server. Without it, you won’t be able to get the server running!
Node.js和運行節點服務器的一般知識。 沒有它,您將無法使服務器運行!
Knowledge of how to use the Jawbone UP API (or another API you intend to use). We’ve covered the Jawbone Up API previously in Connecting to the Jawbone Up API with Node.js, and I’ll be referring to sections from that article throughout.
了解如何使用Jawbone UP API(或您打算使用的其他API)。 之前,我們在使用Node.js連接到Jawbone Up API中已經介紹了Jawbone Up API,并且我將始終引用該文章的各個部分。
An SSL certificate to run your site on HTTPS. You’ll need this if working with the Jawbone Up API. As mentioned at the start of this series, overall it’s a bit easier to do a lot of this on HTTPS. We cover how to set up a self signed certificate on the Jawbone Up API article if you’re interested, but it’s not the easiest option these days. You can do it really easily using Let’s Encrypt as mentioned in the first article in the series. Glitch.com also provides a great prototyping environment that comes with HTTPS by default.
用于在HTTPS上運行您的站點的SSL證書。 如果使用Jawbone Up API,則需要此功能。 正如本系列文章開頭提到的,總體上,在HTTPS上執行許多操作要容易一些。 如果您有興趣,我們將在Jawbone Up API文章中介紹如何設置自簽名證書 ,但這不是當前最簡單的選擇。 您可以使用本系列第一篇文章中提到的“讓我們加密”來真正輕松地做到這一點。 默認情況下, Glitch.com還提供了HTTPS隨附的出色原型開發環境。
代碼 (The Code)
All code for this demo is available for you to download and use however you please! You can find it all on GitHub.
您可以下載和使用此演示的所有代碼,請您隨意使用! 您可以在GitHub上找到所有內容 。
運作方式 (How This Works)
Your Api.ai assistant is already connected to a simple web app that accepts statements via the HTML5 Speech Recognition API. From here, you need to add a new bit of functionality that listens for a specific action from your Api.ai agent. In your case, this is the action of “sleepHours”.
您的Api.ai助手已經連接到一個簡單的Web應用程序,該應用程序通過HTML5語音識別API接受語句。 從這里開始,您需要添加一些新功能,以偵聽來自Api.ai代理的特定操作。 在您的情況下,這是“ sleepHours”的動作。
Whenever your JavaScript detects this action, it triggers a separate call to your Node.js app to ask the Jawbone API for that data. Once the web app receives this data, your web app turns it into a nice sentence and reads it out —?giving your assistant a whole new range of intelligence!
每當您JavaScript檢測到此操作時,它都會觸發對Node.js應用的單獨調用,以要求Jawbone API提供該數據。 Web應用程序接收到這些數據后,您的Web應用程序會將其變成一個漂亮的句子并讀出來-為您的助手提供全新的智能范圍!
您的項目結構 (Your Project Structure)
I’ve adjusted the app from the initial HTML-only structure to one that uses EJS views so that you can switch pages in your web app when logging into the Jawbone Up API via OAuth. In reality, you only really have one page, but this method allows you to add more in future if needed for other IoT devices. This single view is at /views/index.ejs. You then have your Node server in the root folder as server.js and certificate files in root too. To keep things relatively simple and contained, all front-end JavaScript and CSS is inline. Feel free to move these into CSS and JS files as you prefer, minify them and make them pretty.
我已經將應用程序從最初的僅HTML結構調整為使用EJS視圖的結構,以便在通過OAuth登錄Jawbone Up API時可以在Web應用程序中切換頁面。 實際上,您實際上只有一頁,但是此方法允許您將來在其他物聯網設備需要時添加更多頁面。 該單一視圖位于/views/index.ejs 。 然后,將節點服務器作為server.js放在根文件夾中,并將證書文件也放在根目錄中。 為了使事情相對簡單和包含內容,所有前端JavaScript和CSS都是內聯的。 隨意將它們隨意移動到CSS和JS文件中,將它們縮小并使其漂亮。
響應JavaScript中的Api.ai操作 (Responding to Api.ai Actions in JavaScript)
As you might remember from the previous article, when Api.ai returns a response, it provides a JSON object that looks like so:
您可能還記得上一篇文章,當Api.ai返回響應時,它提供了一個JSON對象,如下所示:
{"id": "6b42eb42-0ad2-4bab-b7ea-853773b90219","timestamp": "2016-02-12T01:25:09.173Z","result": {"source": "agent","resolvedQuery": "how did I sleep last night","speech": "I'll retrieve your sleep stats for you now, one moment!","action": "sleepHours","parameters": {"sleep": "sleep"},"metadata": {"intentId": "25d04dfc-c90c-4f55-a7bd-6681e83b45ec","inputContexts": [],"outputContexts": [],"contexts": [],"intentName": "How many hours of @sleep:sleep did I get last night?"}},"status": {"code": 200,"errorType": "success"} }Within that JSON object there are two bits of data you need to use — action and parameters.sleep:
在該JSON對象中,您需要使用兩位數據action和parameters.sleep :
"action": "sleepHours", "parameters": {"sleep": "sleep" },action is the name you gave to the Api.ai action that the user has triggered. In the case of your sleep example, you named it “sleepHours”. parameters contain the variables in your sentence that can change a few details. In the case of sleep, your parameter tells you what type of sleep — “sleep”, “deep sleep”, “light sleep” or “REM sleep” (or just “REM”).
action是您為用戶觸發的Api.ai操作指定的名稱。 在您的睡眠示例中,您將其命名為“ sleepHours”。 parameters包含句子中的變量,可以更改一些細節。 在睡眠的情況下,您的參數告訴您什么類型的睡眠-“睡眠”,“深度睡眠”,“輕度睡眠”或“ REM睡眠”(或簡稱為“ REM”)。
Initially, in an earlier article on Api.ai, the prepareResponse() function took the JSON response from Api.ai, put the whole thing into your debug text field on the bottom right and took out Api.ai’s verbal response to display in the web app. You completely relied on what your Api.ai agent said, without adding any of your own functionality:
最初,在之前有關Api.ai的文章中, prepareResponse()函數從Api.ai接收JSON響應,將整個內容放入右下角的調試文本字段中,并取出Api.ai的口頭響應以顯示在網絡應用。 您完全依賴Api.ai代理所說的內容,而無需添加任何自己的功能:
function prepareResponse(val) {var debugJSON = JSON.stringify(val, undefined, 2),spokenResponse = val.result.speech;respond(spokenResponse);debugRespond(debugJSON); }This time around, keep an eye out for the action field and run your own function called requestSleepData() if the action contains "sleepHours". Within this function, pass in the sleep parameter so you know what type of sleep is being requested:
這一次,請注意action字段,如果該動作包含"sleepHours" ,請運行您自己的名為requestSleepData()的函數。 在此函數中,傳入sleep參數,以便您知道請求哪種類型的睡眠:
function prepareResponse(val) {var debugJSON = JSON.stringify(val, undefined, 2),spokenResponse = val.result.speech;if (val.result.action == "sleepHours") {requestSleepData(val.result.parameters.sleep);} else {respond(spokenResponse);}debugRespond(debugJSON); }Within requestSleepData(), request all sleep data from your Node.js server and then filter it by looking at the very first value in the returned array of data (data.items[0].details): this would be last night’s sleep. Within these details, you have data.items[0].details.rem with your REM sleep, data.items[0].details.sound with your deep sleep, data.items[0].details.light with your light sleep and data.items[0].details.duration with the combined amount of sleep recorded:
在requestSleepData() ,從Node.js服務器請求所有睡眠數據,然后通過查看返回的數據數組中的第一個值( data.items[0].details )進行data.items[0].details :這將是昨晚的睡眠。 在這些細節,你有data.items[0].details.rem與REM睡眠, data.items[0].details.sound與你的深度睡眠, data.items[0].details.light與淺睡眠和data.items[0].details.duration與記錄的總睡眠時間:
function requestSleepData(type) {$.ajax({type: "GET",url: "/sleep_data/",contentType: "application/json; charset=utf-8",dataType: "json",success: function(data) {console.log("Sleep data!", data);if (data.error) {respond(data.error);window.location.replace("/login/jawbone");}switch (type) {case "REM sleep":respond("You had " + toHours(data.items[0].details.rem) + " of REM sleep.");break;case "deep sleep":respond("You had " + toHours(data.items[0].details.sound) + " of deep sleep.");break;case "light sleep":respond("You had " + toHours(data.items[0].details.light) + " of light sleep.");break;case "sleep":respond("You had " + toHours(data.items[0].details.duration) + " of sleep last night. That includes " + toHours(data.items[0].details.rem) + " of REM sleep, " + toHours(data.items[0].details.sound) + " of deep sleep and " + toHours(data.items[0].details.light) + " of light sleep.");break;}},error: function() {respond(messageInternalError);}}); }toHours() is a crude and quick function that formats your times into sentences like “1 hour, 53 minutes and 59 seconds”:
toHours()是一種粗略而快捷的函數,可將您的時間格式化為“ 1小時53分59秒”之類的句子:
function toHours(secs) {hours = Math.floor(secs / 3600),minutes = Math.floor((secs - (hours * 3600)) / 60),seconds = secs - (hours * 3600) - (minutes * 60);hourText = hours + (hours > 1 ? " hours, " : " hour, ");minuteText = minutes + (minutes > 1 ? " minutes " : " minute ");secondText = seconds + (seconds > 1 ? " seconds" : " second");return hourText + minuteText + "and " + secondText; }As you’ll see when looking into the requestSleepData() function, the end result is a call to respond() — the same function that previously took Api.ai’s voice response. You reuse your existing functionality to bring speech to your own response, allowing your assistant to tell the user this information once it’s ready.
正如您在查看requestSleepData()函數時所看到的那樣,最終結果是調用了response respond() ,該函數先前與Api.ai的語音響應相同。 您可以重用現有功能,以使語音響應自己的React,使您的助手在準備好信息后就可以告訴用戶。
One last aspect of your front-end JavaScript to point out is error handling. If you have an issue with how Jawbone returns data (usually due to not being logged into the service), your server responds with a JSON value in the format of {"error" : "Your error message"}. The assistant sees this and automatically takes the user to your OAuth login page:
要指出的前端JavaScript的最后一個方面是錯誤處理。 如果您對Jawbone返回數據的方式有疑問(通常是由于未登錄服務),則服務器將以{"error" : "Your error message"}格式返回JSON值。 助手會看到此情況,并自動將用戶帶到您的OAuth登錄頁面:
if (data.error) {respond(data.error);window.location.replace("/login/jawbone"); }您的Node.js服務器 (Your Node.js Server)
Your Node.js server is based on the one used in Connecting to the Jawbone UP API with Node.js. If any of the code looks confusing, feel free to refer to that earlier article, as it explains all about connecting to the Jawbone API via OAuth and setting up an HTTPS server to run it. If you don’t have a Jawbone Up, the same concepts can be used for other IoT devices. You would just need to add your own methods of responding to GET requests with different data (and you may not need to worry about OAuth). The Jawbone Up data here is just an example.
您的Node.js服務器基于使用Node.js連接到Jawbone UP API中使用的服務器 。 如果任何代碼看起來令人困惑,請隨時參考該較早的文章,因為它解釋了有關通過OAuth連接到Jawbone API以及設置HTTPS服務器以運行它的所有內容。 如果您沒有Jawbone Up,則可以將相同的概念用于其他IoT設備。 您只需要添加自己的方法即可使用不同的數據來響應GET請求(并且您不必擔心OAuth)。 這里的Jawbone Up數據只是一個例子。
Your Jawbone data has been adjusted from the earlier article to provide a simple JSON response rather than formatting it all into a table template view. The variables of up and options have been moved to global variables so that they can be reused in multiple requests to the API (in that other SitePoint example, we only requested data in one big chunk each time).
您的Jawbone數據已根據上一篇文章進行了調整,以提供簡單的JSON響應,而不是將其全部格式化為表格模板視圖。 up和options變量已移至全局變量,以便可以在對API的多次請求中重復使用(在另一個SitePoint示例中,我們每次僅請求一大塊數據)。
To log into the Jawbone API via OAuth, the user can go to /login/jawbone. However, as you saw above, they don’t need to know to do this. Your assistant can also redirect them if it notices they aren’t logged in. You could also add a new intent into your Api.ai agent that understands the phrase “log me into my Jawbone Up data”, if you wanted to make this truly seamless. Your login route in Node.js looks like so:
要通過OAuth登錄Jawbone API,用戶可以轉到/login/jawbone 。 但是,正如您在上面看到的,他們不需要知道這樣做。 您的助手也可以重定向到它們,如果發現它們尚未登錄。您還可以在Api.ai代理中添加一個新意圖,該意圖可以理解“將我登錄到我的Jawbone Up數據中”這一短語,如果您想真正做到這一點的話無縫。 您在Node.js中的登錄路線如下所示:
app.get("/login/jawbone",passport.authorize("jawbone", {scope: ["basic_read","sleep_read"],failureRedirect: "/"}) );Once you’ve logged into the Jawbone API via passport.use("jawbone", new JawboneStrategy()), assign this access to your up variable and direct the user to /barry. You could redirect the user to any path of your choice, as long as it’s different from your root directory (that seemed to just cause an endless struggle for my server). I chose /barry, as I named my assistant Barry and thought it self explanatory (the page shows the exact same index view and thus isn’t different in any way). You could also use this as a way of providing a different view for users who have logged into their Jawbone device successfully if you so desire. Once logged in, the user can go back to the root https://localhost:5000 page and use Up functionality as well.
通過passport.use("jawbone", new JawboneStrategy())登錄到Jawbone API后,將此訪問權限分配給您的up變量,并將用戶定向到/barry 。 您可以將用戶重定向到您選擇的任何路徑,只要它與您的根目錄不同即可(這似乎為我的服務器帶來了無盡的麻煩)。 我選擇了/barry ,因為我命名了助手Barry并認為它可以自我解釋(該頁面顯示了完全相同的索引視圖,因此在任何方面都沒有區別)。 如果您愿意,也可以使用此方法為成功登錄其Jawbone設備的用戶提供不同的視圖。 登錄后,用戶可以返回到根https://localhost:5000頁面并使用Up功能。
發回您的物聯網數據 (Sending Back Your IoT Data)
Your retrieval of Jawbone data is done in a very simple way upon receiving a GET request for /sleep_data. Check if the up variable is defined: if not, your user hasn’t logged in and you tell the web app this so that it can perform the redirection and tell the user they’ll need to log in. Do the same thing if Jawbone returns any errors when you call up.sleeps.get() and if jawboneData.items isn’t defined:
收到對/sleep_data的GET請求后,可以非常簡單的方式檢索Jawbone數據。 檢查是否定義了up變量:如果未定義,則您的用戶尚未登錄,您將此通知Web應用程序,以便它可以執行重定向并告訴用戶他們需要登錄。如果Jawbone,請執行相同的操作當您調用up.sleeps.get()且未定義jawboneData.items時,將返回任何錯誤:
app.get("/sleep_data", function(req, resp) {if (up !== undefined) {up.sleeps.get({}, function(err, body) {if (err) {console.log("Error receiving Jawbone UP data");resp.send({"error": "Your sleep tracker isn't talking to me. Let's try logging in again."});} else {var jawboneData = JSON.parse(body).data;if (jawboneData.items) {resp.send(jawboneData);} else {console.log("Error: " + jawboneData);resp.send({"error": "Your sleep tracker isn't talking to me. Let's try logging in again."});}}});} else {console.log("Up is not ready, lets ask to log in.");resp.send({"error": "Your sleep tracker isn't talking to me. Let's try logging in again."});} });The errors here could be caused by other factors too, but to keep it simple, I’m focusing on getting them to try logging in again. In a production level app, you’d want to look into various causes and adjust your response.
這里的錯誤也可能是由其他因素引起的,但是為了簡單起見,我專注于讓他們嘗試再次登錄。 在生產級應用程序中,您需要調查各種原因并調整響應。
If all is well and you receive a valid response, send it as a JSON response back to the web app to read and parse through nicely:
如果一切正常,并且您收到了有效的響應,請將其作為JSON響應發送回Web應用程序,以很好地閱讀和解析:
if (jawboneData.items) {resp.send(jawboneData); }With the web app and your Node.js server working together, you should be able to retrieve sleep data from your Jawbone Up device now. So let’s give it a go.
在Web應用程序和Node.js服務器協同工作的情況下,您現在應該能夠從Jawbone Up設備檢索睡眠數據。 因此,讓我們開始吧。
行動中 (In Action)
Run your server with the usual node server.js. Remember that you’ll need to have run npm install for your npm modules and will need to have a certificate on your server to run it via HTTPS.
使用通常的node server.js運行服務器。 請記住,您需要為npm模塊運行npm install ,并且需要在服務器上具有證書才能通過HTTPS運行它。
Visiting https://localhost:5000 in your web browser should bring you to your AI assistant. (If you’re using a service like Glitch, you’ll have a Glitch URL instead.) Ask it how much sleep you’ve had:
在網絡瀏覽器中訪問https://localhost:5000應該會帶您進入AI助手。 (如果您使用的是諸如Glitch之類的服務,則將改為使用Glitch URL。)詢問您已睡了多少時間:
Turns out you aren’t logged in yet. It directs you to the Jawbone Up OAuth login screen. Log in and agree to provide access to your data, then click “Agree”:
原來您尚未登錄。 它會將您定向到Jawbone Up OAuth登錄屏幕。 登錄并同意提供對您的數據的訪問,然后單擊“同意”:
If you ask it this time around, you’ll receive the right answer:
如果您這次要求這樣做,您將獲得正確的答案:
You can also ask it something more specific, such as “How much REM did I get?” to test out the parameters:
您還可以詢問一些更具體的信息,例如“我獲得了多少REM?” 測試參數:
結論 (Conclusion)
That concludes quite a varied exploration of Api.ai’s capabilities for now! You could expand upon this example to allow it to understand date ranges (e.g. “How much sleep did I get on Tuesday?”) or to format the time slightly better (notice a slight bug in one of the responses?). You might have much nicer and shorter ways of phrasing the responses too. Personalize it — make it your own!
到目前為止,對Api.ai的功能進行了各種各樣的探索 ! 您可以擴展此示例,以使其理解日期范圍(例如“星期二我睡了多少時間?”)或略微改善時間格式(注意其中一個響應中的錯誤)。 您也可能有更好和更短的短語表達方式。 個性化-自己制作!
As you can see, by using these methods you can connect up any Node.js compatible or web API compatible service to your Node.js server, hook it up to an intent in your Api.ai agent and teach it all sorts of things! You could connect up plenty of IoT devices via IFTTT, connect up your LIFX smart lights via IFTTT or even connect up your very own Nodebot. Possibilities are limited only by the devices you have at your disposal!
如您所見,通過使用這些方法,您可以將任何與Node.js兼容或與Web API兼容的服務連接到Node.js服務器,將其與Api.ai代理中的意圖掛鉤并進行各種操作! 您可以通過IFTTT連接大量物聯網設備,通過IFTTT 連接LIFX智能燈,甚至可以連接自己的Nodebot 。 可能性僅受您擁有的設備限制!
If you’ve been following along and building your own personal assistant using Api.ai, I’d love to hear how you went! What have you connected it to? Let me know in the comments below, or get in touch with me on Twitter at @thatpatrickguy.
如果您一直在使用Api.ai并建立自己的私人助理,我很想聽聽您的情況! 您連接了什么? 在下面的評論中讓我知道,或者在Twitter上通過@thatpatrickguy與我聯系 。
Give your AI the human touch with a sentiment tool. Check out our screencasts on the Microsoft Cognitive Services and the Text Analytics API.
使用情感工具讓您的AI具有人性化的感覺。 查看我們有關Microsoft Cognitive Services和Text Analytics API的截屏視頻。
翻譯自: https://www.sitepoint.com/how-to-connect-your-api-ai-assistant-to-the-iot/
ai物聯網工程師
總結
以上是生活随笔為你收集整理的ai物联网工程师_如何将Api.ai助手连接到物联网的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: springboot学习(六十七) sp
- 下一篇: 3dmaxobj导出选项_3dmax将模