@@ -72,94 +72,94 @@ To build the server to receive webhooks and interact with Slack:
72
72
73
73
// Create a Bolt Receiver
74
74
const receiver = new ExpressReceiver ({
75
- signingSecret: process .env .SLACK_SIGNING_SECRET ,
75
+ signingSecret: process .env .SLACK_SIGNING_SECRET ,
76
76
});
77
77
receiver .router .use (bodyParser .json ());
78
78
79
79
// Create the Bolt App, using the receiver
80
80
const app = new App ({
81
- token: process .env .SLACK_BOT_TOKEN ,
82
- logLevel: LogLevel .DEBUG ,
83
- receiver,
81
+ token: process .env .SLACK_BOT_TOKEN ,
82
+ logLevel: LogLevel .DEBUG ,
83
+ receiver,
84
84
});
85
85
86
86
receiver .router .post (" /v1/webhook" , async (req , res ) => {
87
- try {
88
- if (! req .body ) {
89
- return res .status (400 ).send (" Error: request body is missing" );
90
- }
91
-
92
- const { title , body } = req .body ;
93
- if (! title || ! body) {
94
- return res
95
- .status (400 )
96
- .send (' Error: missing fields: "title", or "body"' );
97
- }
98
-
99
- const payload = req .body .payload ;
100
- if (! payload) {
101
- return res .status (400 ).send (' Error: missing "payload" field' );
102
- }
103
-
104
- const { user_email , actions } = payload;
105
- if (! user_email || ! actions) {
106
- return res
107
- .status (400 )
108
- .send (' Error: missing fields: "user_email", "actions"' );
109
- }
110
-
111
- // Get the user ID using Slack API
112
- const userByEmail = await app .client .users .lookupByEmail ({
113
- email: user_email,
114
- });
115
-
116
- const slackMessage = {
117
- channel: userByEmail .user .id ,
118
- text: body,
119
- blocks: [
120
- {
121
- type: " header" ,
122
- text: { type: " plain_text" , text: title },
123
- },
124
- {
125
- type: " section" ,
126
- text: { type: " mrkdwn" , text: body },
127
- },
128
- ],
129
- };
130
-
131
- // Add action buttons if they exist
132
- if (actions && actions .length > 0 ) {
133
- slackMessage .blocks .push ({
134
- type: " actions" ,
135
- elements: actions .map ((action ) => ({
136
- type: " button" ,
137
- text: { type: " plain_text" , text: action .label },
138
- url: action .url ,
139
- })),
140
- });
141
- }
142
-
143
- // Post message to the user on Slack
144
- await app .client .chat .postMessage (slackMessage);
145
-
146
- res .status (204 ).send ();
147
- } catch (error) {
148
- console .error (" Error sending message:" , error);
149
- res .status (500 ).send ();
150
- }
87
+ try {
88
+ if (! req .body ) {
89
+ return res .status (400 ).send (" Error: request body is missing" );
90
+ }
91
+
92
+ const { title , body } = req .body ;
93
+ if (! title || ! body) {
94
+ return res
95
+ .status (400 )
96
+ .send (' Error: missing fields: "title", or "body"' );
97
+ }
98
+
99
+ const payload = req .body .payload ;
100
+ if (! payload) {
101
+ return res .status (400 ).send (' Error: missing "payload" field' );
102
+ }
103
+
104
+ const { user_email , actions } = payload;
105
+ if (! user_email || ! actions) {
106
+ return res
107
+ .status (400 )
108
+ .send (' Error: missing fields: "user_email", "actions"' );
109
+ }
110
+
111
+ // Get the user ID using Slack API
112
+ const userByEmail = await app .client .users .lookupByEmail ({
113
+ email: user_email,
114
+ });
115
+
116
+ const slackMessage = {
117
+ channel: userByEmail .user .id ,
118
+ text: body,
119
+ blocks: [
120
+ {
121
+ type: " header" ,
122
+ text: { type: " plain_text" , text: title },
123
+ },
124
+ {
125
+ type: " section" ,
126
+ text: { type: " mrkdwn" , text: body },
127
+ },
128
+ ],
129
+ };
130
+
131
+ // Add action buttons if they exist
132
+ if (actions && actions .length > 0 ) {
133
+ slackMessage .blocks .push ({
134
+ type: " actions" ,
135
+ elements: actions .map ((action ) => ({
136
+ type: " button" ,
137
+ text: { type: " plain_text" , text: action .label },
138
+ url: action .url ,
139
+ })),
140
+ });
141
+ }
142
+
143
+ // Post message to the user on Slack
144
+ await app .client .chat .postMessage (slackMessage);
145
+
146
+ res .status (204 ).send ();
147
+ } catch (error) {
148
+ console .error (" Error sending message:" , error);
149
+ res .status (500 ).send ();
150
+ }
151
151
});
152
152
153
153
// Acknowledge clicks on link_button, otherwise Slack UI
154
154
// complains about missing events.
155
155
app .action (" button_click" , async ({ body, ack, say }) => {
156
- await ack (); // no specific action needed
156
+ await ack (); // no specific action needed
157
157
});
158
158
159
159
// Start the Bolt app
160
160
(async () => {
161
- await app .start (port);
162
- console .log (" ⚡️ Coder Slack bot is running!" );
161
+ await app .start (port);
162
+ console .log (" ⚡️ Coder Slack bot is running!" );
163
163
})();
164
164
```
165
165
0 commit comments