-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathlib.w
92 lines (80 loc) · 2.81 KB
/
lib.w
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
bring cloud;
bring http;
bring "./util.w" as helpers;
bring "./message.w" as msg;
bring "./events.w" as events;
bring "./context.w" as context;
bring "cdktf" as cdktf;
bring util;
bring ui;
/// Properties for Slack bot
pub struct AppProps {
/// The token secret to use for the app
token: cloud.Secret;
/// Whether events from bot users should be ignored (default: true)
ignoreBots: bool?;
}
pub class App {
pub api: cloud.Api;
eventHandlers: MutMap<inflight(context.EventContext, Json):Json?>;
ignoreBots: bool;
token: cloud.Secret;
isTest: bool;
new(props: AppProps) {
this.eventHandlers = MutMap<inflight (context.EventContext, Json): Json?>{};
this.ignoreBots = props?.ignoreBots ?? true;
this.token = props.token;
this.api = new cloud.Api();
let target = util.env("WING_TARGET");
if target == "tf-aws" {
new cdktf.TerraformOutput(value: "{this.api.url}/slack/events", description: "Slack Request URL") as "Slack_Request_Url";
}
this.isTest = nodeof(this).app.isTestEnvironment;
this.api.post("/slack/events", inflight (req) => {
let eventRequest = events.SlackEvent.parseJson(req.body!);
if eventRequest.type == "url_verification" {
let verificationEvent = events.VerificationEvent.parseJson(req.body!);
return {
status: 200,
body: Json.stringify({
challenge: verificationEvent.challenge
})
};
}
if eventRequest.type == "event_callback" {
let callBackEvent = events.CallbackEvent.fromJson(Json.parse(req.body!)["event"]);
if this.ignoreBots {
if callBackEvent.bot_id != nil || callBackEvent.app_id != nil {
return {};
}
}
if let handler = this.eventHandlers.tryGet(callBackEvent.type) {
let event = Json.parse(req.body!);
if this.isTest {
return {
status: 200,
body: Json.stringify(handler(new context.EventContext_Mock(event, ""), event))
};
} else {
// TODO: pass bot token as cloud.Secret rather than str once: https://github.com/winglang/winglibs/pull/229 is complete
return {
status: 200,
body: Json.stringify(handler(new context.EventContext(event, this.token.value()), event))
};
}
}
}
});
}
/// Register an event handler (for available events see: https://api.slack.com/events)
pub onEvent(eventName: str, handler: inflight(context.EventContext, Json): Json?) {
this.eventHandlers.set(eventName, handler);
}
/// Retrieve a channel object from a channel Id or name
pub inflight channel(id: str): context.Channel {
if this.isTest {
return new context.MockChannel(id, "");
}
return new context.Channel(id, this.token.value());
}
}