fix(core): update

This commit is contained in:
2019-09-18 12:26:31 +02:00
parent ec86352b58
commit e854a62eae
4 changed files with 89 additions and 21 deletions

View File

@ -2,19 +2,34 @@ import * as plugins from './slack.plugins';
import { IMessageOptions } from './slack.classes.slackmessage';
export class Slackme {
private baseUrl = 'https://hooks.slack.com/services/';
private postRoute: string;
constructor(postRouteArg: string) {
this.postRoute = postRouteArg;
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 = 'general') {
const response = await plugins.smartrequest.postJson(`${this.baseUrl}${this.postRoute}`, {
requestBody: {
channel: channelArg,
attachments: [messageOptionsArg]
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

@ -49,24 +49,32 @@ export interface IMessageOptions {
thumb_url?: string;
footer?: string;
footer_icon?: string;
/**
* timestamp as epoch time
*/
ts?: number;
}
export class SlackMessage {
slackmeRef: Slackme;
messageOptions: IMessageOptions;
channel: string;
ts: string;
constructor(messageOptionsArg: IMessageOptions, slackmeArg?: Slackme) {
if (slackmeArg) {
this.slackmeRef = slackmeArg;
}
this.messageOptions = messageOptionsArg;
}
async updateAndSend(messageOptionsArg: IMessageOptions) {
this.messageOptions = messageOptionsArg;
await this.sendToRoom(this.channel);
}
async sendToRoom(roomNameArg: string) {
this.channel = roomNameArg;
if (this.slackmeRef) {
await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg);
const response = await this.slackmeRef.sendMessage(this.messageOptions, roomNameArg, this.ts);
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!');
}