Demo Bot: first proof of concept.

This commit is contained in:
John Livingston
2021-12-07 13:14:01 +01:00
parent f8ce4e6583
commit 978ee83eee
9 changed files with 307 additions and 25 deletions

40
bots/.eslintrc.json Normal file
View File

@ -0,0 +1,40 @@
{
"root": true,
"env": {
"browser": false,
"es6": true
},
"extends": [
"standard-with-typescript"
],
"globals": {},
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"project": [
"./bots/tsconfig.json"
]
},
"plugins": [
"@typescript-eslint"
],
"ignorePatterns": [],
"rules": {
"@typescript-eslint/no-unused-vars": [2, {"argsIgnorePattern": "^_"}],
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-misused-promises": "error",
"@typescript-eslint/no-var-requires": "off",
"@typescript-eslint/strict-boolean-expressions": "off",
"@typescript-eslint/return-await": [2, "in-try-catch"], // FIXME: correct?
"@typescript-eslint/no-invalid-void-type": "off",
"@typescript-eslint/triple-slash-reference": "off",
"max-len": [
"error",
{
"code": 120,
"comments": 120
}
],
"no-unused-vars": "off"
}
}

78
bots/bots.ts Normal file
View File

@ -0,0 +1,78 @@
import * as path from 'path'
let demoBotConfigFile = process.argv[2]
if (!demoBotConfigFile) {
throw new Error('Missing parameter: the demobot configuration file path')
}
demoBotConfigFile = path.resolve(demoBotConfigFile)
// Not necessary, but just in case: perform some path checking...
function checkBotConfigFilePath (configPath: string): void {
const parts = configPath.split(path.sep)
if (!parts.includes('peertube-plugin-livechat')) {
// Indeed, the path should contain the plugin name
// (/var/www/peertube/storage/plugins/data/peertube-plugin-livechat/...)
throw new Error('demobot configuration file path seems invalid (not in peertube-plugin-livechat folder).')
}
if (parts[parts.length - 1] !== 'demobot.js') {
throw new Error('demobot configuration file path seems invalid (filename is not demobot.js).')
}
}
checkBotConfigFilePath(demoBotConfigFile)
const demoBotConf = require(demoBotConfigFile).getConf()
if (!demoBotConf || !demoBotConf.UUIDs || !demoBotConf.UUIDs.length) {
process.exit(0)
}
const { component, xml } = require('@xmpp/component')
const xmpp = component({
service: demoBotConf.service,
domain: demoBotConf.domain,
password: demoBotConf.password
})
const roomId = `${demoBotConf.UUIDs[0] as string}@${demoBotConf.mucDomain as string}`
xmpp.on('error', (err: any) => {
console.error(err)
})
xmpp.on('offline', () => {
console.log('offline')
})
xmpp.on('stanza', async (stanza: any) => {
console.log('stanza received' + (stanza?.toString ? ': ' + (stanza.toString() as string) : ''))
// if (stanza.is('message')) {
// console.log('stanza was a message: ' + (stanza.toString() as string))
// }
})
xmpp.on('online', async (address: any) => {
console.log('Online with address: ' + JSON.stringify(address))
const presence = xml(
'presence',
{
from: address.toString(),
to: roomId + '/DemoBot'
},
xml('x', {
xmlns: 'http://jabber.org/protocol/muc'
})
)
console.log('Sending presence...: ' + (presence.toString() as string))
await xmpp.send(presence)
setTimeout(() => {
const message = xml(
'message',
{ type: 'groupchat', to: roomId, from: address.toString() },
xml('body', {}, 'Hello world')
)
console.log('Sending message...: ' + (message.toString() as string))
xmpp.send(message)
}, 1000)
})
xmpp.start().catch(console.error)

26
bots/tsconfig.json Normal file
View File

@ -0,0 +1,26 @@
{
"extends": "@tsconfig/node12/tsconfig.json",
"compilerOptions": {
"moduleResolution": "node", // Tell tsc to look in node_modules for modules
"strict": true, // That implies alwaysStrict, noImplicitAny, noImplicitThis
"alwaysStrict": true, // should already be true because of strict:true
"noImplicitAny": true, // should already be true because of strict:true
"noImplicitThis": true, // should already be true because of strict:true
"noImplicitReturns": true,
"strictBindCallApply": true, // should already be true because of strict:true
"noUnusedLocals": true,
"removeComments": true,
"sourceMap": true,
"baseUrl": "./",
"outDir": "../dist/",
"paths": {}
},
"include": [
"./**/*",
"../shared/**/*"
],
"exclude": []
}