fix(core): update
This commit is contained in:
@ -1,2 +1,3 @@
|
||||
export * from './slack.classes.slackaccount';
|
||||
export * from './slack.classes.slackmessage';
|
||||
export * from './slack.classes.slacklog';
|
||||
|
@ -10,15 +10,32 @@ export class SlackAccount {
|
||||
}
|
||||
|
||||
async sendMessage(optionsArg: {
|
||||
messageOptionsArg: IMessageOptions;
|
||||
messageOptions: IMessageOptions;
|
||||
channelArg: string;
|
||||
ts?: string;
|
||||
mode: 'new' | 'threaded' | 'update';
|
||||
}) {
|
||||
|
||||
let requestBody: any = {
|
||||
channel: optionsArg.channelArg,
|
||||
attachments: [optionsArg.messageOptionsArg]
|
||||
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) {
|
||||
@ -35,7 +52,11 @@ export class SlackAccount {
|
||||
thread_ts: optionsArg.ts
|
||||
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
console.log(requestBody);
|
||||
|
||||
const response = await plugins.smartrequest.postJson(postUrl, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${this.slackToken}`
|
||||
|
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```'
|
||||
})
|
||||
}
|
||||
|
||||
};
|
@ -53,15 +53,19 @@ export interface IMessageOptions {
|
||||
}
|
||||
|
||||
export class SlackMessage {
|
||||
slackmeRef: SlackAccount;
|
||||
messageOptions: IMessageOptions;
|
||||
channel: string;
|
||||
ts: string;
|
||||
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: SlackAccount) {
|
||||
if (slackmeArg) {
|
||||
this.slackmeRef = slackmeArg;
|
||||
public slackAccountRef: SlackAccount;
|
||||
public messageOptions: IMessageOptions;
|
||||
public channel: string;
|
||||
public ts: string;
|
||||
|
||||
public requestRunning = plugins.smartpromise.defer();
|
||||
|
||||
constructor(slackAccountArg: SlackAccount, messageOptionsArg: IMessageOptions) {
|
||||
if (slackAccountArg) {
|
||||
this.slackAccountRef = slackAccountArg;
|
||||
}
|
||||
this.messageOptions = messageOptionsArg;
|
||||
this.requestRunning.resolve();
|
||||
}
|
||||
|
||||
async updateAndSend(messageOptionsArg: IMessageOptions) {
|
||||
@ -76,15 +80,17 @@ export class SlackMessage {
|
||||
|
||||
async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') {
|
||||
this.channel = channelNameArg;
|
||||
if (this.slackmeRef) {
|
||||
const response = await this.slackmeRef.sendMessage({
|
||||
if (this.slackAccountRef) {
|
||||
const response = await this.slackAccountRef.sendMessage({
|
||||
channelArg: this.channel,
|
||||
messageOptionsArg: this.messageOptions,
|
||||
messageOptions: this.messageOptions,
|
||||
mode: modeArg,
|
||||
ts: this.ts
|
||||
});
|
||||
this.ts = response.body.message.ts;
|
||||
this.channel = response.body.channel;
|
||||
if (modeArg === 'new') {
|
||||
this.ts = response.body.message.ts;
|
||||
this.channel = response.body.channel;
|
||||
}
|
||||
} else {
|
||||
throw new Error('you need to set a slackRef before sending the message!');
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
// pushrocks scope
|
||||
import * as smartpromise from '@pushrocks/smartpromise';
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
|
||||
export { smartrequest };
|
||||
export { smartpromise, smartrequest };
|
||||
|
Reference in New Issue
Block a user