Skip to content

Commit 7e5ebf1

Browse files
committed
Add contact info message shortcut
1 parent ce37158 commit 7e5ebf1

File tree

1 file changed

+42
-1
lines changed

1 file changed

+42
-1
lines changed

bot.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {App, UsersSelectAction} from '@slack/bolt';
1+
import {App, MessageShortcut, UsersSelectAction} from '@slack/bolt';
22
import {WebClient} from '@slack/web-api';
33
import {Actions, Header, Input, Message, Modal, Section, TextInput, UserSelect} from 'slack-block-builder';
44
import {getInfo} from './util/sheets';
@@ -38,6 +38,14 @@ app.action('info-select', async ({ack, payload, client, respond}) => {
3838
await respond(message);
3939
});
4040

41+
app.shortcut('info-shortcut', async ({ack, payload, client, shortcut}) => {
42+
await ack();
43+
const user = (shortcut as MessageShortcut).message.user;
44+
if (!user) return;
45+
const modal = await infoModal(client, user);
46+
await client.views.open({trigger_id: payload.trigger_id, view: modal});
47+
});
48+
4149
async function infoResponse(client: WebClient, id: string) {
4250
const res = await client.users.info({ token, user: id });
4351
if (!res.user?.real_name) return Message()
@@ -74,6 +82,39 @@ async function infoResponse(client: WebClient, id: string) {
7482
.buildToObject()
7583
}
7684

85+
// TODO: this can probably be abstracted further with the function above;
86+
// a common function might return only the block kit blocks passed to `.blocks(...)`,
87+
// but because the message has a dropdown and the modal does not this wouldn't quite work.
88+
async function infoModal(client: WebClient, id: string) {
89+
const res = await client.users.info({ token, user: id });
90+
if (!res.user?.real_name) return Modal({title: 'SEC contact info'})
91+
.blocks(
92+
Header({text: 'There was an error fetching your name.'}),
93+
Section({text: 'If this issue persists, please message <@U03GQC0A9MJ>.'})
94+
)
95+
.buildToObject()
96+
97+
const info = await getInfo(res.user.real_name);
98+
if (!info) return Modal({title: 'SEC contact info'})
99+
.blocks(
100+
Header({text: `${res.user.real_name} was not found on the contacts spreadsheet.`}),
101+
Section({text: 'If this is a mistake, please message <@U03GQC0A9MJ>.'})
102+
)
103+
.buildToObject()
104+
105+
const [lastName, firstName, position, email, cell, home] = info;
106+
const fields = [`*Email:*\n${email}`];
107+
if (cell) fields.push(`*Cell phone:*\n${parseCellPhone(cell)}`);
108+
if (home) fields.push(`*Home phone:*\n${parseCellPhone(home)}`);
109+
110+
return Modal({title: 'SEC contact info'})
111+
.blocks(
112+
Header({text: `Contact info for ${capitalize(firstName)} ${capitalize(lastName)} (${position})`}),
113+
Section().fields(fields)
114+
)
115+
.buildToObject()
116+
}
117+
77118
// /help
78119
// Sends info about other commands.
79120
app.command('/help', async ({ack, respond}) => {

0 commit comments

Comments
 (0)