Skip to content

Commit

Permalink
Merge pull request #27 from botwiki/glitch
Browse files Browse the repository at this point in the history
Nicer design. Link to admin logout. Overall code cleanup.
  • Loading branch information
stefanbohacek authored Jan 21, 2020
2 parents 15cba59 + c17eac2 commit 10d6d9c
Show file tree
Hide file tree
Showing 29 changed files with 6,662 additions and 359 deletions.
6 changes: 6 additions & 0 deletions .glitch-assets
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@
{"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"}
{"name":"relieved-face.png","date":"2020-01-21T16:55:20.629Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Frelieved-face.png","type":"image/png","size":4782,"imageWidth":512,"imageHeight":512,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Frelieved-face.png","thumbnailWidth":330,"thumbnailHeight":330,"uuid":"ystBbCRjHvMTAkmP"}
{"uuid":"lmZRNK6DdCO5aJr7","deleted":true}
{"name":"feed.png","date":"2020-01-21T20:46:04.612Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Ffeed.png","type":"image/png","size":435181,"imageWidth":2822,"imageHeight":1630,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Ffeed.png","thumbnailWidth":330,"thumbnailHeight":191,"uuid":"QJrzlYrlyc1CjcsQ"}
{"name":"feed-mastodon.png","date":"2020-01-21T20:46:04.659Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Ffeed-mastodon.png","type":"image/png","size":235591,"imageWidth":644,"imageHeight":1478,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Ffeed-mastodon.png","thumbnailWidth":144,"thumbnailHeight":330,"uuid":"i1w6Ti1cNwHF1vbT"}
{"name":"feed-mastodon-small.png","date":"2020-01-21T20:48:45.012Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Ffeed-mastodon-small.png","type":"image/png","size":82308,"imageWidth":200,"imageHeight":459,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Ffeed-mastodon-small.png","thumbnailWidth":144,"thumbnailHeight":330,"uuid":"x1IOEKWrW0Uaafk4"}
{"name":"feed-comb.png","date":"2020-01-21T21:08:53.717Z","url":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Ffeed-comb.png","type":"image/png","size":453422,"imageWidth":2156,"imageHeight":1630,"thumbnail":"https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fthumbnails%2Ffeed-comb.png","thumbnailWidth":330,"thumbnailHeight":250,"uuid":"YRb0rstRrVoXfWQv"}
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
**Note**: This project is under active development and not available for remixing on Glitch. Instead, please [import it from this GitHub repo](https://glitch.com/#!/import/github/fourtonfish/glitch-fediverse-bot).
***Note: This project is under active development and not available for remixing on Glitch. Instead, please [import it from this GitHub repo](https://glitch.com/#!/import/github/botwiki/glitch-fediverse-bot).***


![Glitch Fediverse bot](https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Fglitch-fediverse-bot.png)
![Glitch Fediverse bot feed](https://cdn.glitch.com/a4825d5c-d1d6-4780-8464-8636780177ef%2Ffeed-comb.png)


# Glitch Fediverse bot

With this Glitch starter project, you can create a bot that anyone [in the fediverse](https://en.wikipedia.org/wiki/Fediverse) can follow. This project is still in early development with more features coming.
With this Glitch starter project, you can create a bot that anyone [in the fediverse](https://en.wikipedia.org/wiki/Fediverse) can follow. This project is [in early development](https://github.com/botwiki/glitch-fediverse-bot/issues) with more features coming.

To automate your bot, set up a free service like [cron-job.org](https://cron-job.org/en/), [Uptime Robot](https://uptimerobot.com/), or [a similar one](https://www.google.com/search?q=free+web+cron) to wake up your bot [every 25+ minutes](https://support.glitch.com/t/a-simple-twitter-bot-template/747/16). Use `https://YOUR_PROJECT_NAME.glitch.me/BOT_ENDPOINT` as a URL to which to send the HTTP request. (`BOT_ENDPOINT` is set in your `.env` file.)


## Bot administration

You can log into the admin panel by going to `YOUR_PROJECT_NAME.glitch.me/admin` and logging in using the password set inside your `.env` file. This will allow you to delete your bot's posts one by one. (Multi-post deletion is coming!)
Expand Down
40 changes: 20 additions & 20 deletions bot/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ var fs = require('fs'),
url = require('url'),
util = require('util'),
moment = require('moment'),
db = require(__dirname + '/../helpers/db.js'),
dbHelper = 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_compose_reply = require(__dirname + '/responses.js');
botComposeReply = require(__dirname + '/responses.js');

if (!fs.existsSync(public_key_path) || !fs.existsSync(private_key_path)) {
keys.generate_keys(function(){
Expand Down Expand Up @@ -75,8 +75,8 @@ else{
'publicKeyPem': public_key
}
},
compose_reply: bot_compose_reply,
send_reply: function(options, cb){
composeReply: botComposeReply,
sendReply: function(options, cb){
var bot = this,
reply_to_username = '';

Expand All @@ -88,16 +88,16 @@ else{
console.log({reply_to_username});
} catch(err){ /*noop*/ }

bot.create_post({
bot.createPost({
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>`,
content: `<blockquote>${options.payload.object.content}<cite><a href="${options.payload.object.url}">${options.payload.object.url}</a></cite></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){
createPost: function(options, cb){
var bot = this;

if ((!options.content || options.content.trim().length === 0 ) && !options.attachment ){
Expand All @@ -112,8 +112,8 @@ else{
reply_message = options.reply_message || null,
post_content = options.content || options.url || '',
post_attachment = JSON.stringify(options.attachment) || '[]';

db.save_post({
dbHelper.savePost({
type: post_type,
content: post_content,
attachment: post_attachment
Expand Down Expand Up @@ -160,13 +160,13 @@ else{

console.log({post_in_reply_to});

db.get_followers(function(err, followers){
dbHelper.getFollowers(function(err, followers){
if (followers){
console.log(`sending update to ${followers.length} follower(s)...`);

followers.forEach(function(follower){
if (follower.url){
bot.sign_and_send({
bot.signAndSend({
follower: follower,
message: post
}, function(err, data){
Expand All @@ -182,11 +182,11 @@ else{
}
});
},
delete_post: function(post_id, follower_url, cb){
deletePost: function(post_id, follower_url, cb){
var bot = this;
// guid = crypto.randomBytes(16).toString('hex');

bot.sign_and_send({
bot.signAndSend({
follower: {
url: follower_url
},
Expand All @@ -208,11 +208,11 @@ else{
var bot = this,
guid = crypto.randomBytes(16).toString('hex');

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


bot.sign_and_send({
bot.signAndSend({
follower: {
url: payload.actor
},
Expand All @@ -228,15 +228,15 @@ else{
cb(err, payload, data);
}
console.log('saving event', payload.id)
db.save_event(payload.id);
dbHelper.saveEvent(payload.id);
});

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

// if (!err && !data){
// bot.sign_and_send({
// bot.signAndSend({
// follower: {
// url: payload.actor
// },
Expand All @@ -252,14 +252,14 @@ else{
// cb(err, payload, data);
// }
// console.log('saving event', payload.id)
// db.save_event(payload.id);
// dbHelper.saveEvent(payload.id);
// });
// } else if (!err){
// console.log('duplicate event');
// }
// });
},
sign_and_send: function(options, cb){
signAndSend: function(options, cb){
var bot = this;
// console.log('message to sign:');
// console.log(util.inspect(options.message, false, null, true));
Expand Down
10 changes: 5 additions & 5 deletions examples/generative-art-bot/routes/bot-endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ var express = require('express'),
},
grammar = require(__dirname + '/../tracery/tracery.js').grammar,
helpers = require(__dirname + '/../helpers/general.js'),
image_uploader = require(__dirname + '/../helpers/image-uploader.js'),
bot = require(__dirname + '/../bot.js');
imageUploader = require(__dirname + '/../helpers/image-uploader.js'),
bot = require(__dirname + '/../bot/bot.js');

router.get('/', function (req, res) {
var content = grammar.flatten("#origin#"),
Expand All @@ -17,7 +17,7 @@ router.get('/', function (req, res) {
See https://www.npmjs.com/package/color-scheme#schemes on how to use ColorScheme.
*/

scheme.from_hex(helpers.get_random_hex().replace('#',''))
scheme.from_hex(helpers.getRandomHex().replace('#',''))
.scheme('mono')
.variation('hard');

Expand All @@ -30,13 +30,13 @@ router.get('/', function (req, res) {

var img_name = img_data.path.replace('img/', '');

image_uploader.upload_image(img_data, function(err, img_url, data){
imageUploader.uploadImage(img_data, function(err, img_url, data){
if (err){
console.log(err);
} else {
// console.log(img_url);

bot.create_post({
bot.createPost({
type: 'Note',
content: content,
attachment: [
Expand Down
6 changes: 3 additions & 3 deletions generators/triangular-mesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = function(options, cb) {
var line, dot,
odd = false,
lines = [],
gap = width / helpers.get_random_int(5,12);
gap = width / helpers.getRandomInt(5,12);

for (var y = -2 * gap / 2; y <= 2 * height; y+= gap) {
odd = !odd
Expand All @@ -66,7 +66,7 @@ module.exports = function(options, cb) {
ctx.lineTo(pointC.x, pointC.y);
ctx.lineTo(pointA.x, pointA.y);
ctx.closePath();
ctx.fillStyle = '#' + helpers.random_from_array(options.colors);
ctx.fillStyle = '#' + helpers.randomFromArray(options.colors);
ctx.fill();
ctx.stroke();
}
Expand Down Expand Up @@ -104,7 +104,7 @@ module.exports = function(options, cb) {
});
}
else{
var file_path = `${img_path}/${helpers.get_timestamp()}-${crypto.randomBytes(4).toString('hex')}`,
var file_path = `${img_path}/${helpers.getTimestamp()}-${crypto.randomBytes(4).toString('hex')}`,
file_path_png = `${file_path}.png`,
out = fs.createWriteStream(file_path_png),
stream = canvas.createPNGStream();
Expand Down
Loading

0 comments on commit 10d6d9c

Please sign in to comment.