Last active
September 1, 2020 16:03
-
-
Save oroce/68e97ba8f555c4309aa3 to your computer and use it in GitHub Desktop.
Huginn javascript agent stuff
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
var Agent = {}; | |
Agent.check = function() { | |
var events = JSON.parse(this.memory('events') || '[]'); | |
this.log('memory is: (' + typeof events + '):' + JSON.stringify(events)); | |
var len = events.length; | |
var tags = (this.options('tags') || '').split(','); | |
var count = parseInt(this.options('count'), 10) || tags.length; | |
var delimeter = this.options('delimeter') || ','; | |
var tagPrefix = this.options('tagPrefix') || this.options('tagprefix') || ''; | |
if (len === 0) { | |
return; | |
} | |
var idx = Math.floor(Math.random() * len); | |
var event = events[idx]; | |
event.tags = tags | |
.sort(function() { | |
return 0.5 - Math.random(); | |
}) | |
.slice(0, count) | |
.map(function(tag) { | |
return tagPrefix + tag; | |
}) | |
.join(delimeter); | |
this.createEvent(event); | |
}.bind({ | |
log: console.log.bind(console), | |
memory: function(key) { | |
return ({ | |
events: JSON.stringify([{'foo': 'bar'}]) | |
})[key]; | |
}, | |
options: function(key) { | |
return ({ | |
tags: 'foobar,barfoo,FOO,BAR,CELMA', | |
count: 2, | |
delimeter: '|', | |
tagPrefix: '#' | |
})[key]; | |
}, | |
createEvent: function(data) { | |
document.body.innerHTML += '<pre><code>' + JSON.stringify(data, null, 2) + '</code></pre><hr />'; | |
} | |
}); | |
var i = 0; | |
while(i < 5) { | |
i++; | |
setTimeout(Agent.check.bind(Agent), i * 1000); | |
} | |
Agent.check(); |
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
Agent.receive = function() { | |
var events = JSON.parse(this.memory('events') || '[]'); | |
this.log('memory is: (' + typeof events + '):' + JSON.stringify(events)); | |
this.incomingEvents().forEach(function(event) { | |
events.push(event.payload); | |
}); | |
this.memory('events', JSON.stringify(events)); | |
}; | |
Agent.check = function() { | |
var events = JSON.parse(this.memory('events') || '[]'); | |
this.log('memory is: (' + typeof events + '):' + JSON.stringify(events)); | |
var len = events.length; | |
var tags = (this.options('tags') || '').split(','); | |
var count = parseInt(this.options('count'), 10) || tags.length; | |
var delimeter = this.options('delimeter') || ','; | |
var tagPrefix = this.options('tagPrefix') || this.options('tagprefix') || ''; | |
if (len === 0) { | |
return; | |
} | |
var idx = Math.floor(Math.random() * len); | |
var event = events[idx]; | |
event.tags = tags | |
.sort(function() { | |
return 0.5 - Math.random(); | |
}) | |
.slice(0, count) | |
.map(function(tag) { | |
return tagPrefix + tag; | |
}) | |
.join(delimeter); | |
this.createEvent(event); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment