slack/ts/classes.slackmessage.ts

99 lines
2.3 KiB
TypeScript
Raw Permalink Normal View History

2024-01-25 00:10:10 +01:00
import * as plugins from './plugins.js';
2024-01-25 00:09:32 +01:00
import { SlackAccount } from './classes.slackaccount.js';
2017-09-17 15:25:00 +02:00
export interface IAttachmentField {
2018-09-15 22:23:05 +02:00
title: string;
value: string;
short?: boolean;
2017-09-17 15:25:00 +02:00
}
export interface IMessageOptions {
/**
* "Required plain-text summary of the attachment."
*/
2018-09-15 22:23:05 +02:00
fallback?: string;
/**
* a side color
*/
2018-09-15 22:23:05 +02:00
color?: string;
/**
* a message to show above
*/
2018-09-15 22:23:05 +02:00
pretext?: string;
2017-09-17 15:25:00 +02:00
/**
* author name of the attachment
*/
2018-09-15 22:23:05 +02:00
author_name?: string;
2017-09-17 15:25:00 +02:00
/**
* a link to the author
*/
2018-09-15 22:23:05 +02:00
author_link?: string;
2017-09-17 15:25:00 +02:00
/**
* a string to the author
*/
2018-09-15 22:23:05 +02:00
author_icon?: string;
2017-09-17 15:25:00 +02:00
/**
* a title for the attachment
*/
2018-09-15 22:23:05 +02:00
title?: string;
2017-09-17 15:25:00 +02:00
/**
* a link for the title
*/
2018-09-15 22:23:05 +02:00
title_link?: string;
/**
* the main text of the message
*/
2018-09-15 22:23:05 +02:00
text?: string;
fields?: IAttachmentField[];
image_url?: string;
thumb_url?: string;
footer?: string;
footer_icon?: string;
ts?: number;
}
export class SlackMessage {
2019-09-18 16:47:31 +02:00
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;
}
2018-09-15 22:23:05 +02:00
this.messageOptions = messageOptionsArg;
2019-09-18 16:47:31 +02:00
this.requestRunning.resolve();
}
2019-09-18 12:26:31 +02:00
async updateAndSend(messageOptionsArg: IMessageOptions) {
this.messageOptions = messageOptionsArg;
2019-09-18 15:47:51 +02:00
await this.sendToRoom(this.channel, 'update');
2019-09-18 12:26:31 +02:00
}
2019-09-18 15:47:51 +02:00
async startThread(messageOptionsArg: IMessageOptions) {
this.messageOptions = messageOptionsArg;
2019-09-18 19:37:45 +02:00
this.sendToRoom(this.channel, 'threaded');
2019-09-18 15:47:51 +02:00
}
async sendToRoom(channelNameArg: string, modeArg: 'new' | 'update' | 'threaded' = 'new') {
this.channel = channelNameArg;
2019-09-18 16:47:31 +02:00
if (this.slackAccountRef) {
const response = await this.slackAccountRef.sendMessage({
2019-09-18 15:47:51 +02:00
channelArg: this.channel,
2019-09-18 16:47:31 +02:00
messageOptions: this.messageOptions,
2019-09-18 15:47:51 +02:00
mode: modeArg,
ts: this.ts
});
2019-09-18 16:47:31 +02:00
if (modeArg === 'new') {
this.ts = response.body.message.ts;
this.channel = response.body.channel;
}
2019-09-05 13:08:42 +02:00
} else {
throw new Error('you need to set a slackRef before sending the message!');
}
}
}