slack/ts/classes.slackaccount.ts

64 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-01-24 23:10:10 +00:00
import * as plugins from './plugins.js';
2024-01-24 23:09:32 +00:00
import { type IMessageOptions } from './classes.slackmessage.js';
2019-09-18 13:47:51 +00:00
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: {
2019-09-18 14:47:31 +00:00
messageOptions: IMessageOptions;
2019-09-18 13:47:51 +00:00
channelArg: string;
ts?: string;
mode: 'new' | 'threaded' | 'update';
}) {
let requestBody: any = {
channel: optionsArg.channelArg,
2019-09-18 17:37:45 +00:00
text: optionsArg.messageOptions.text
2019-09-18 13:47:51 +00:00
};
2019-09-18 14:47:31 +00:00
if (optionsArg.messageOptions.fields) {
requestBody = {
...requestBody,
2019-09-18 17:37:45 +00:00
attachments: [
{
pretext: optionsArg.messageOptions.pretext,
fields: optionsArg.messageOptions.fields,
ts: optionsArg.messageOptions.ts,
color: optionsArg.messageOptions.color
}
]
};
2019-09-18 14:47:31 +00:00
}
2019-09-18 13:47:51 +00:00
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
2019-09-18 17:37:45 +00:00
};
2019-09-18 14:47:31 +00:00
break;
2019-09-18 13:47:51 +00:00
}
2019-09-18 14:47:31 +00:00
2019-09-18 13:47:51 +00:00
const response = await plugins.smartrequest.postJson(postUrl, {
headers: {
Authorization: `Bearer ${this.slackToken}`
},
requestBody
});
return response;
}
}