/
callback.ts
66 lines (60 loc) · 1.67 KB
/
callback.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { APIGatewayProxyHandler } from 'aws-lambda';
import { InvokeCommand, LambdaClient } from '@aws-sdk/client-lambda';
import { type LambdaEvent } from './api-invoker';
const lambdaClient = new LambdaClient();
interface VerificationRequest {
type: 'url_verification';
challenge: string;
}
interface EventCallBack {
type: 'event_callback';
event: {
type: 'app_mention';
subtype?: string;
user: string;
channel: string;
text: string;
ts: string;
thread_ts: string;
};
}
type SlackRequest = VerificationRequest | EventCallBack;
// FIXME: request verification
// https://api.slack.com/authentication/verifying-requests-from-slack
export const handler: APIGatewayProxyHandler = async (event) => {
const request: SlackRequest = JSON.parse(event.body || '{}');
console.debug({ request });
switch (request.type) {
case 'event_callback': {
const event: LambdaEvent = {
channel: request.event.channel,
text: request.event.text.replaceAll(/<@U[0-9A-Z]+>/g, ''), // メンション自体を除去
ts: request.event.ts,
threadTs: request.event.thread_ts,
threadBroadcast: request.event.subtype === 'thread_broadcast'
};
await lambdaClient.send(
new InvokeCommand({
InvocationType: 'Event', // 非同期実行
FunctionName: process.env.API_INVOKER_NAME,
Payload: JSON.stringify(event)
})
);
return {
statusCode: 200,
body: '',
headers: {
'Content-Type': 'text/plain'
}
};
}
case 'url_verification':
return {
statusCode: 200,
body: request.challenge,
headers: {
'Content-Type': 'text/plain'
}
};
}
};