diff --git a/Readme.md b/Readme.md index de1ae3d..bf2bdc2 100644 --- a/Readme.md +++ b/Readme.md @@ -15,6 +15,7 @@ The following notification destinations are supported: | ---------- | --------------------------------------------------------------------------------- | | smtpServer | email | | webHooks | Webhook, the $msg variable can be used to insert the docker-notify update message | +| twitter | twitter API, $msg variable can be used for tweet text | ## Setup @@ -116,6 +117,16 @@ The `config.json` looks like the following: "httpBody": { "text": "$msg" } + }, + { + "twitter": { + "rasaAlerts": { + "appKey": "your_app_api_key", + "appSecret": "your_app_api_secret", + "accessToken": "your_app_access_token", + "accessSecret": "your_app_access_secret", + "message": "$msg" + } } } } diff --git a/package.json b/package.json index 2886b6d..3768b97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "docker-notify", - "version": "1.1.0", + "version": "1.2.0", "description": "", "main": "index.js", "scripts": { @@ -12,7 +12,8 @@ "ajv": "^8.6.3", "ajv-formats": "^2.1.1", "axios": "^0.24.0", - "nodemailer": "^6.7.0" + "nodemailer": "^6.7.0", + "twitter-api-v2": "^1.6.4" }, "devDependencies": { "eslint": "^7.32.0", diff --git a/src/index.js b/src/index.js index 8df407a..efd056a 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,5 @@ const dockerAPI = new (require('./DockerAPI').DockerAPI)(); +const { TwitterApi } = require('twitter-api-v2'); const mailService = require('./mailService'); const Cache = require('./Cache'); const schema = require('./schema.json'); @@ -57,8 +58,8 @@ const notifyServices = config.notifyServices; // these things are: smtp-server referenced in notifyJob is existing and // webhooks referenced in notifyJob is existing if (!notifyServices.every((o) => o.actions.every((o2) => o2.type === 'webHook' ? config.webHooks[o2.instance] : - o2.type === 'mailHook' ? config.smtpServer[o2.instance] : false))){ - logger.error('Mail/Smtp Hooks that are referenced are not defined!'); + o2.type === 'mailHook' ? config.smtpServer[o2.instance] : o2.type === 'twitter' ? config.twitter[o2.instance] : false))) { + logger.error('Mail/Smtp/twitter Hooks that are referenced are not defined!'); process.exit(3); } @@ -236,6 +237,25 @@ const checkForUpdates = function () { mailHookSend(o2.instance, o2.recipient, o.updatedString, 'Docker image \'' + o.updatedString + '\' was updated:\n' + JSON.stringify(o.job.image, null, 2)); } + else if (o2.type == 'twitter') { + const twitter = config.twitter[o2.instance]; + const twitterClient = new TwitterApi({ + appKey: twitter.appKey, + appSecret: twitter.appSecret, + accessToken: twitter.accessToken, + accessSecret: twitter.accessSecret, + }); + + (async () => { + try { + const message = twitter.message.replace('$msg', 'Docker image \'' + o.updatedString + '\' was updated'); + await twitterClient.v1.tweet(message); + } catch (err) { + logger.error('tweet error: ', err); + logger.error('tweet error: ', err.data.errors.message); + } + })(); + } else { logger.error('Trying to execute an unknown hook(' + o2.type + '), falling back to printing to console'); logger.error('Image: ' + JSON.stringify(o.job.image)); diff --git a/src/schema.json b/src/schema.json index 9ec59c3..21a4d52 100644 --- a/src/schema.json +++ b/src/schema.json @@ -74,6 +74,28 @@ "recipient" ], "additionalProperties": false + }, + { + "type": "object", + "description": "A twitter instance consists of the reference in the twitter Array and the type specified as twitter", + "properties": { + "type": { + "type": "string", + "description": "As we are creating a twitter API instance, this can only be twitter", + "enum": [ + "twitter" + ] + }, + "instance": { + "type": "string", + "description": "A reference to a object in the list of webhooks (Reference to id)" + } + }, + "required": [ + "type", + "instance" + ], + "additionalProperties": false } ] }, @@ -164,6 +186,44 @@ } }, "additionalProperties": false + }, + "twitter": { + "type": "object", + "patternProperties": { + "^..*$": { + "type": "object", + "properties": { + "appKey": { + "type": "string", + "minLength": 10 + }, + "appSecret": { + "type": "string", + "minLength": 20 + }, + "accessToken": { + "type": "string", + "minLength": 10 + }, + "accessSecret": { + "type": "string", + "minLength": 20 + }, + "message": { + "type": "string", + "default": "$msg" + } + }, + "required": [ + "appKey", + "appSecret", + "accessToken", + "accessSecret" + ], + "additionalProperties": false + } + }, + "additionalProperties": false } }, "required": [