Skip to content

Commit

Permalink
Minor design improvements. Work in progress on bot replies.
Browse files Browse the repository at this point in the history
  • Loading branch information
Glitch (glitch-fediverse-bot) committed Oct 9, 2018
1 parent e7d047b commit 7a04d1a
Show file tree
Hide file tree
Showing 27 changed files with 680 additions and 64 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
sessions
5 changes: 5 additions & 0 deletions .glitch-assets
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@
{"name":"glitch-fediverse-bot.png","date":"2018-09-29T12:49:07.963Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fglitch-fediverse-bot.png","type":"image/png","size":58653,"imageWidth":1920,"imageHeight":1079,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Fglitch-fediverse-bot.png","thumbnailWidth":330,"thumbnailHeight":186,"dominantColor":"rgb(252,252,252)","uuid":"0KNy0WXAgI1pZ0fd"}
{"name":"glitch-fediverse-bot-960px.png","date":"2018-09-29T12:49:08.030Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fglitch-fediverse-bot-960px.png","type":"image/png","size":50867,"imageWidth":960,"imageHeight":540,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Fglitch-fediverse-bot-960px.png","thumbnailWidth":330,"thumbnailHeight":186,"dominantColor":"rgb(252,252,252)","uuid":"8tHyMsnvvVKkjFXF"}
{"name":"glitch-fediverse-bot-with-image.png","date":"2018-10-03T21:11:53.744Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fglitch-fediverse-bot-with-image.png","type":"image/png","size":469823,"imageWidth":2880,"imageHeight":1465,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Fglitch-fediverse-bot-with-image.png","thumbnailWidth":330,"thumbnailHeight":168,"dominantColor":"rgb(252,252,252)","uuid":"oHDFFpP6XyCauPmh"}
{"name":"bot-replies.png","date":"2018-10-09T12:35:59.939Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fbot-replies.png","type":"image/png","size":186912,"imageWidth":2834,"imageHeight":1256,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Fbot-replies.png","thumbnailWidth":330,"thumbnailHeight":147,"dominantColor":"rgb(236,244,244)","uuid":"sHZ0AbdbgCtnaQYM"}
{"uuid":"iLCHg6pDTsAX7jkg","deleted":true}
{"uuid":"0KNy0WXAgI1pZ0fd","deleted":true}
{"uuid":"8tHyMsnvvVKkjFXF","deleted":true}
{"name":"glitch-fediverse-bot.png","date":"2018-10-09T12:49:00.686Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fglitch-fediverse-bot.png","type":"image/png","size":206001,"imageWidth":2854,"imageHeight":1398,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Fglitch-fediverse-bot.png","thumbnailWidth":330,"thumbnailHeight":162,"dominantColor":"rgb(236,244,244)","uuid":"6oFnGGQy88GLx9Be"}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
**Note**: This project is under active development and not available for remixing, but you can [import it from GitHub](https://glitch.com/#!/import/github/fourtonfish/glitch-fediverse-bot).


![Glitch Fediverse bot](https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fglitch-fediverse-bot-small-1024px.png?1538225347895)
![Glitch Fediverse bot](https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fglitch-fediverse-bot.png)


# Glitch Fediverse bot
Expand Down
6 changes: 6 additions & 0 deletions bot/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Bot logic

The files in the `/bot` folder contain the source code for the bot's behavior.

- Inside `bot.js` you'll find internal methods, like `create_post` and `delete_post`.
- To change bot's responses to messages it receives, see `responses.js`.
84 changes: 72 additions & 12 deletions bot.js → bot/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ var fs = require('fs'),
url = require('url'),
util = require('util'),
moment = require('moment'),
db = require(__dirname + '/helpers/db.js'),
keys = require(__dirname + '/helpers/keys.js'),
db = require(__dirname + '/../helpers/db.js'),
keys = require(__dirname + '/../helpers/keys.js'),
request = require('request'),
public_key_path = '.data/rsa/pubKey',
private_key_path = '.data/rsa/privKey',
bot_url = `https://${process.env.PROJECT_DOMAIN}.glitch.me`;
bot_url = `https://${process.env.PROJECT_DOMAIN}.glitch.me`,
bot_compose_reply = require(__dirname + '/responses.js');

if (!fs.existsSync(public_key_path) || !fs.existsSync(private_key_path)) {
keys.generate_keys(function(){
Expand All @@ -21,7 +22,7 @@ else{

module.exports = {
bot_url: bot_url,
links: [
links: [
// {
// rel: 'http://webfinger.net/rel/profile-page',
// type: 'text/html',
Expand Down Expand Up @@ -74,6 +75,28 @@ else{
'publicKeyPem': public_key
}
},
compose_reply: bot_compose_reply,
send_reply: function(options, cb){
var bot = this,
reply_to_username = '';

try{
var actor_url_parts = options.payload.actor.split('/');
var username = actor_url_parts[actor_url_parts.length-1];
reply_to_username = `@${username}@${url.parse(options.payload.actor).hostname} `;

console.log({reply_to_username});
} catch(err){ /*noop*/ }

bot.create_post({
type: 'Note',
content: `<blockquote>${options.payload.object.content}<p><a href="${options.payload.object.url}">${options.payload.object.url}</a></p></blockquote><p>${options.reply_message}</p>`,
reply_message: `<a href="${options.payload.actor}">${reply_to_username}</a> ${options.reply_message}`,
in_reply_to: options.payload.object.url
}, function(err, message){
// console.log(err, message);
});
},
create_post: function(options, cb){
var bot = this;

Expand All @@ -86,6 +109,7 @@ else{
post_description = options.description,
post_date = moment().format(),
post_in_reply_to = options.in_reply_to || null,
reply_message = options.reply_message || null,
post_content = options.content || options.url || '',
post_attachment = JSON.stringify(options.attachment) || '[]';

Expand All @@ -104,7 +128,7 @@ else{
'type': post_type,
'published': post_date,
'attributedTo': `${bot_url}/bot`,
'content': post_content,
'content': reply_message || post_content,
'to': 'https://www.w3.org/ns/activitystreams#Public'
};

Expand All @@ -122,18 +146,20 @@ else{
}
}

if (post_in_reply_to){
post_object.inReplyTo = post_in_reply_to;
}

var post = {
'@context': 'https://www.w3.org/ns/activitystreams',
'id': `${bot_url}/post/${post_id}`,
'type': 'Create',
'actor': `${bot_url}/bot`,
'object': post_object
}

if (options.post_in_reply_to){
post.object.inReplyTo = post_in_reply_to;
}


console.log({post_in_reply_to});

db.get_followers(function(err, followers){
if (followers){
console.log(`sending update to ${followers.length} follower(s)...`);
Expand Down Expand Up @@ -179,8 +205,12 @@ else{
});
},
accept: function(payload, cb){
var bot = this,
guid = crypto.randomBytes(16).toString('hex');
var bot = this,
guid = crypto.randomBytes(16).toString('hex');

db.get_event(payload.id, function(err, data){
// console.log('get_event', err, data);


bot.sign_and_send({
follower: {
Expand All @@ -197,7 +227,37 @@ else{
if (cb){
cb(err, payload, data);
}
console.log('saving event', payload.id)
db.save_event(payload.id);
});

});
// db.get_event(payload.id, function(err, data){
// console.log('get_event', err, data);

// if (!err && !data){
// bot.sign_and_send({
// follower: {
// url: payload.actor
// },
// message: {
// '@context': 'https://www.w3.org/ns/activitystreams',
// 'id': `${bot.bot_url}/${guid}`,
// 'type': 'Accept',
// 'actor': `${bot.bot_url}/bot`,
// 'object': payload,
// }
// }, function(err, data){
// if (cb){
// cb(err, payload, data);
// }
// console.log('saving event', payload.id)
// db.save_event(payload.id);
// });
// } else if (!err){
// console.log('duplicate event');
// }
// });
},
sign_and_send: function(options, cb){
var bot = this;
Expand Down
43 changes: 43 additions & 0 deletions bot/responses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Here you can modify how the bot responds to messages it receives.
*/

module.exports = function(data, callback_function) {
/*
At the end of this function we need to pass an error message and a response text.
Let's set up some default values.
*/

var error = null,
response = 'Hello 👋';

/*
The data object this function receives looks like this:
data = {
payload: 'The original data object.',
message_body: 'The content of the message sent to the bot.',
message_from: 'The URL of the message sender.'
}
message_body and message_from come from the payload object, so we can access them more conveniently. If we need more details, we can get those from the payload object itself.
*/

console.log(`new message from ${data.message_from}:`)
console.log(data.message_body);

/*
We can modify the response text.
*/

if (data.message_body.toLowerCase().indexOf('hello') > -1){
response = 'Hi 👋';
}

/*
Finally, we pass the error and reply message to the callback function that sends it to the author of the message that the bot received and saves it to the post database.
*/

callback_function(error, response);
};
15 changes: 15 additions & 0 deletions examples/replies/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
**Work in progress**

![A bot replying to a message](https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fbot-replies.png?1539088559939)

# Bot replies

To use bot replies, update `bot/responses.js` with a function that returns a reply message.


TODO:

- correctly dedupe events
- add support for private messages
- send notification when posting reply

43 changes: 43 additions & 0 deletions examples/replies/bot/bot-replies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Here you can modify how the bot responds to messages it receives.
*/

module.exports = function(data, callback_function) {
/*
At the end of this function we need to pass an error message and a response text.
Let's set up some default values.
*/

var error = null,
response = 'Hello 👋';

/*
The data object this function receives looks like this:
data = {
payload: 'The original data object.',
message_body: 'The content of the message sent to the bot.',
message_from: 'The URL of the message sender.'
}
message_body and message_from come from the payload object, so we can access them more conveniently. If we need more details, we can get those from the payload object itself.
*/

console.log(`new message from ${data.message_from}:`)
console.log(data.message_body);

/*
We can modify the response text.
*/

if (data.message_body.toLowerCase().indexOf('hello') > -1){
response = 'Hi 👋';
}

/*
Finally, we pass the error and reply message to the callback function that sends it to the author of the message that the bot received and saves it to the post database.
*/

callback_function(error, response);
};
Loading

0 comments on commit 7a04d1a

Please sign in to comment.