-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(slack): http mode,
session.quote
, reactions
- Loading branch information
1 parent
12253bd
commit 0935b98
Showing
7 changed files
with
154 additions
and
35 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,63 @@ | ||
import { Adapter, Schema } from '@satorijs/satori' | ||
import { Adapter, Logger, Schema } from '@satorijs/satori' | ||
import { SlackBot } from './bot' | ||
import crypto from 'node:crypto' | ||
import { EnvelopedEvent, SlackEvent, SocketEvent } from './types' | ||
import { adaptSession } from './utils' | ||
|
||
export class HttpServer extends Adapter.Server<SlackBot<SlackBot.BaseConfig & HttpServer.Config>> { | ||
export class HttpServer extends Adapter.Server<SlackBot> { | ||
logger = new Logger('slack') | ||
async start(bot: SlackBot) { | ||
// @ts-ignore | ||
const { signing } = bot.config | ||
const { userId } = await bot.getSelf() | ||
bot.selfId = userId | ||
bot.ctx.router.post('/slack', async (ctx) => { | ||
const timestamp = ctx.request.header['x-slack-request-timestamp'].toString() | ||
const signature = ctx.request.header['x-slack-signature'].toString() | ||
const requestBody = ctx.request.rawBody | ||
|
||
const hmac = crypto.createHmac('sha256', signing) | ||
const [version, hash] = signature.split('=') | ||
|
||
const fiveMinutesAgo = Math.floor(Date.now() / 1000) - 60 * 5 | ||
if (Number(timestamp) < fiveMinutesAgo) { | ||
return ctx.status = 403 | ||
} | ||
|
||
hmac.update(`${version}:${timestamp}:${requestBody}`) | ||
|
||
if (hash !== hmac.digest('hex')) { | ||
return ctx.status = 403 | ||
} | ||
const { type } = ctx.request.body as SocketEvent | ||
if (type === 'url_verification') { | ||
ctx.status = 200 | ||
return ctx.body = { | ||
challenge: ctx.request.body.challenge, | ||
} | ||
} | ||
// https://api.slack.com/apis/connections/events-api#receiving-events | ||
if (type === 'event_callback') { | ||
ctx.status = 200 | ||
ctx.body = 'ok' | ||
const payload: EnvelopedEvent<SlackEvent> = ctx.request.body | ||
this.logger.debug(require('util').inspect(payload, false, null, true)) | ||
const session = await adaptSession(bot, payload) | ||
this.logger.debug(require('util').inspect(session, false, null, true)) | ||
if (session) bot.dispatch(session) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export namespace HttpServer { | ||
export interface Config { | ||
protocol: 'http' | ||
signing: string | ||
} | ||
|
||
export const Config: Schema<Config> = Schema.object({ | ||
protocol: Schema.const('http').required(), | ||
signing: Schema.string().required(), | ||
}) | ||
} |
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