Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
ccc2fc689f | |||
9e3808bfee | |||
717dcab4ad | |||
6a8946f2b1 | |||
84e1624e64 | |||
b46d958f35 | |||
d044085739 | |||
3510767a72 | |||
ec17d7835e | |||
cb072d0f6a | |||
1240505227 | |||
09c0bc4e6f | |||
41f98b6869 | |||
e854a62eae |
29
.vscode/launch.json
vendored
Normal file
29
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"version": "0.2.0",
|
||||||
|
"configurations": [
|
||||||
|
{
|
||||||
|
"name": "current file",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"${relativeFile}"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "test.ts",
|
||||||
|
"type": "node",
|
||||||
|
"request": "launch",
|
||||||
|
"args": [
|
||||||
|
"test/test.ts"
|
||||||
|
],
|
||||||
|
"runtimeArgs": ["-r", "@gitzone/tsrun"],
|
||||||
|
"cwd": "${workspaceRoot}",
|
||||||
|
"protocol": "inspector",
|
||||||
|
"internalConsoleOptions": "openOnSessionStart"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
8
package-lock.json
generated
8
package-lock.json
generated
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/slack",
|
"name": "@mojoio/slack",
|
||||||
"version": "2.0.9",
|
"version": "3.0.3",
|
||||||
"lockfileVersion": 1,
|
"lockfileVersion": 1,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
@ -233,9 +233,9 @@
|
|||||||
"dev": true
|
"dev": true
|
||||||
},
|
},
|
||||||
"@pushrocks/smartpromise": {
|
"@pushrocks/smartpromise": {
|
||||||
"version": "3.0.2",
|
"version": "3.0.5",
|
||||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.2.tgz",
|
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartpromise/-/smartpromise-3.0.5.tgz",
|
||||||
"integrity": "sha512-jmrJMUEmBCWChWK8CIcx4Vw3wv/8OgVNmkaxJrbs+WMaoRUfJtpWWJfrAwwHWt9ZXJbarJ+CwfwfYiiZXymndQ=="
|
"integrity": "sha512-9kHBWyDFjQ6cV1rseOfge02EH6huh/mrtqxlFoJoxnMaGWf5F8H3UEsskBBUGI6QKE1Bl8evr74AIKWwJ0r/bA=="
|
||||||
},
|
},
|
||||||
"@pushrocks/smartrequest": {
|
"@pushrocks/smartrequest": {
|
||||||
"version": "1.1.23",
|
"version": "1.1.23",
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@mojoio/slack",
|
"name": "@mojoio/slack",
|
||||||
"version": "2.0.9",
|
"version": "3.0.3",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "slack api abstraction for the mojo.io ecosystem",
|
"description": "slack api abstraction for the mojo.io ecosystem",
|
||||||
"main": "dist/index.js",
|
"main": "dist/index.js",
|
||||||
@ -20,6 +20,7 @@
|
|||||||
"@types/node": "^12.7.4"
|
"@types/node": "^12.7.4"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"@pushrocks/smartpromise": "^3.0.5",
|
||||||
"@pushrocks/smartrequest": "^1.1.23"
|
"@pushrocks/smartrequest": "^1.1.23"
|
||||||
},
|
},
|
||||||
"files": [
|
"files": [
|
||||||
|
98
test/test.ts
98
test/test.ts
@ -5,15 +5,15 @@ let testQenv = new Qenv(process.cwd(), process.cwd() + '/.nogit');
|
|||||||
|
|
||||||
import * as slackme from '../ts/index';
|
import * as slackme from '../ts/index';
|
||||||
|
|
||||||
let testSlackme: slackme.Slackme;
|
let testSlackAccount: slackme.SlackAccount;
|
||||||
let testSlackMessage: slackme.SlackMessage;
|
let testSlackMessage: slackme.SlackMessage;
|
||||||
|
|
||||||
tap.test('should create a valid slackme instance', async () => {
|
tap.test('should create a valid slackme instance', async (tools) => {
|
||||||
testSlackme = new slackme.Slackme(testQenv.getEnvVarOnDemand('SLACK_TOKEN'));
|
testSlackAccount = new slackme.SlackAccount(testQenv.getEnvVarOnDemand('SLACK_TOKEN'));
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should send a message to Slack', async () => {
|
tap.test('should send a message to Slack', async (tools) => {
|
||||||
testSlackMessage = new slackme.SlackMessage({
|
const messageOptions = {
|
||||||
author_name: 'GitLab CI',
|
author_name: 'GitLab CI',
|
||||||
author_link: 'https://gitlab.com/',
|
author_link: 'https://gitlab.com/',
|
||||||
pretext: '*Good News*: Build successfull!',
|
pretext: '*Good News*: Build successfull!',
|
||||||
@ -29,15 +29,18 @@ tap.test('should send a message to Slack', async () => {
|
|||||||
value: 'pushrocks',
|
value: 'pushrocks',
|
||||||
short: true
|
short: true
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
ts: new Date().getTime() / 1000
|
};
|
||||||
|
await testSlackAccount.sendMessage({
|
||||||
|
channelArg: 'random',
|
||||||
|
messageOptions: messageOptions,
|
||||||
|
mode: 'new'
|
||||||
});
|
});
|
||||||
await testSlackme.sendMessage(testSlackMessage.messageOptions, 'random');
|
|
||||||
await expect(testSlackMessage.sendToRoom('random')).to.eventually.be.rejected;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should send a message to Slack by directly calling the message', async () => {
|
tap.test('should send a message to Slack by directly calling the message', async (tools) => {
|
||||||
testSlackMessage = new slackme.SlackMessage(
|
testSlackMessage = new slackme.SlackMessage(
|
||||||
|
testSlackAccount,
|
||||||
{
|
{
|
||||||
author_name: 'GitLab CI',
|
author_name: 'GitLab CI',
|
||||||
author_link: 'https://gitlab.com/',
|
author_link: 'https://gitlab.com/',
|
||||||
@ -54,12 +57,79 @@ tap.test('should send a message to Slack by directly calling the message', async
|
|||||||
value: 'pushrocks',
|
value: 'pushrocks',
|
||||||
short: true
|
short: true
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
ts: new Date().getTime() / 1000
|
}
|
||||||
},
|
|
||||||
testSlackme
|
|
||||||
);
|
);
|
||||||
await testSlackMessage.sendToRoom('random');
|
await testSlackMessage.sendToRoom('random');
|
||||||
|
await tools.delayFor(1000);
|
||||||
|
await testSlackMessage.updateAndSend({
|
||||||
|
author_name: 'GitLab CI',
|
||||||
|
author_link: 'https://gitlab.com/',
|
||||||
|
pretext: '*Good News*: Build successfull!',
|
||||||
|
color: '#3cb371',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
title: 'Branch',
|
||||||
|
value: 'Lossless Studio',
|
||||||
|
short: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Product ID',
|
||||||
|
value: 'pushrocks',
|
||||||
|
short: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
await testSlackMessage.updateAndSend({
|
||||||
|
author_name: 'GitLab CI',
|
||||||
|
author_link: 'https://gitlab.com/',
|
||||||
|
pretext: '*Good News*: Build successfull!',
|
||||||
|
color: '#3cb371',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
title: 'Branch',
|
||||||
|
value: 'Lossless Studio',
|
||||||
|
short: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Product ID',
|
||||||
|
value: 'onboard.me',
|
||||||
|
short: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
await testSlackMessage.startThread({
|
||||||
|
author_name: 'Lossless Compliance',
|
||||||
|
author_link: 'https://gitlab.com/',
|
||||||
|
pretext: '*Good News*: Build successfull!',
|
||||||
|
color: '#3cb371',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
title: 'Branch',
|
||||||
|
value: 'Lossless Studio',
|
||||||
|
short: true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
title: 'Product ID',
|
||||||
|
value: 'pushrocks',
|
||||||
|
short: true
|
||||||
|
}
|
||||||
|
]
|
||||||
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
|
tap.test('should send logs', async () => {
|
||||||
|
const slackLog = new slackme.SlackLog({
|
||||||
|
slackAccount: testSlackAccount,
|
||||||
|
channelName: 'random'
|
||||||
|
})
|
||||||
|
for (let i = 0; i < 30; i++) {
|
||||||
|
await slackLog.sendLogLine('hi there');
|
||||||
|
await slackLog.sendLogLine('so awesome');
|
||||||
|
await slackLog.sendLogLine('really');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
tap.start();
|
tap.start();
|
||||||
|
@ -1,2 +1,3 @@
|
|||||||
export * from './slack.classes.slackme';
|
export * from './slack.classes.slackaccount';
|
||||||
export * from './slack.classes.slackmessage';
|
export * from './slack.classes.slackmessage';
|
||||||
|
export * from './slack.classes.slacklog';
|
||||||
|
66
ts/slack.classes.slackaccount.ts
Normal file
66
ts/slack.classes.slackaccount.ts
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
import * as plugins from './slack.plugins';
|
||||||
|
import { IMessageOptions } from './slack.classes.slackmessage';
|
||||||
|
|
||||||
|
export class SlackAccount {
|
||||||
|
private postUrl = 'https://slack.com/api/chat.postMessage';
|
||||||
|
private updateUrl = 'https://slack.com/api/chat.update';
|
||||||
|
private slackToken: string;
|
||||||
|
constructor(slackTokenArg: string) {
|
||||||
|
this.slackToken = slackTokenArg;
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendMessage(optionsArg: {
|
||||||
|
messageOptions: IMessageOptions;
|
||||||
|
channelArg: string;
|
||||||
|
ts?: string;
|
||||||
|
mode: 'new' | 'threaded' | 'update';
|
||||||
|
}) {
|
||||||
|
|
||||||
|
let requestBody: any = {
|
||||||
|
channel: optionsArg.channelArg,
|
||||||
|
text: optionsArg.messageOptions.text,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (optionsArg.messageOptions.fields) {
|
||||||
|
requestBody = {
|
||||||
|
...requestBody,
|
||||||
|
attachments: [{
|
||||||
|
pretext: optionsArg.messageOptions.pretext,
|
||||||
|
fields: optionsArg.messageOptions.fields,
|
||||||
|
ts: optionsArg.messageOptions.ts,
|
||||||
|
color: optionsArg.messageOptions.color
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
let postUrl = this.postUrl;
|
||||||
|
|
||||||
|
switch (true) {
|
||||||
|
case optionsArg.ts && optionsArg.mode === 'update':
|
||||||
|
requestBody = {
|
||||||
|
...requestBody,
|
||||||
|
ts: optionsArg.ts
|
||||||
|
};
|
||||||
|
postUrl = this.updateUrl;
|
||||||
|
break;
|
||||||
|
case optionsArg.ts && optionsArg.mode === 'threaded':
|
||||||
|
requestBody = {
|
||||||
|
...requestBody,
|
||||||
|
thread_ts: optionsArg.ts
|
||||||
|
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await plugins.smartrequest.postJson(postUrl, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${this.slackToken}`
|
||||||
|
},
|
||||||
|
requestBody
|
||||||
|
});
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
}
|
32
ts/slack.classes.slacklog.ts
Normal file
32
ts/slack.classes.slacklog.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import { SlackAccount } from "./slack.classes.slackaccount";
|
||||||
|
import { SlackMessage } from "./slack.classes.slackmessage";
|
||||||
|
|
||||||
|
export class SlackLog {
|
||||||
|
public slackAccount: SlackAccount;
|
||||||
|
public slackMessage: SlackMessage;
|
||||||
|
public channelName: string;
|
||||||
|
|
||||||
|
public completeLog = ``;
|
||||||
|
|
||||||
|
constructor(optionsArg: {
|
||||||
|
slackAccount: SlackAccount;
|
||||||
|
channelName: string;
|
||||||
|
}) {
|
||||||
|
this.slackAccount = optionsArg.slackAccount;
|
||||||
|
this.channelName = optionsArg.channelName;
|
||||||
|
}
|
||||||
|
public async sendLogLine(logText: string) {
|
||||||
|
if (!this.slackMessage) {
|
||||||
|
this.slackMessage = new SlackMessage(this.slackAccount, {
|
||||||
|
text: '``` log is loading... ```'
|
||||||
|
});
|
||||||
|
await this.slackMessage.sendToRoom(this.channelName);
|
||||||
|
}
|
||||||
|
const date = new Date();
|
||||||
|
this.completeLog += `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()} - ` + logText + '\n';
|
||||||
|
await this.slackMessage.updateAndSend({
|
||||||
|
text: '```\n' + this.completeLog + '\n```'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
@ -1,20 +0,0 @@
|
|||||||
import * as plugins from './slack.plugins';
|
|
||||||
import { IMessageOptions } from './slack.classes.slackmessage';
|
|
||||||
|
|
||||||
export class Slackme {
|
|
||||||
private baseUrl = 'https://hooks.slack.com/services/';
|
|
||||||
private postRoute: string;
|
|
||||||
constructor(postRouteArg: string) {
|
|
||||||
this.postRoute = postRouteArg;
|
|
||||||
}
|
|
||||||
|
|
||||||
async sendMessage(messageOptionsArg: IMessageOptions, channelArg: string = 'general') {
|
|
||||||
const response = await plugins.smartrequest.postJson(`${this.baseUrl}${this.postRoute}`, {
|
|
||||||
requestBody: {
|
|
||||||
channel: channelArg,
|
|
||||||
attachments: [messageOptionsArg]
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,5 +1,5 @@
|
|||||||
import * as plugins from './slack.plugins';
|
import * as plugins from './slack.plugins';
|
||||||
import { Slackme } from './slack.classes.slackme';
|
import { SlackAccount } from './slack.classes.slackaccount';
|
||||||
|
|
||||||
export interface IAttachmentField {
|
export interface IAttachmentField {
|
||||||
title: string;
|
title: string;
|
||||||
@ -49,24 +49,48 @@ export interface IMessageOptions {
|
|||||||
thumb_url?: string;
|
thumb_url?: string;
|
||||||
footer?: string;
|
footer?: string;
|
||||||
footer_icon?: string;
|
footer_icon?: string;
|
||||||
/**
|
|
||||||
* timestamp as epoch time
|
|
||||||
*/
|
|
||||||
ts?: number;
|
ts?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class SlackMessage {
|
export class SlackMessage {
|
||||||
slackmeRef: Slackme;
|
public slackAccountRef: SlackAccount;
|
||||||
messageOptions: IMessageOptions;
|
public messageOptions: IMessageOptions;
|
||||||
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: Slackme) {
|
public channel: string;
|
||||||
if (slackmeArg) {
|
public ts: string;
|
||||||
this.slackmeRef = slackmeArg;
|
|
||||||
|
public requestRunning = plugins.smartpromise.defer();
|
||||||
|
|
||||||
|
constructor(slackAccountArg: SlackAccount, messageOptionsArg: IMessageOptions) {
|
||||||
|
if (slackAccountArg) {
|
||||||
|
this.slackAccountRef = slackAccountArg;
|
||||||
}
|
}
|
||||||
this.messageOptions = messageOptionsArg;
|
this.messageOptions = messageOptionsArg;
|
||||||
|
this.requestRunning.resolve();
|
||||||
}
|
}
|
||||||
async sendToRoom(roomNameArg: string) {
|
|
||||||
if (this.slackmeRef) {
|
async updateAndSend(messageOptionsArg: IMessageOptions) {
|
||||||
await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg);
|
this.messageOptions = messageOptionsArg;
|
||||||
|
await this.sendToRoom(this.channel, 'update');
|
||||||
|
}
|
||||||
|
|
||||||
|
async startThread(messageOptionsArg: IMessageOptions) {
|
||||||
|
this.messageOptions = messageOptionsArg;
|
||||||
|
this.sendToRoom(this.channel, 'threaded')
|
||||||
|
}
|
||||||
|
|
||||||
|
async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') {
|
||||||
|
this.channel = channelNameArg;
|
||||||
|
if (this.slackAccountRef) {
|
||||||
|
const response = await this.slackAccountRef.sendMessage({
|
||||||
|
channelArg: this.channel,
|
||||||
|
messageOptions: this.messageOptions,
|
||||||
|
mode: modeArg,
|
||||||
|
ts: this.ts
|
||||||
|
});
|
||||||
|
if (modeArg === 'new') {
|
||||||
|
this.ts = response.body.message.ts;
|
||||||
|
this.channel = response.body.channel;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
throw new Error('you need to set a slackRef before sending the message!');
|
throw new Error('you need to set a slackRef before sending the message!');
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
// pushrocks scope
|
// pushrocks scope
|
||||||
|
import * as smartpromise from '@pushrocks/smartpromise';
|
||||||
import * as smartrequest from '@pushrocks/smartrequest';
|
import * as smartrequest from '@pushrocks/smartrequest';
|
||||||
|
|
||||||
export { smartrequest };
|
export { smartpromise, smartrequest };
|
||||||
|
Reference in New Issue
Block a user