-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
938 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { App, KnownEventFromType, ReactionAddedEvent, ReactionRemovedEvent, SlackEvent } from "@slack/bolt"; | ||
import { Main, METRIC_RECEIVED_MESSAGE } from "./Main"; | ||
import { SlackEventHandler } from "./SlackEventHandler"; | ||
import { Logger } from "matrix-appservice-bridge"; | ||
import { ISlackEvent } from "./BaseSlackHandler"; | ||
|
||
const log = new Logger("SlackAppHandler"); | ||
|
||
interface Config { | ||
appToken: string, | ||
signingSecret: string, | ||
botToken: string, | ||
} | ||
|
||
export class SlackAppHandler extends SlackEventHandler { | ||
private constructor( | ||
main: Main, | ||
private app: App, | ||
private teamId: string | ||
) { | ||
super(main); | ||
this.app.message(async ({ message }) => this.handleSlackAppEvent(message)); | ||
this.app.event(new RegExp('^reaction'), ({ event }) => this.handleSlackAppEvent(event)); | ||
} | ||
|
||
public static async create(main: Main, config: Config) { | ||
const app = new App({ | ||
appToken: config.appToken, | ||
token: config.botToken, | ||
signingSecret: config.signingSecret, | ||
socketMode: true, | ||
}); | ||
await app.start(); | ||
const teamId = await main.clientFactory.upsertTeamByToken(config.botToken); | ||
log.info(`Slack App listening for events for team ${teamId}`); | ||
return new SlackAppHandler(main, app, teamId); | ||
} | ||
|
||
private async handleSlackAppEvent(ev: SlackEvent) { | ||
try { | ||
switch (ev.type) { | ||
case 'message': return this.onMessage(ev); | ||
case 'reaction_added': | ||
case 'reaction_removed': | ||
return this.onReaction(ev); | ||
default: | ||
log.warn(`Ignoring event of type ${ev.type}`); | ||
} | ||
} catch (err) { | ||
log.error(`Failed to handle Slack App event ${JSON.stringify(ev, undefined, 2)}: ${err}`); | ||
} | ||
} | ||
|
||
private async onMessage(msg: KnownEventFromType<'message'>) { | ||
log.debug("Received a message:", msg); | ||
|
||
this.main.incCounter(METRIC_RECEIVED_MESSAGE, { side: "remote" }); | ||
|
||
switch (msg.subtype) { | ||
case undefined: // regular message | ||
return this.handleEvent({ | ||
...msg, | ||
user_id: msg.user, | ||
}, this.teamId); | ||
default: | ||
log.warn(`Unhandled message subtype ${msg.subtype}`); | ||
} | ||
} | ||
|
||
async onReaction(ev: ReactionAddedEvent|ReactionRemovedEvent): Promise<void> { | ||
return this.handleEvent(ev as unknown as ISlackEvent, this.teamId); | ||
} | ||
} |
Oops, something went wrong.