Skip to content

Instantly share code, notes, and snippets.

@oroce
Last active September 1, 2020 16:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oroce/68e97ba8f555c4309aa3 to your computer and use it in GitHub Desktop.
Save oroce/68e97ba8f555c4309aa3 to your computer and use it in GitHub Desktop.
Huginn javascript agent stuff
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();
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