Skip to content

Commit

Permalink
Some cleanup - esp. to language parsing from gerty and how text input…
Browse files Browse the repository at this point in the history
…s are inputted. New app to announce AND execute code from whitelisted Tweets.
  • Loading branch information
imbrianj committed Jan 9, 2018
1 parent f172ef9 commit 4e6556c
Show file tree
Hide file tree
Showing 14 changed files with 147 additions and 35 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Supported Devices/Services
| SMS | Stable | Uses Twilio. Requires ID and token |
| Speech | Stable | Uses espeak for *nix, say on OSX. Win not supported |
| Sports | Stable | Pull sports scores from ESPN (please don't hammer this endpoint) |
| Stocks | Stable | Uses Yahoo Finance |
| Stocks | Unreliable | Uses Yahoo Finance (API has been unreliable - looking for alternatives) |
| Traffic Cams | Stable | View multiple traffic webcams |
| Travis CI | Stable | |
| Twitter | Development | Present mentions of your Twitter handle |
Expand Down
12 changes: 7 additions & 5 deletions apps/gerty.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (function () {
'use strict';

return {
version : 20161221,
version : 20180107,

gerty : function (deviceId, command, controllers, values, config, appParams) {
var translate = require(__dirname + '/../lib/translate'),
Expand Down Expand Up @@ -83,10 +83,12 @@ module.exports = (function () {
utterance = acted || '';
}

for (tempDevice in controllers) {
if (tempDevice !== 'config') {
if ((controllers[tempDevice].config.typeClass === 'speech') || (controllers[tempDevice].config.typeClass === 'clientSpeech')) {
runCommand.runCommand(tempDevice, 'text-' + utterance);
if (utterance) {
for (tempDevice in controllers) {
if (tempDevice !== 'config') {
if ((controllers[tempDevice].config.typeClass === 'speech') || (controllers[tempDevice].config.typeClass === 'clientSpeech')) {
runCommand.runCommand(tempDevice, 'text-' + utterance);
}
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions apps/gerty/runCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ module.exports = (function () {
'use strict';

return {
version : 20171208,
version : 20180107,

/**
* Take in a typeClass and return the type of device it's categorized as.
Expand Down Expand Up @@ -118,7 +118,8 @@ module.exports = (function () {
k = 0,
x = 0;

text = rawText.split(' ');
rawText = translate.stripPunctuation(rawText);
text = rawText.split(' ');

if (macros) {
// Loop through all configured macros anywhere in the inputted text.
Expand Down
88 changes: 88 additions & 0 deletions apps/twitterCommand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* Copyright (c) 2014 [email protected]
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/

/**
* @author [email protected]
* @fileoverview Checks for new tweets to a given user. If that tweet is
* authored by someone on our whitelist, pass the contents of the
* tweet to Gerty for plain language execution.
*/

module.exports = (function () {
'use strict';

var startup = null;
var completed = [];

return {
version : 20180107,

twitterCommand : function (deviceId, command, controllers, values, config) {
var senders = config.senders,
i = 0,
value,
message,
translate,
notify;

// On first execution, log the time. Any commands that preceed startup
// time are not worth executing.
if (!startup) {
startup = new Date().getTime();
}

// If you don't have any whitelist of Tweet senders, there's nothing we
// can safely do.
if (senders) {
for (i; i < values.value.length; i += 1) {
value = values.value[i];

// Tweets are provided in reverse chron. Our first instance of one
// that's too old implies that they are all too old.
if (value.date < startup) {
break;
}

// Only execute a tweet once - then add it to a completed list.
else if (completed.indexOf(value.url) === -1) {
// The sender is on our trusted whitelist.
if (senders.indexOf(value.author) !== -1) {
notify = require(__dirname + '/../lib/notify');
translate = require(__dirname + '/../lib/translate');

// Add to the blacklist so this same message won't be executed
// again.
completed.push(value.url);

message = translate.translate('{{i18n_FROM_TWITTER}}', 'twitter', controllers.config.language);
message = message.split('{{MESSAGE}}').join(value.text);

// Send the message to all controllers. Let them sort out what
// they want to do with them.
notify.notify(message, controllers, 'macro');
}
}
}
}
}
};
}());
2 changes: 1 addition & 1 deletion cache/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1514494668493
1515464995205
7 changes: 7 additions & 0 deletions config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,13 @@ exports.config = {
accessToken : 'xxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
consumerSecret : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
oauthTokenSecret : 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
// If any users listed under "senders" Tweets @ your user, it well send the
// text body of the tweet to all configured controllers. If Gerty is
// included, it will attempt to execute macros and commands.
apps : { 'Twitter Command' : { id : 'twitterCommand',
senders : ['imbrianj'],
controllerIds : ['gerty', 'clientNotify', 'speech', 'clientSpeech', 'pushover'],
disabled : true } },
disabledMarkup : true,
disabled : true
},
Expand Down
12 changes: 3 additions & 9 deletions devices/gerty/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = (function () {
* @fileoverview Register comments to Gerty.
*/
return {
version : 20161221,
version : 20180107,

inputs : ['command', 'text'],

Expand Down Expand Up @@ -177,19 +177,13 @@ module.exports = (function () {
now;

if (gertyState && gertyState.value && gertyState.value.comments) {
allComments = gertyState.value.comments;
allComments = gertyState.value.comments || [];
}

if (comment) {
now = new Date().getTime();

if (allComments.length) {
allComments.push({ text : comment, time : now, name : user.name, code : user.code });
}

else {
allComments = [{ text : comment, time : now, name : user.name, code : user.code }];
}
allComments.push({ text : comment, time : now, name : user.name, code : user.code });
}

// We don't need to keep a full log as it'd be too heavy to update with
Expand Down
2 changes: 1 addition & 1 deletion js/combo.min.js

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ module.exports = (function () {
BUILD_STATUS : "Build Status"
},
twitter : {
TWITTER : "Twitter"
TWITTER : "Twitter",
FROM_TWITTER : "From Twitter: {{MESSAGE}}"
},
weather : {
WEATHER : "Weather",
Expand Down
3 changes: 2 additions & 1 deletion lang/es-CO.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,8 @@ module.exports = (function () {
BUILD_STATUS : "Estado de construcción"
},
twitter : {
TWITTER : "Twitter"
TWITTER : "Twitter",
FROM_TWITTER : "De Twitter: {{MESSAGE}}"
},
weather : {
WEATHER : "Tiempo",
Expand Down
15 changes: 8 additions & 7 deletions lib/runCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ module.exports = (function () {
var Controllers = {};

return {
version : 20170414,
version : 20180107,

/**
* As we want to keep controllers private, but we also want to make them
Expand Down Expand Up @@ -102,7 +102,8 @@ module.exports = (function () {
* bits and send it to runCommand to execute.
*/
parseCommands : function (device, command, source, request, response, endResponse) {
var reply = '',
var reply = '',
isText = this.getCommandType(command) === 'text',
rawMacro,
macro;

Expand All @@ -116,7 +117,7 @@ module.exports = (function () {
}
}

else if (command.indexOf(';') !== -1) {
else if ((command.indexOf(';') !== -1) && (!isText)) {
console.log('\x1b[35mMulti-device macro command issued\x1b[0m');

if (endResponse) {
Expand All @@ -133,7 +134,7 @@ module.exports = (function () {
}
}

else if (command.indexOf(',') !== -1) {
else if ((command.indexOf(',') !== -1) && (!isText)) {
console.log('\x1b[35mMacro command issued\x1b[0m');

if (endResponse) {
Expand Down Expand Up @@ -234,7 +235,7 @@ module.exports = (function () {
* Each (valid) command is prefixed with it's type. Here, we use that to
* register it's type.
*/
getCommandType : function (device, command) {
getCommandType : function (command) {
var type = 'command';

if (!command) {
Expand Down Expand Up @@ -275,7 +276,7 @@ module.exports = (function () {
* real security.
*/
validateCommand : function (device, command) {
var commandType = this.getCommandType(device, command),
var commandType = this.getCommandType(command),
controller = Controllers[device],
commandValid = false;

Expand Down Expand Up @@ -324,7 +325,7 @@ module.exports = (function () {
runCommand : function (deviceId, command, source, request, response, callback, endResponse) {
var apps = require(__dirname + '/../lib/apps'),
message = 'invalid',
commandType = this.getCommandType(deviceId, command),
commandType = this.getCommandType(command),
controllers = Controllers,
enableLogging = Controllers.config ? !!Controllers.config.ai.eventLogging : false,
controller = Controllers[deviceId],
Expand Down
9 changes: 8 additions & 1 deletion lib/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ module.exports = (function () {
'use strict';

return {
version : 20160329,
version : 20180107,

/**
* Accept a string and return that string without punctation.
*/
stripPunctuation : function (input) {
return input.replace(/[:;.,!?]/g, '');
},

/**
* Returns the array of synonyms for a given device's translations.
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/lib/runCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

/**
* @author [email protected]
* @fileoverview Unit test for loadMarkup.js
* @fileoverview Unit test for runCommand.js
*/

exports.runCommandTest = {
Expand Down Expand Up @@ -88,11 +88,11 @@ exports.runCommandTest = {
'use strict';

var runCommand = require(__dirname + '/../../../lib/runCommand'),
empty = runCommand.getCommandType(null, null),
text = runCommand.getCommandType(null, 'text-TEST'),
launch = runCommand.getCommandType(null, 'launch-TEST'),
list = runCommand.getCommandType(null, 'list'),
command = runCommand.getCommandType(null, 'TEST');
empty = runCommand.getCommandType(null),
text = runCommand.getCommandType('text-TEST'),
launch = runCommand.getCommandType('launch-TEST'),
list = runCommand.getCommandType('list'),
command = runCommand.getCommandType('TEST');

test.strictEqual(empty, '', 'Should return an empty string');
test.strictEqual(text, 'text', 'Should return text');
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/lib/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@
*/

exports.translateTest = {
stripPunctuation : function (test) {
'use strict';

var translate = require('../../../lib/translate');

test.deepEqual(translate.stripPunctuation('This, is; a ... test?'), 'This is a test', 'Replaced punctuation');

test.done();
},

findSynonyms : function (test) {
'use strict';

Expand Down

0 comments on commit 4e6556c

Please sign in to comment.