8 Commits

Author SHA1 Message Date
84e1624e64 3.0.1 2019-09-18 15:47:51 +02:00
b46d958f35 fix(core): update 2019-09-18 15:47:51 +02:00
d044085739 3.0.0 2019-09-18 15:11:55 +02:00
3510767a72 BREAKING CHANGE(changed main class to SlackAccount): update 2019-09-18 15:11:55 +02:00
ec17d7835e 2.0.12 2019-09-18 12:28:42 +02:00
cb072d0f6a fix(core): update 2019-09-18 12:28:41 +02:00
1240505227 2.0.11 2019-09-18 12:27:52 +02:00
09c0bc4e6f fix(core): update 2019-09-18 12:27:52 +02:00
7 changed files with 111 additions and 49 deletions

2
package-lock.json generated
View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/slack", "name": "@mojoio/slack",
"version": "2.0.10", "version": "3.0.1",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View File

@ -1,6 +1,6 @@
{ {
"name": "@mojoio/slack", "name": "@mojoio/slack",
"version": "2.0.10", "version": "3.0.1",
"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",

View File

@ -5,11 +5,11 @@ 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 testSlackme: slackme.SlackAccount;
let testSlackMessage: slackme.SlackMessage; let testSlackMessage: slackme.SlackMessage;
tap.test('should create a valid slackme instance', async (tools) => { tap.test('should create a valid slackme instance', async (tools) => {
testSlackme = new slackme.Slackme(testQenv.getEnvVarOnDemand('SLACK_TOKEN')); testSlackme = new slackme.SlackAccount(testQenv.getEnvVarOnDemand('SLACK_TOKEN'));
}); });
tap.test('should send a message to Slack', async (tools) => { tap.test('should send a message to Slack', async (tools) => {
@ -31,7 +31,11 @@ tap.test('should send a message to Slack', async (tools) => {
} }
] ]
}); });
await testSlackme.sendMessage(testSlackMessage.messageOptions, 'random'); await testSlackme.sendMessage({
channelArg: 'random',
messageOptionsArg: testSlackMessage.messageOptions,
mode: 'new'
});
}); });
tap.test('should send a message to Slack by directly calling the message', async (tools) => { tap.test('should send a message to Slack by directly calling the message', async (tools) => {
@ -57,7 +61,7 @@ tap.test('should send a message to Slack by directly calling the message', async
testSlackme testSlackme
); );
await testSlackMessage.sendToRoom('random'); await testSlackMessage.sendToRoom('random');
await tools.delayFor(5000); await tools.delayFor(1000);
await testSlackMessage.updateAndSend({ await testSlackMessage.updateAndSend({
author_name: 'GitLab CI', author_name: 'GitLab CI',
author_link: 'https://gitlab.com/', author_link: 'https://gitlab.com/',
@ -75,6 +79,42 @@ tap.test('should send a message to Slack by directly calling the message', async
short: true 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
}
]
}) })
}); });

View File

@ -1,2 +1,2 @@
export * from './slack.classes.slackme'; export * from './slack.classes.slackaccount';
export * from './slack.classes.slackmessage'; export * from './slack.classes.slackmessage';

View File

@ -0,0 +1,47 @@
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: {
messageOptionsArg: IMessageOptions;
channelArg: string;
ts?: string;
mode: 'new' | 'threaded' | 'update';
}) {
let requestBody: any = {
channel: optionsArg.channelArg,
attachments: [optionsArg.messageOptionsArg]
};
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
}
}
const response = await plugins.smartrequest.postJson(postUrl, {
headers: {
Authorization: `Bearer ${this.slackToken}`
},
requestBody
});
return response;
}
}

View File

@ -1,35 +0,0 @@
import * as plugins from './slack.plugins';
import { IMessageOptions } from './slack.classes.slackmessage';
export class Slackme {
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(messageOptionsArg: IMessageOptions, channelArg: string = 'status', tsArg?: string) {
let requestBody: any = {
channel: channelArg,
attachments: [messageOptionsArg]
}
let postUrl = this.postUrl;
if (tsArg) {
requestBody = {
...requestBody,
ts: tsArg
}
postUrl = this.updateUrl;
}
console.log(requestBody);
const response = await plugins.smartrequest.postJson(postUrl, {
headers: {
'Authorization': `Bearer ${this.slackToken}`
},
requestBody
});
console.log(response.body);
return response;
}
}

View File

@ -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;
@ -53,11 +53,11 @@ export interface IMessageOptions {
} }
export class SlackMessage { export class SlackMessage {
slackmeRef: Slackme; slackmeRef: SlackAccount;
messageOptions: IMessageOptions; messageOptions: IMessageOptions;
channel: string; channel: string;
ts: string; ts: string;
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: Slackme) { constructor(messageOptionsArg: IMessageOptions, slackmeArg?: SlackAccount) {
if (slackmeArg) { if (slackmeArg) {
this.slackmeRef = slackmeArg; this.slackmeRef = slackmeArg;
} }
@ -66,13 +66,23 @@ export class SlackMessage {
async updateAndSend(messageOptionsArg: IMessageOptions) { async updateAndSend(messageOptionsArg: IMessageOptions) {
this.messageOptions = messageOptionsArg; this.messageOptions = messageOptionsArg;
await this.sendToRoom(this.channel); await this.sendToRoom(this.channel, 'update');
} }
async sendToRoom(roomNameArg: string) { async startThread(messageOptionsArg: IMessageOptions) {
this.channel = roomNameArg; this.messageOptions = messageOptionsArg;
this.sendToRoom(this.channel, 'threaded')
}
async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') {
this.channel = channelNameArg;
if (this.slackmeRef) { if (this.slackmeRef) {
const response = await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg, this.ts); const response = await this.slackmeRef.sendMessage({
channelArg: this.channel,
messageOptionsArg: this.messageOptions,
mode: modeArg,
ts: this.ts
});
this.ts = response.body.message.ts; this.ts = response.body.message.ts;
this.channel = response.body.channel; this.channel = response.body.channel;
} else { } else {