fix(core): update

This commit is contained in:
Philipp Kunz 2019-09-18 15:47:51 +02:00
parent d044085739
commit b46d958f35
5 changed files with 104 additions and 40 deletions

View File

@ -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) => {
@ -75,6 +79,42 @@ tap.test('should send a message to Slack by directly calling the message', async
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';

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,33 +0,0 @@
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(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;
}
const response = await plugins.smartrequest.postJson(postUrl, {
headers: {
'Authorization': `Bearer ${this.slackToken}`
},
requestBody
});
return response;
}
}

View File

@ -1,5 +1,5 @@
import * as plugins from './slack.plugins';
import { SlackAccount } from './slack.classes.slackme';
import { SlackAccount } from './slack.classes.slackaccount';
export interface IAttachmentField {
title: string;
@ -66,13 +66,23 @@ export class SlackMessage {
async updateAndSend(messageOptionsArg: IMessageOptions) {
this.messageOptions = messageOptionsArg;
await this.sendToRoom(this.channel);
await this.sendToRoom(this.channel, 'update');
}
async sendToRoom(roomNameArg: string) {
this.channel = roomNameArg;
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.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.channel = response.body.channel;
} else {