Created
January 17, 2017 18:06
-
-
Save andkon/b157d58d4cafc41aa3e753a8147a74c1 to your computer and use it in GitHub Desktop.
Realm Mobile Platform PE — wit.ai NLP event handling demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var fs = require('fs'); | |
var Realm = require('realm'); | |
const {Wit, log} = require('node-wit'); | |
// Insert the Realm admin token | |
// Linux: `cat /etc/realm/admin_token.base64` | |
// macOS (from within zip): `cat realm-object-server/admin_token.base64` | |
var REALM_ADMIN_TOKEN = "YOUR_ADMIN_TOKEN"; | |
// The URL to the Realm Object Server | |
var SERVER_URL = 'realm://YOUR_SERVER_IP:9080'; | |
// or use the 9443 port for secure URLs: | |
// var SERVER_URL = 'realm://YOUR_SERVER_IP:9443'; | |
// The path used by the global notifier to listen for changes across all | |
// Realms that match. | |
var NOTIFIER_PATH = ".*/realmtasks"; | |
// Server access token from wit.ai API | |
var WIT_ACCESS_TOKEN = "WIT_ACCESS_TOKEN" | |
const client = new Wit({accessToken: WIT_ACCESS_TOKEN}) | |
function isRealmObject(x) { | |
return x !== null && x !== undefined && x.constructor === Realm.Object | |
} | |
var change_notification_callback = function(change_event) { | |
let realm = change_event.realm; | |
let changes = change_event.changes.Task; | |
let taskIndexes = changes.modifications; | |
console.log(changes); | |
// Get the task objects to processes | |
var tasks = realm.objects("Task"); | |
for (var i = 0; i < taskIndexes.length; i++) { | |
let taskIndex = taskIndexes[i]; | |
// Retrieve the task object from the Realm with index | |
let task = tasks[taskIndex]; | |
if (isRealmObject(task)) { | |
console.log("New task received: " + change_event.path); | |
console.log(JSON.stringify(task)) | |
// get date from wit.ai | |
// probably use this https://wit.ai/docs/http/20160526#get--message-link | |
// node-wit: https://github.com/wit-ai/node-wit | |
const client = new Wit({accessToken: WIT_ACCESS_TOKEN}); | |
client.message(task.text, {}).then((data) => { | |
console.log('Yay, got Wit.ai response: ' + JSON.stringify(data)); | |
}) | |
.catch(console.error); | |
// to write the date, we'll have to add a date property on the client and migrate it | |
// realm.write(function() { | |
// task.date = ; | |
// }); | |
} | |
} | |
}; | |
//Create the admin user | |
var admin_user = Realm.Sync.User.adminUser(REALM_ADMIN_TOKEN); | |
//Callback on Realm changes | |
Realm.Sync.addListener(SERVER_URL, admin_user, NOTIFIER_PATH, 'change', change_notification_callback); | |
console.log('Listening for Realm changes across: ' + NOTIFIER_PATH); | |
// End of index.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "TaskApp", | |
"version": "0.0.1", | |
"description": "Perform NLP with event handling.", | |
"main": "index.js", | |
"author": "Realm", | |
"dependencies": { | |
"node-wit": "^4.2.0", | |
"realm": "file:realm-professional.tgz" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment