Compare commits
No commits in common. "main" and "v1.1.0" have entirely different histories.
|
@ -45,14 +45,6 @@ Then run `npm install .` to install npm modules `axios` and `ws`.
|
|||
|
||||
Start the bot with the traditional `node index.js`.
|
||||
|
||||
However, you probably want this to run automatically on a web server. You can do this with `systemd` using the example unit file: `mastodon-clippy.service.example`. Adjust this for your user and paths, and then activate it:
|
||||
|
||||
```bash
|
||||
sudo cp mastodon-clippy.service.example /etc/systemd/system/mastodon-clippy.service
|
||||
sudo systemctl enable mastodon-clippy.service
|
||||
sudo systemctl start mastodon-clippy service
|
||||
```
|
||||
|
||||
## interacting with the bot
|
||||
|
||||
To "sign up" for notification from your bot, users have two options:
|
||||
|
|
145
index.js
145
index.js
|
@ -26,14 +26,8 @@ const headers = {
|
|||
'Authorization' : `Bearer ${access_token}`
|
||||
}
|
||||
|
||||
function resetConnection(socket) {
|
||||
terminate(socket)
|
||||
console.log(`waiting after error`)
|
||||
setTimeout( function() { listen() }, 5000)
|
||||
}
|
||||
|
||||
// set up bot account with correct settings
|
||||
function initiateSettings(socket) {
|
||||
function initiateSettings() {
|
||||
|
||||
let account = {
|
||||
locked: false,
|
||||
|
@ -46,50 +40,37 @@ function initiateSettings(socket) {
|
|||
return axios.patch(`https://${domain}/api/v1/accounts/update_credentials`, account, { headers: headers })
|
||||
.catch( err => {
|
||||
console.error('ERROR applying bot user settings: ', err.message)
|
||||
terminate(socket)
|
||||
})
|
||||
}
|
||||
|
||||
// return random suggestion string
|
||||
function suggestion(username) {
|
||||
function suggestion() {
|
||||
|
||||
const n = crypto.randomInt(12)
|
||||
const n = crypto.randomInt(6)
|
||||
|
||||
switch(n) {
|
||||
case 0:
|
||||
return 'How about logging off instead?';
|
||||
case 1:
|
||||
return `Would you like to delete your toot, @${username}?`;
|
||||
return 'Would you like to delete your toot?';
|
||||
case 2:
|
||||
return 'Can I help you take a walk outside? 🚶➡️';
|
||||
return 'Can I help you take a walk outside?';
|
||||
case 3:
|
||||
return 'You may like to reconsider your life choices.';
|
||||
case 4:
|
||||
return 'Why not try looking at #CatsOfMastodon instead?';
|
||||
return 'Why not try looking at #CatsOfInstagram instead?';
|
||||
case 5:
|
||||
return `Come on @${username}, we've talked about this. 🤷♂️`;
|
||||
case 6:
|
||||
return `You should go look at some trees. Trees are calming 🌳`;
|
||||
case 7:
|
||||
return `I'm not angry. I'm just very disappointed. 😔`;
|
||||
case 8:
|
||||
return `You said you were going to stop doing that ...and yet here we are.`;
|
||||
case 9:
|
||||
return `Time to touch some grass 🌱`;
|
||||
case 10:
|
||||
return `Why not have a nice cup of tea instead? 🫖`;
|
||||
case 11:
|
||||
return `And yet you still haven't read all of those books in your TBR pile. 🤔`;
|
||||
return `You're better than this, come on.`;
|
||||
}
|
||||
}
|
||||
|
||||
// send a message when someone toots about the topic
|
||||
function sendResponse(rid, user, username) {
|
||||
function sendResponse(rip, user) {
|
||||
|
||||
let payload = {
|
||||
'status' : `@${user} It looks like you're posting about '${topic}'. ${suggestion(username)}`,
|
||||
'status' : `@${user} It looks like you're posting about '${topic}'. ${suggestion()}`,
|
||||
'spoiler_text' : topic,
|
||||
'in_reply_to_id' : rid,
|
||||
'in_reply_to_id' : rip,
|
||||
}
|
||||
|
||||
axios.post(`https://${domain}/api/v1/statuses`, payload, { headers: headers })
|
||||
|
@ -127,80 +108,66 @@ function filterMentions(text, mentions) {
|
|||
// STREAMING USER TIMELINE
|
||||
// This is where the action is!
|
||||
// ***********************
|
||||
const ws = new WebSocket(`wss://${domain}/api/v1/streaming?access_token=${access_token}&stream=user`)
|
||||
|
||||
function terminate(socket) {
|
||||
console.error(`Terminating connection...`)
|
||||
socket.terminate()
|
||||
console.log(`Terminated`)
|
||||
}
|
||||
// make sure bot is set up correctly each time it starts
|
||||
initiateSettings()
|
||||
|
||||
function listen() {
|
||||
// errors
|
||||
ws.on('error', err => {
|
||||
console.error(`WebSocket error: ${err.message}`)
|
||||
})
|
||||
|
||||
console.log(`Listening...`)
|
||||
const ws = new WebSocket(`wss://${domain}/api/v1/streaming?access_token=${access_token}&stream=user`)
|
||||
// check updates and notifications in the stream
|
||||
ws.on('message', msg => {
|
||||
let packet = JSON.parse(msg)
|
||||
let data = JSON.parse(packet.payload)
|
||||
|
||||
// make sure bot is set up correctly each time it starts
|
||||
initiateSettings(ws)
|
||||
// notifications
|
||||
if (packet.event == 'notification') {
|
||||
|
||||
// errors
|
||||
ws.on('error', err => {
|
||||
console.error(`WebSocket error: ${err.message}`)
|
||||
resetConnection(ws)
|
||||
})
|
||||
// always follow back
|
||||
if (data.type == 'follow') {
|
||||
followAction(data.account.id, 'follow')
|
||||
}
|
||||
|
||||
// check updates and notifications in the stream
|
||||
ws.on('message', msg => {
|
||||
let packet = JSON.parse(msg)
|
||||
let data = JSON.parse(packet.payload)
|
||||
if (data.type == 'mention') {
|
||||
|
||||
// notifications
|
||||
if (packet.event == 'notification') {
|
||||
let post = data.status.content
|
||||
|
||||
// always follow back
|
||||
if (data.type == 'follow') {
|
||||
// check start requests
|
||||
if (post.match(/\bSTART\b/)) {
|
||||
followAction(data.account.id, 'follow')
|
||||
}
|
||||
|
||||
if (data.type == 'mention') {
|
||||
|
||||
let post = data.status.content
|
||||
|
||||
// check start requests
|
||||
if (post.match(/\bSTART\b/)) {
|
||||
followAction(data.account.id, 'follow')
|
||||
}
|
||||
|
||||
// check stop requests
|
||||
if (post.match(/\STOP\b/)) {
|
||||
followAction(data.account.id, 'unfollow')
|
||||
}
|
||||
// check stop requests
|
||||
if (post.match(/\STOP\b/)) {
|
||||
followAction(data.account.id, 'unfollow')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// updates (posts)
|
||||
if (packet.event == 'update') {
|
||||
let rid = data.id
|
||||
let user = data.account.acct
|
||||
let username = data.account.username
|
||||
// get just the account names (@name@domain.tld)
|
||||
let mentions = data.mentions.map( mention => mention.acct)
|
||||
// exclude own toots and @mentions to avoid an infinite loops
|
||||
if (username !== clippy && !mentions.includes(clippy)) {
|
||||
// get rid of mentions in case topic is within a username
|
||||
let text = filterMentions(data.content, mentions)
|
||||
if ( text.toLowerCase().includes(topic) ) {
|
||||
sendResponse(rid, user, username)
|
||||
}
|
||||
else if (data.spoiler_text.toLowerCase().includes(topic)) {
|
||||
sendResponse(rid, user, username)
|
||||
}
|
||||
else if (data.tags.map(tag => tag.name.toLowerCase()).includes(topic)) {
|
||||
sendResponse(rid, user, username)
|
||||
}
|
||||
// updates (posts)
|
||||
if (packet.event == 'update') {
|
||||
let rip = data.id
|
||||
let user = data.account.acct
|
||||
// get just the account names (@name@domain.tld)
|
||||
let mentions = data.mentions.map( mention => mention.acct)
|
||||
// exclude own toots and @mentions to avoid an infinite loops
|
||||
if (data.account.username !== clippy && !mentions.includes(clippy)) {
|
||||
// get rid of mentions in case topic is within a username
|
||||
let text = filterMentions(data.content, mentions)
|
||||
if ( text.toLowerCase().includes(topic) ) {
|
||||
sendResponse(rip, user)
|
||||
}
|
||||
else if (data.spoiler_text.toLowerCase().includes(topic)) {
|
||||
sendResponse(rip, user)
|
||||
}
|
||||
else if (data.tags.map(tag => tag.name.toLowerCase()).includes(topic)) {
|
||||
sendResponse(rip, user)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
// let's go
|
||||
listen()
|
22
package-lock.json
generated
22
package-lock.json
generated
|
@ -1,26 +1,26 @@
|
|||
{
|
||||
"name": "mastodon-clippy",
|
||||
"version": "1.1.1",
|
||||
"version": "1.1.0",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
"axios": {
|
||||
"version": "0.21.4",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.4.tgz",
|
||||
"integrity": "sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==",
|
||||
"version": "0.21.1",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
|
||||
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
|
||||
"requires": {
|
||||
"follow-redirects": "^1.14.0"
|
||||
"follow-redirects": "^1.10.0"
|
||||
}
|
||||
},
|
||||
"follow-redirects": {
|
||||
"version": "1.15.6",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz",
|
||||
"integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA=="
|
||||
"version": "1.14.1",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.14.1.tgz",
|
||||
"integrity": "sha512-HWqDgT7ZEkqRzBvc2s64vSZ/hfOceEol3ac/7tKwzuvEyWx3/4UegXh5oBOIotkGsObyk3xznnSRVADBgWSQVg=="
|
||||
},
|
||||
"ws": {
|
||||
"version": "7.5.10",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.5.10.tgz",
|
||||
"integrity": "sha512-+dbF1tHwZpXcbOJdVOkzLDxZP1ailvSxM6ZweXTegylPny803bFhA+vqBYw4s31NSAk4S2Qz+AKXK9a4wkdjcQ=="
|
||||
"version": "7.4.5",
|
||||
"resolved": "https://registry.npmjs.org/ws/-/ws-7.4.5.tgz",
|
||||
"integrity": "sha512-xzyu3hFvomRfXKH8vOFMU3OguG6oOvhXMo3xsGy3xWExqaM2dxBbVxuD99O7m3ZUFMvvscsZDqxfgMaRr/Nr1g=="
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
{
|
||||
"name": "mastodon-clippy",
|
||||
"version": "1.1.2",
|
||||
"version": "1.1.0",
|
||||
"description": "Mastodon clippy bot",
|
||||
"repository": "https://git.suboptimal.solutions/hugh/mastodon-clippy",
|
||||
"repository": "https://github.com/hughrun/mastodon-clippy.git",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
|
@ -10,7 +10,7 @@
|
|||
"author": "Hugh Rundle <hugh@hughrundle.net> (https://www.hughrundle.net)",
|
||||
"license": "AGPL-3.0-or-later",
|
||||
"dependencies": {
|
||||
"axios": "^1.7.3",
|
||||
"ws": "^8.18"
|
||||
"axios": "^0.21.1",
|
||||
"ws": "^7.4.5"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue