Skip to content

Commit

Permalink
feat: slack listener can fetch messages
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Jan 11, 2019
1 parent 9249ba6 commit bfa73c4
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
2 changes: 2 additions & 0 deletions docs/listener/slack-listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ metadata:
kind: slack-listener
name: slack-isolex
data:
fetch:
window: 6000
presence:
game:
name: Global Thermonuclear Warfare
Expand Down
6 changes: 3 additions & 3 deletions src/controller/SedController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export class SedController extends BaseController<SedControllerData> implements
}

return this.reply(ctx, this.translate('create.missing'));
} catch (error) {
this.logger.error('Failed to fetch messages.');
} catch (err) {
this.logger.error(err, 'Failed to fetch messages.');
}
}

Expand All @@ -58,7 +58,7 @@ export class SedController extends BaseController<SedControllerData> implements
}

private async processMessage(message: Message, command: Command, parts: RegExpMatchArray): Promise<boolean> {
if (doesExist(message.context) && doesExist(command.context) && message.context.channel.thread === command.context.channel.thread) {
if (doesExist(message.context) && doesExist(command.context) && message.context.channel.thread !== command.context.channel.thread) {
return false;
}

Expand Down
39 changes: 28 additions & 11 deletions src/listener/SlackListener.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RTMClient, WebAPICallResult, WebClient } from '@slack/client';
import { LogLevel, RTMClient, WebAPICallResult, WebClient } from '@slack/client';
import * as escape from 'escape-html';
import { isNil } from 'lodash';
import { BaseError, Inject, logWithLevel } from 'noicejs';
Expand All @@ -7,13 +7,15 @@ import { INJECT_CLOCK } from 'src/BaseService';
import { BotServiceOptions } from 'src/BotService';
import { Message } from 'src/entity/Message';
import { NotFoundError } from 'src/error/NotFoundError';
import { NotImplementedError } from 'src/error/NotImplementedError';
import { Listener, ListenerData } from 'src/listener';
import { FetchOptions, Listener, ListenerData } from 'src/listener';
import { SessionListener } from 'src/listener/SessionListener';
import { doesExist, mustExist } from 'src/utils';
import { TYPE_TEXT } from 'src/utils/Mime';

export interface SlackListenerData extends ListenerData {
fetch: {
window: number;
};
token: {
bot: string;
web: string;
Expand Down Expand Up @@ -71,19 +73,34 @@ export class SlackListener extends SessionListener<SlackListenerData> implements
this.logger.error('could not find destination in message context');
}

public async fetch(): Promise<Array<Message>> {
throw new NotImplementedError('slack listener cannot fetch specific messages yet');
public async fetch(options: FetchOptions): Promise<Array<Message>> {
const client = mustExist(this.webClient);
const latest = this.clock.getSeconds();
const oldest = latest - this.data.fetch.window;
const search = await client.channels.history({
channel: mustExist(options.channel),
inclusive: true,
latest: latest.toString(),
oldest: oldest.toString(),
}) as SlackSearchResults;

if (!search.ok) {
throw new NotFoundError('message not found for reaction');
}

const messages = await Promise.all(search.messages.map((msg) => this.convertMessage(msg)));
this.logger.debug({ messages }, 'fetched slack messages');
return messages;
}

public async start() {
await super.start();

this.client = new RTMClient(this.data.token.bot, {
logger: (level, msg) => {
logWithLevel(this.logger, level, { msg }, 'slack client logged message');
},
});
this.webClient = new WebClient(this.data.token.web);
const logger = (level: LogLevel, msg: string) => {
logWithLevel(this.logger, level, { msg }, 'slack client logged message');
};
this.client = new RTMClient(this.data.token.bot, { logger });
this.webClient = new WebClient(this.data.token.web, { logger });

this.client.on('message', (msg) => {
this.convertMessage(msg).then((it) => this.bot.receive(it)).catch((err) => {
Expand Down
4 changes: 2 additions & 2 deletions src/listener/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { Session } from 'src/entity/Session';
import { Service } from 'src/Service';

export interface FetchOptions {
after?: boolean;
before?: boolean;
after?: number;
before?: number;
channel: string;
count?: number;
id?: string;
Expand Down
9 changes: 8 additions & 1 deletion src/schema/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -647,8 +647,15 @@ definitions:
- $ref: "#/definitions/service-listener"
- type: object
additionalProperties: true
required: [token]
required: [fetch, token]
properties:
fetch:
type: object
additionalProperties: false
required: [window]
properties:
window:
type: number
token:
type: object
additionalProperties: false
Expand Down

0 comments on commit bfa73c4

Please sign in to comment.